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.h270
-rw-r--r--core/src/fxcodec/codec/fx_codec.cpp447
-rw-r--r--core/src/fxcodec/codec/fx_codec_fax.cpp968
-rw-r--r--core/src/fxcodec/codec/fx_codec_flate.cpp945
-rw-r--r--core/src/fxcodec/codec/fx_codec_icc.cpp1360
-rw-r--r--core/src/fxcodec/codec/fx_codec_jbig.cpp212
-rw-r--r--core/src/fxcodec/codec/fx_codec_jbig_enc.cpp11
-rw-r--r--core/src/fxcodec/codec/fx_codec_jpeg.cpp735
-rw-r--r--core/src/fxcodec/codec/fx_codec_jpx_opj.cpp799
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/include/fx_openjpeg.h13
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/CMakeLists.txt98
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.c194
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.h134
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.c658
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.h399
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.c925
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.h122
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.c146
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.h102
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.c119
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.h131
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.c240
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.h68
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/indexbox_manager.h148
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.c294
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.h64
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.c10295
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.h844
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.c2825
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.h495
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.c342
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.h155
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.c610
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.h207
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.c960
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.h1501
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.c64
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.h59
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_codec.h165
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h2
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h.cmake.in2
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config_private.h43
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_includes.h180
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_intmath.h178
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_inttypes.h48
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_malloc.h194
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_stdint.h52
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.c1876
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.h188
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.c95
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.h106
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.c1757
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.h163
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_generate_luts.c282
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_luts.h143
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.c1344
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.h133
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.c2129
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.h354
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.c337
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.h146
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_bio.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_cio.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_dwt.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_event.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_function_list.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_image.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_invert.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_j2k.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_j2k_lib.c9
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_jpt.c9
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_mct.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_mqc.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg_jp2.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_opj_clock.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_pi.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_raw.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_t1.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_t1_generate_luts.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_t2.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_tcd.c10
-rw-r--r--core/src/fxcodec/fx_libopenjpeg/src/fx_tgt.c10
-rw-r--r--core/src/fxcodec/fx_zlib/include/fx_zlib.h10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_adler32.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_compress.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_crc32.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_deflate.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_gzclose.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_gzlib.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_gzread.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_gzwrite.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_infback.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_inffast.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_inflate.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_inftrees.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_trees.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_uncompr.c10
-rw-r--r--core/src/fxcodec/fx_zlib/src/fx_zlib_zutil.c10
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/crc32.h441
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/deflate.h346
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_adler32.c179
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_compress.c80
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_crc32.c425
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_deflate.c1966
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_infback.c640
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inffast.c340
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inflate.c1513
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inftrees.c306
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_trees.c1226
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_uncompr.c59
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_zutil.c349
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/gzclose.c25
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/gzguts.h227
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/gzlib.c634
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/gzread.c594
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/gzwrite.c576
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/inffast.h11
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/inffixed.h94
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/inflate.h122
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/inftrees.h62
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/trees.h128
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/zconf.h519
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/zlib.h1832
-rw-r--r--core/src/fxcodec/fx_zlib/zlib_v128/zutil.h255
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithDecoder.h126
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp105
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h39
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithQe.h64
-rw-r--r--core/src/fxcodec/jbig2/JBig2_BitStream.h316
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.cpp1812
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.h135
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Define.h34
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp4290
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h278
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp55
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h24
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp193
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.h42
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h251
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.cpp1619
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.h68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_List.h67
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Module.h32
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.cpp72
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.h43
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Page.h19
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.cpp24
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.h22
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.cpp53
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.h68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp34
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.h26
-rw-r--r--core/src/fxcodec/lcms2/include/fx_lcms2.h10
-rw-r--r--core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h10
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2.h1882
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2_plugin.h637
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmscam02.c486
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmscgats.c2775
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmscnvrt.c1142
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c707
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgamma.c1298
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c590
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmshalf.c534
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsintrp.c1506
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio0.c1883
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio1.c1020
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmslut.c1793
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmd5.c343
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmtrx.c175
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsnamed.c929
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsopt.c1795
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmspack.c3369
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmspcs.c931
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsplugin.c955
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsps2.c1597
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmssamp.c572
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmssm.c734
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmstypes.c5564
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsvirt.c1194
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmswtpnt.c349
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsxform.c1132
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/lcms2_internal.h1032
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscam02.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscgats.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscnvrt.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmserr.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsgamma.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsgmt.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmshalf.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsintrp.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsio0.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsio1.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmslut.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsmd5.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsmtrx.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsnamed.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsopt.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmspack.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmspcs.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsplugin.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsps2.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmssamp.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmssm.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmstypes.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsvirt.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmswtpnt.c7
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsxform.c7
-rw-r--r--core/src/fxcodec/libjpeg/cderror.h132
-rw-r--r--core/src/fxcodec/libjpeg/cdjpeg.h184
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c283
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c164
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c452
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jccolor.c462
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c390
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jchuff.c915
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcinit.c75
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c296
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c667
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c593
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c109
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcparam.c613
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c836
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c357
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcsample.c522
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jctrans.c391
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c398
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c279
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c739
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c399
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c272
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c657
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdinput.c384
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c515
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c1396
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c560
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c406
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c671
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c293
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdsample.c481
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c146
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jerror.c255
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c227
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c286
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c371
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctint.c392
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctred.c401
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c1123
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c126
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jutils.c182
-rw-r--r--core/src/fxcodec/libjpeg/jchuff.h47
-rw-r--r--core/src/fxcodec/libjpeg/jconfig.h45
-rw-r--r--core/src/fxcodec/libjpeg/jdct.h176
-rw-r--r--core/src/fxcodec/libjpeg/jdhuff.h201
-rw-r--r--core/src/fxcodec/libjpeg/jerror.h291
-rw-r--r--core/src/fxcodec/libjpeg/jinclude.h102
-rw-r--r--core/src/fxcodec/libjpeg/jmemsys.h200
-rw-r--r--core/src/fxcodec/libjpeg/jmorecfg.h376
-rw-r--r--core/src/fxcodec/libjpeg/jpegint.h392
-rw-r--r--core/src/fxcodec/libjpeg/jpeglib.h1165
-rw-r--r--core/src/fxcodec/libjpeg/jversion.h14
-rw-r--r--core/src/fxcodec/libjpeg/makefile8
-rw-r--r--core/src/fxcodec/libjpeg/transupp.h135
263 files changed, 119785 insertions, 0 deletions
diff --git a/core/src/fxcodec/codec/codec_int.h b/core/src/fxcodec/codec/codec_int.h
new file mode 100644
index 0000000000..571af61470
--- /dev/null
+++ b/core/src/fxcodec/codec/codec_int.h
@@ -0,0 +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:
+};
diff --git a/core/src/fxcodec/codec/fx_codec.cpp b/core/src/fxcodec/codec/fx_codec.cpp
new file mode 100644
index 0000000000..2f586ca94b
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_fax.cpp b/core/src/fxcodec/codec/fx_codec_fax.cpp
new file mode 100644
index 0000000000..21c7deb076
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_fax.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_flate.cpp b/core/src/fxcodec/codec/fx_codec_flate.cpp
new file mode 100644
index 0000000000..b6f94e021c
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_flate.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_icc.cpp b/core/src/fxcodec/codec/fx_codec_icc.cpp
new file mode 100644
index 0000000000..65617642fe
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_icc.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_jbig.cpp b/core/src/fxcodec/codec/fx_codec_jbig.cpp
new file mode 100644
index 0000000000..a644e99c5d
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_jbig.cpp
@@ -0,0 +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();
+}
+
+
+
diff --git a/core/src/fxcodec/codec/fx_codec_jbig_enc.cpp b/core/src/fxcodec/codec/fx_codec_jbig_enc.cpp
new file mode 100644
index 0000000000..4a982e66a8
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_jbig_enc.cpp
@@ -0,0 +1,11 @@
+// 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"
+ICodec_Jbig2Encoder* CCodec_ModuleMgr::CreateJbig2Encoder()
+{
+ return NULL;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_jpeg.cpp b/core/src/fxcodec/codec/fx_codec_jpeg.cpp
new file mode 100644
index 0000000000..fc91a6c162
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_jpeg.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
new file mode 100644
index 0000000000..423819f69f
--- /dev/null
+++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/include/fx_openjpeg.h b/core/src/fxcodec/fx_libopenjpeg/include/fx_openjpeg.h
new file mode 100644
index 0000000000..da480f22a0
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/include/fx_openjpeg.h
@@ -0,0 +1,13 @@
+// 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_OPENJPEG_H_
+#define _FX_OPENJPEG_H_
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/openjpeg.h"
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/CMakeLists.txt b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/CMakeLists.txt
new file mode 100644
index 0000000000..5c87f29771
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/CMakeLists.txt
@@ -0,0 +1,98 @@
+include_regular_expression("^.*$")
+
+#
+install( FILES ${CMAKE_CURRENT_BINARY_DIR}/opj_config.h
+ DESTINATION ${OPENJPEG_INSTALL_INCLUDE_DIR} COMPONENT Headers)
+
+include_directories(
+ ${OPENJPEG_BINARY_DIR}/src/lib/openjp2 # opj_config.h and opj_config_private.h
+)
+# Defines the source code for the library
+set(OPENJPEG_SRCS
+ ${CMAKE_CURRENT_SOURCE_DIR}/bio.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/cio.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/dwt.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/event.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/image.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/invert.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/j2k.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/jp2.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/mct.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/mqc.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/opj_clock.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/pi.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/raw.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/t1.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/t2.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/tcd.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/tgt.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/function_list.c
+)
+if(BUILD_JPIP)
+ add_definitions(-DUSE_JPIP)
+ set(OPENJPEG_SRCS
+ ${OPENJPEG_SRCS}
+ ${CMAKE_CURRENT_SOURCE_DIR}/cidx_manager.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/phix_manager.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/ppix_manager.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/thix_manager.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/tpix_manager.c
+ )
+endif()
+
+# Build the library
+if(WIN32)
+ if(BUILD_SHARED_LIBS)
+ add_definitions(-DOPJ_EXPORTS)
+ else()
+ add_definitions(-DOPJ_STATIC)
+ endif()
+endif()
+add_library(${OPENJPEG_LIBRARY_NAME} ${OPENJPEG_SRCS})
+if(UNIX)
+ target_link_libraries(${OPENJPEG_LIBRARY_NAME} m)
+endif()
+set_target_properties(${OPENJPEG_LIBRARY_NAME} PROPERTIES ${OPENJPEG_LIBRARY_PROPERTIES})
+
+# Install library
+install(TARGETS ${OPENJPEG_LIBRARY_NAME}
+ EXPORT OpenJPEGTargets
+ RUNTIME DESTINATION ${OPENJPEG_INSTALL_BIN_DIR} COMPONENT Applications
+ LIBRARY DESTINATION ${OPENJPEG_INSTALL_LIB_DIR} COMPONENT Libraries
+ ARCHIVE DESTINATION ${OPENJPEG_INSTALL_LIB_DIR} COMPONENT Libraries
+)
+
+# Install includes files
+install(FILES openjpeg.h opj_stdint.h
+ DESTINATION ${OPENJPEG_INSTALL_INCLUDE_DIR} COMPONENT Headers
+)
+
+# install man page of the library
+install(
+ FILES ${OPENJPEG_SOURCE_DIR}/doc/man/man3/libopenjp2.3
+ DESTINATION ${OPENJPEG_INSTALL_MAN_DIR}/man3)
+
+# internal utilities to generate t1_luts.h (part of the jp2 lib)
+# no need to install:
+add_executable(t1_generate_luts t1_generate_luts.c)
+if(UNIX)
+ target_link_libraries(t1_generate_luts m)
+endif()
+
+# Experimental option; let's how cppcheck performs
+# Implementation details:
+# I could not figure out how to easily upload a file to CDash. Instead simply
+# pretend cppcheck is part of the Build step. Technically cppcheck can even
+# output gcc formatted error/warning report
+# Another implementation detail: I could not redirect error to the error
+# catching mechanism something is busted in cmake 2.8.5, I had to use the
+# warning regex to catch them.
+if(OPENJPEG_CPPCHECK)
+ find_package(CPPCHECK REQUIRED)
+ foreach(f ${OPENJPEG_SRCS})
+ # cppcheck complains about too many configuration, pretend to be WIN32:
+ add_custom_command(TARGET ${OPENJPEG_LIBRARY_NAME}
+ COMMAND ${CPPCHECK_EXECUTABLE} -DWIN32 ${f})
+ endforeach()
+endif()
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.c
new file mode 100644
index 0000000000..5d49580178
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.c
@@ -0,0 +1,194 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup BIO BIO - Individual bit input-output stream */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Write a bit
+@param bio BIO handle
+@param b Bit to write (0 or 1)
+*/
+static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
+/**
+Read a bit
+@param bio BIO handle
+@return Returns the read bit
+*/
+static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio);
+/**
+Write a byte
+@param bio BIO handle
+@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
+*/
+static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio);
+/**
+Read a byte
+@param bio BIO handle
+@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
+*/
+static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio);
+
+/*@}*/
+
+/*@}*/
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+OPJ_BOOL opj_bio_byteout(opj_bio_t *bio) {
+ bio->buf = (bio->buf << 8) & 0xffff;
+ bio->ct = bio->buf == 0xff00 ? 7 : 8;
+ if (bio->bp >= bio->end) {
+ return OPJ_FALSE;
+ }
+ *bio->bp++ = (OPJ_BYTE)(bio->buf >> 8);
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_bio_bytein(opj_bio_t *bio) {
+ bio->buf = (bio->buf << 8) & 0xffff;
+ bio->ct = bio->buf == 0xff00 ? 7 : 8;
+ if (bio->bp >= bio->end) {
+ return OPJ_FALSE;
+ }
+ bio->buf |= *bio->bp++;
+ return OPJ_TRUE;
+}
+
+void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b) {
+ if (bio->ct == 0) {
+ opj_bio_byteout(bio); /* MSD: why not check the return value of this function ? */
+ }
+ bio->ct--;
+ bio->buf |= b << bio->ct;
+}
+
+OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio) {
+ if (bio->ct == 0) {
+ opj_bio_bytein(bio); /* MSD: why not check the return value of this function ? */
+ }
+ bio->ct--;
+ return (bio->buf >> bio->ct) & 1;
+}
+
+/*
+==========================================================
+ Bit Input/Output interface
+==========================================================
+*/
+
+opj_bio_t* opj_bio_create(void) {
+ opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
+ return bio;
+}
+
+void opj_bio_destroy(opj_bio_t *bio) {
+ if(bio) {
+ opj_free(bio);
+ }
+}
+
+ptrdiff_t opj_bio_numbytes(opj_bio_t *bio) {
+ return (bio->bp - bio->start);
+}
+
+void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) {
+ bio->start = bp;
+ bio->end = bp + len;
+ bio->bp = bp;
+ bio->buf = 0;
+ bio->ct = 8;
+}
+
+void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) {
+ bio->start = bp;
+ bio->end = bp + len;
+ bio->bp = bp;
+ bio->buf = 0;
+ bio->ct = 0;
+}
+
+void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) {
+ OPJ_UINT32 i;
+ for (i = n - 1; i < n; i--) {
+ opj_bio_putbit(bio, (v >> i) & 1);
+ }
+}
+
+OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n) {
+ OPJ_UINT32 i;
+ OPJ_UINT32 v;
+ v = 0;
+ for (i = n - 1; i < n; i--) {
+ v += opj_bio_getbit(bio) << i;
+ }
+ return v;
+}
+
+OPJ_BOOL opj_bio_flush(opj_bio_t *bio) {
+ bio->ct = 0;
+ if (! opj_bio_byteout(bio)) {
+ return OPJ_FALSE;
+ }
+ if (bio->ct == 7) {
+ bio->ct = 0;
+ if (! opj_bio_byteout(bio)) {
+ return OPJ_FALSE;
+ }
+ }
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_bio_inalign(opj_bio_t *bio) {
+ bio->ct = 0;
+ if ((bio->buf & 0xff) == 0xff) {
+ if (! opj_bio_bytein(bio)) {
+ return OPJ_FALSE;
+ }
+ bio->ct = 0;
+ }
+ return OPJ_TRUE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.h
new file mode 100644
index 0000000000..fba2428476
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/bio.h
@@ -0,0 +1,134 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __BIO_H
+#define __BIO_H
+
+#include <stddef.h> /* ptrdiff_t */
+
+/**
+@file bio.h
+@brief Implementation of an individual bit input-output (BIO)
+
+The functions in BIO.C have for goal to realize an individual bit input - output.
+*/
+
+/** @defgroup BIO BIO - Individual bit input-output stream */
+/*@{*/
+
+/**
+Individual bit input-output stream (BIO)
+*/
+typedef struct opj_bio {
+ /** pointer to the start of the buffer */
+ OPJ_BYTE *start;
+ /** pointer to the end of the buffer */
+ OPJ_BYTE *end;
+ /** pointer to the present position in the buffer */
+ OPJ_BYTE *bp;
+ /** temporary place where each byte is read or written */
+ OPJ_UINT32 buf;
+ /** coder : number of bits free to write. decoder : number of bits read */
+ OPJ_UINT32 ct;
+} opj_bio_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new BIO handle
+@return Returns a new BIO handle if successful, returns NULL otherwise
+*/
+opj_bio_t* opj_bio_create(void);
+/**
+Destroy a previously created BIO handle
+@param bio BIO handle to destroy
+*/
+void opj_bio_destroy(opj_bio_t *bio);
+/**
+Number of bytes written.
+@param bio BIO handle
+@return Returns the number of bytes written
+*/
+ptrdiff_t opj_bio_numbytes(opj_bio_t *bio);
+/**
+Init encoder
+@param bio BIO handle
+@param bp Output buffer
+@param len Output buffer length
+*/
+void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len);
+/**
+Init decoder
+@param bio BIO handle
+@param bp Input buffer
+@param len Input buffer length
+*/
+void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len);
+/**
+Write bits
+@param bio BIO handle
+@param v Value of bits
+@param n Number of bits to write
+*/
+void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n);
+/**
+Read bits
+@param bio BIO handle
+@param n Number of bits to read
+@return Returns the corresponding read number
+*/
+OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n);
+/**
+Flush bits
+@param bio BIO handle
+@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
+*/
+OPJ_BOOL opj_bio_flush(opj_bio_t *bio);
+/**
+Passes the ending bits (coming from flushing)
+@param bio BIO handle
+@return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
+*/
+OPJ_BOOL opj_bio_inalign(opj_bio_t *bio);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __BIO_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.c
new file mode 100644
index 0000000000..6cba658a98
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.c
@@ -0,0 +1,658 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ----------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+
+void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
+
+ assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
+
+ memcpy(p_buffer,l_data_ptr,p_nb_bytes);
+}
+
+void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1;
+ OPJ_UINT32 i;
+
+ assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
+
+ for (i=0;i<p_nb_bytes;++i) {
+ *(p_buffer++) = *(l_data_ptr--);
+ }
+}
+
+void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
+
+ assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
+
+ *p_value = 0;
+ memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
+}
+
+void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1;
+ OPJ_UINT32 i;
+
+ assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
+
+ *p_value = 0;
+ for (i=0;i<p_nb_bytes;++i) {
+ *(l_data_ptr--) = *(p_buffer++);
+ }
+}
+
+void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
+ memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT64));
+}
+
+void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT64) - 1;
+ OPJ_UINT32 i;
+ for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
+ *(p_buffer++) = *(l_data_ptr--);
+ }
+}
+
+void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
+ memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT64));
+}
+
+void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64)-1;
+ OPJ_UINT32 i;
+ for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
+ *(l_data_ptr--) = *(p_buffer++);
+ }
+}
+
+void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
+ memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT32));
+}
+
+void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
+{
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT32) - 1;
+ OPJ_UINT32 i;
+ for (i=0;i<sizeof(OPJ_FLOAT32);++i) {
+ *(p_buffer++) = *(l_data_ptr--);
+ }
+}
+
+void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
+ memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT32));
+}
+
+void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
+{
+ OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32)-1;
+ OPJ_UINT32 i;
+ for (i=0;i<sizeof(OPJ_FLOAT32);++i) {
+ *(l_data_ptr--) = *(p_buffer++);
+ }
+}
+
+opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,OPJ_BOOL l_is_input)
+{
+ opj_stream_private_t * l_stream = 00;
+ l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
+ if (! l_stream) {
+ return 00;
+ }
+
+ memset(l_stream,0,sizeof(opj_stream_private_t));
+ l_stream->m_buffer_size = p_buffer_size;
+ l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size);
+ if (! l_stream->m_stored_data) {
+ opj_free(l_stream);
+ return 00;
+ }
+
+ l_stream->m_current_data = l_stream->m_stored_data;
+
+ if (l_is_input) {
+ l_stream->m_status |= opj_stream_e_input;
+ l_stream->m_opj_skip = opj_stream_read_skip;
+ l_stream->m_opj_seek = opj_stream_read_seek;
+ }
+ else {
+ l_stream->m_status |= opj_stream_e_output;
+ l_stream->m_opj_skip = opj_stream_write_skip;
+ l_stream->m_opj_seek = opj_stream_write_seek;
+ }
+
+ l_stream->m_read_fn = opj_stream_default_read;
+ l_stream->m_write_fn = opj_stream_default_write;
+ l_stream->m_skip_fn = opj_stream_default_skip;
+ l_stream->m_seek_fn = opj_stream_default_seek;
+
+ return (opj_stream_t *) l_stream;
+}
+
+opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL l_is_input)
+{
+ return opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,l_is_input);
+}
+
+void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if (l_stream) {
+ if (l_stream->m_free_user_data_fn) {
+ l_stream->m_free_user_data_fn(l_stream->m_user_data);
+ }
+ opj_free(l_stream->m_stored_data);
+ l_stream->m_stored_data = 00;
+ opj_free(l_stream);
+ }
+}
+
+void OPJ_CALLCONV opj_stream_destroy_v3(opj_stream_t* p_stream)
+{
+ opj_stream_destroy(p_stream);
+}
+
+void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input))) {
+ return;
+ }
+
+ l_stream->m_read_fn = p_function;
+}
+
+void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if (!l_stream) {
+ return;
+ }
+ l_stream->m_seek_fn = p_function;
+}
+
+void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output))) {
+ return;
+ }
+
+ l_stream->m_write_fn = p_function;
+}
+
+void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if (! l_stream) {
+ return;
+ }
+
+ l_stream->m_skip_fn = p_function;
+}
+
+void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+ if (!l_stream)
+ return;
+ l_stream->m_user_data = p_data;
+}
+
+void OPJ_CALLCONV opj_stream_set_user_data_v3(opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+ if (!l_stream)
+ return;
+ l_stream->m_user_data = p_data;
+ l_stream->m_free_user_data_fn = p_function;
+}
+
+void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
+{
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+ if (!l_stream)
+ return;
+ l_stream->m_user_data_length = data_length;
+}
+
+OPJ_SIZE_T opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ OPJ_SIZE_T l_read_nb_bytes = 0;
+ if (p_stream->m_bytes_in_buffer >= p_size) {
+ memcpy(p_buffer,p_stream->m_current_data,p_size);
+ p_stream->m_current_data += p_size;
+ p_stream->m_bytes_in_buffer -= p_size;
+ l_read_nb_bytes += p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
+ return l_read_nb_bytes;
+ }
+
+ /* we are now in the case when the remaining data if not sufficient */
+ if (p_stream->m_status & opj_stream_e_end) {
+ l_read_nb_bytes += p_stream->m_bytes_in_buffer;
+ memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
+ p_stream->m_current_data += p_stream->m_bytes_in_buffer;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
+ }
+
+ /* the flag is not set, we copy data and then do an actual read on the stream */
+ if (p_stream->m_bytes_in_buffer) {
+ l_read_nb_bytes += p_stream->m_bytes_in_buffer;
+ memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_buffer += p_stream->m_bytes_in_buffer;
+ p_size -= p_stream->m_bytes_in_buffer;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ }
+ else {
+ /* case where we are already at the end of the buffer
+ so reset the m_current_data to point to the start of the
+ stored buffer to get ready to read from disk*/
+ p_stream->m_current_data = p_stream->m_stored_data;
+ }
+
+ while(1){
+ /* we should read less than a chunk -> read a chunk */
+ if (p_size < p_stream->m_buffer_size) {
+ /* we should do an actual read on the media */
+ p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_stream->m_stored_data,p_stream->m_buffer_size,p_stream->m_user_data);
+
+ if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
+ /* end of stream */
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+
+ p_stream->m_bytes_in_buffer = 0;
+ p_stream->m_status |= opj_stream_e_end;
+ /* end of stream */
+ return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
+ }
+ else if (p_stream->m_bytes_in_buffer < p_size) {
+ /* not enough data */
+ l_read_nb_bytes += p_stream->m_bytes_in_buffer;
+ memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_buffer += p_stream->m_bytes_in_buffer;
+ p_size -= p_stream->m_bytes_in_buffer;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ }
+ else {
+ l_read_nb_bytes += p_size;
+ memcpy(p_buffer,p_stream->m_current_data,p_size);
+ p_stream->m_current_data += p_size;
+ p_stream->m_bytes_in_buffer -= p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
+ return l_read_nb_bytes;
+ }
+ }
+ else {
+ /* direct read on the dest buffer */
+ p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer,p_size,p_stream->m_user_data);
+
+ if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
+ /* end of stream */
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+
+ p_stream->m_bytes_in_buffer = 0;
+ p_stream->m_status |= opj_stream_e_end;
+ /* end of stream */
+ return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
+ }
+ else if (p_stream->m_bytes_in_buffer < p_size) {
+ /* not enough data */
+ l_read_nb_bytes += p_stream->m_bytes_in_buffer;
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_buffer += p_stream->m_bytes_in_buffer;
+ p_size -= p_stream->m_bytes_in_buffer;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ }
+ else {
+ /* we have read the exact size */
+ l_read_nb_bytes += p_stream->m_bytes_in_buffer;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_stream->m_bytes_in_buffer = 0;
+ return l_read_nb_bytes;
+ }
+ }
+ }
+}
+
+OPJ_SIZE_T opj_stream_write_data (opj_stream_private_t * p_stream,
+ const OPJ_BYTE * p_buffer,
+ OPJ_SIZE_T p_size,
+ opj_event_mgr_t * p_event_mgr)
+{
+ OPJ_SIZE_T l_remaining_bytes = 0;
+ OPJ_SIZE_T l_write_nb_bytes = 0;
+
+ if (p_stream->m_status & opj_stream_e_error) {
+ return (OPJ_SIZE_T)-1;
+ }
+
+ while(1) {
+ l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
+
+ /* we have more memory than required */
+ if (l_remaining_bytes >= p_size) {
+ memcpy(p_stream->m_current_data, p_buffer, p_size);
+
+ p_stream->m_current_data += p_size;
+ p_stream->m_bytes_in_buffer += p_size;
+ l_write_nb_bytes += p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
+
+ return l_write_nb_bytes;
+ }
+
+ /* we copy data and then do an actual read on the stream */
+ if (l_remaining_bytes) {
+ l_write_nb_bytes += l_remaining_bytes;
+
+ memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
+
+ p_stream->m_current_data = p_stream->m_stored_data;
+
+ p_buffer += l_remaining_bytes;
+ p_size -= l_remaining_bytes;
+ p_stream->m_bytes_in_buffer += l_remaining_bytes;
+ p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
+ }
+
+ if (! opj_stream_flush(p_stream, p_event_mgr)) {
+ return (OPJ_SIZE_T)-1;
+ }
+ }
+
+}
+
+OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
+{
+ /* the number of bytes written on the media. */
+ OPJ_SIZE_T l_current_write_nb_bytes = 0;
+
+ p_stream->m_current_data = p_stream->m_stored_data;
+
+ while (p_stream->m_bytes_in_buffer) {
+ /* we should do an actual write on the media */
+ l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,
+ p_stream->m_bytes_in_buffer,
+ p_stream->m_user_data);
+
+ if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
+ p_stream->m_status |= opj_stream_e_error;
+ opj_event_msg(p_event_mgr, EVT_INFO, "Error on writing stream!\n");
+
+ return OPJ_FALSE;
+ }
+
+ p_stream->m_current_data += l_current_write_nb_bytes;
+ p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
+ }
+
+ p_stream->m_current_data = p_stream->m_stored_data;
+
+ return OPJ_TRUE;
+}
+
+OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ OPJ_OFF_T l_skip_nb_bytes = 0;
+ OPJ_OFF_T l_current_skip_nb_bytes = 0;
+
+ assert( p_size >= 0 );
+
+ if (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size) {
+ p_stream->m_current_data += p_size;
+ /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
+ which is of type OPJ_SIZE_T */
+ p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
+ l_skip_nb_bytes += p_size;
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+ return l_skip_nb_bytes;
+ }
+
+ /* we are now in the case when the remaining data if not sufficient */
+ if (p_stream->m_status & opj_stream_e_end) {
+ l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_current_data += p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+ return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
+ }
+
+ /* the flag is not set, we copy data and then do an actual skip on the stream */
+ if (p_stream->m_bytes_in_buffer) {
+ l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
+ p_stream->m_bytes_in_buffer = 0;
+ }
+
+ while (p_size > 0) {
+ /* we should do an actual skip on the media */
+ l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
+ if (l_current_skip_nb_bytes == (OPJ_OFF_T) -1) {
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+
+ p_stream->m_status |= opj_stream_e_end;
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+ /* end if stream */
+ return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
+ }
+ p_size -= l_current_skip_nb_bytes;
+ l_skip_nb_bytes += l_current_skip_nb_bytes;
+ }
+
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+
+ return l_skip_nb_bytes;
+}
+
+OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ OPJ_BOOL l_is_written = 0;
+ OPJ_OFF_T l_current_skip_nb_bytes = 0;
+ OPJ_OFF_T l_skip_nb_bytes = 0;
+
+ if (p_stream->m_status & opj_stream_e_error) {
+ return (OPJ_OFF_T) -1;
+ }
+
+ /* we should flush data */
+ l_is_written = opj_stream_flush (p_stream, p_event_mgr);
+ if (! l_is_written) {
+ p_stream->m_status |= opj_stream_e_error;
+ p_stream->m_bytes_in_buffer = 0;
+ return (OPJ_OFF_T) -1;
+ }
+ /* then skip */
+
+ while (p_size > 0) {
+ /* we should do an actual skip on the media */
+ l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
+
+ if (l_current_skip_nb_bytes == (OPJ_OFF_T)-1) {
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n");
+
+ p_stream->m_status |= opj_stream_e_error;
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+ /* end if stream */
+ return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
+ }
+ p_size -= l_current_skip_nb_bytes;
+ l_skip_nb_bytes += l_current_skip_nb_bytes;
+ }
+
+ p_stream->m_byte_offset += l_skip_nb_bytes;
+
+ return l_skip_nb_bytes;
+}
+
+OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
+{
+ return p_stream->m_byte_offset;
+}
+
+OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
+{
+ assert( p_stream->m_byte_offset >= 0 );
+ assert( p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset);
+ return p_stream->m_user_data_length ?
+ (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset :
+ 0;
+}
+
+OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ assert(p_size >= 0);
+ return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
+}
+
+OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ OPJ_ARG_NOT_USED(p_event_mgr);
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_stream->m_bytes_in_buffer = 0;
+
+ if( !(p_stream->m_seek_fn(p_size,p_stream->m_user_data)) ) {
+ p_stream->m_status |= opj_stream_e_end;
+ return OPJ_FALSE;
+ }
+ else {
+ /* reset stream status */
+ p_stream->m_status &= (~opj_stream_e_end);
+ p_stream->m_byte_offset = p_size;
+
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+{
+ if (! opj_stream_flush(p_stream,p_event_mgr)) {
+ p_stream->m_status |= opj_stream_e_error;
+ return OPJ_FALSE;
+ }
+
+ p_stream->m_current_data = p_stream->m_stored_data;
+ p_stream->m_bytes_in_buffer = 0;
+
+ if (! p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
+ p_stream->m_status |= opj_stream_e_error;
+ return OPJ_FALSE;
+ }
+ else {
+ p_stream->m_byte_offset = p_size;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
+{
+ assert(p_size >= 0);
+ return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
+}
+
+OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream)
+{
+ return p_stream->m_seek_fn != opj_stream_default_seek;
+}
+
+OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
+{
+ OPJ_ARG_NOT_USED(p_buffer);
+ OPJ_ARG_NOT_USED(p_nb_bytes);
+ OPJ_ARG_NOT_USED(p_user_data);
+ return (OPJ_SIZE_T) -1;
+}
+
+OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
+{
+ OPJ_ARG_NOT_USED(p_buffer);
+ OPJ_ARG_NOT_USED(p_nb_bytes);
+ OPJ_ARG_NOT_USED(p_user_data);
+ return (OPJ_SIZE_T) -1;
+}
+
+OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
+{
+ OPJ_ARG_NOT_USED(p_nb_bytes);
+ OPJ_ARG_NOT_USED(p_user_data);
+ return (OPJ_OFF_T) -1;
+}
+
+OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
+{
+ OPJ_ARG_NOT_USED(p_nb_bytes);
+ OPJ_ARG_NOT_USED(p_user_data);
+ return OPJ_FALSE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.h
new file mode 100644
index 0000000000..729d073ac2
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/cio.h
@@ -0,0 +1,399 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __CIO_H
+#define __CIO_H
+/**
+@file cio.h
+@brief Implementation of a byte input-output process (CIO)
+
+The functions in CIO.C have for goal to realize a byte input / output process.
+*/
+
+/** @defgroup CIO CIO - byte input-output stream */
+/*@{*/
+
+#include "opj_config.h"
+
+/* ----------------------------------------------------------------------- */
+
+#if defined(OPJ_BIG_ENDIAN)
+ #define opj_write_bytes opj_write_bytes_BE
+ #define opj_read_bytes opj_read_bytes_BE
+ #define opj_write_double opj_write_double_BE
+ #define opj_read_double opj_read_double_BE
+ #define opj_write_float opj_write_float_BE
+ #define opj_read_float opj_read_float_BE
+#else
+ #define opj_write_bytes opj_write_bytes_LE
+ #define opj_read_bytes opj_read_bytes_LE
+ #define opj_write_double opj_write_double_LE
+ #define opj_read_double opj_read_double_LE
+ #define opj_write_float opj_write_float_LE
+ #define opj_read_float opj_read_float_LE
+#endif
+
+
+
+typedef enum
+{
+ opj_signed_sentinel = -1, /* do not use in code */
+ opj_stream_e_output = 0x1,
+ opj_stream_e_input = 0x2,
+ opj_stream_e_end = 0x4,
+ opj_stream_e_error = 0x8
+}
+opj_stream_flag ;
+
+/**
+Byte input-output stream.
+*/
+typedef struct opj_stream_private
+{
+ /**
+ * User data, be it files, ... The actual data depends on the type of the stream.
+ */
+ void * m_user_data;
+
+ /**
+ * Pointer to function to free m_user_data (NULL at initialization)
+ * when destroying the stream. If pointer is NULL the function is not
+ * called and the m_user_data is not freed (even if non-NULL).
+ */
+ opj_stream_free_user_data_fn m_free_user_data_fn;
+
+ /**
+ * User data length
+ */
+ OPJ_UINT64 m_user_data_length;
+
+ /**
+ * Pointer to actual read function (NULL at the initialization of the cio.
+ */
+ opj_stream_read_fn m_read_fn;
+
+ /**
+ * Pointer to actual write function (NULL at the initialization of the cio.
+ */
+ opj_stream_write_fn m_write_fn;
+
+ /**
+ * Pointer to actual skip function (NULL at the initialization of the cio.
+ * There is no seek function to prevent from back and forth slow procedures.
+ */
+ opj_stream_skip_fn m_skip_fn;
+
+ /**
+ * Pointer to actual seek function (if available).
+ */
+ opj_stream_seek_fn m_seek_fn;
+
+ /**
+ * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
+ * you should never access this data directly.
+ */
+ OPJ_BYTE * m_stored_data;
+
+ /**
+ * Pointer to the current read data.
+ */
+ OPJ_BYTE * m_current_data;
+
+ /**
+ * FIXME DOC.
+ */
+ OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
+
+ /**
+ * FIXME DOC.
+ */
+ OPJ_BOOL (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
+
+ /**
+ * number of bytes containing in the buffer.
+ */
+ OPJ_SIZE_T m_bytes_in_buffer;
+
+ /**
+ * The number of bytes read/written from the beginning of the stream
+ */
+ OPJ_OFF_T m_byte_offset;
+
+ /**
+ * The size of the buffer.
+ */
+ OPJ_SIZE_T m_buffer_size;
+
+ /**
+ * Flags to tell the status of the stream.
+ */
+ opj_stream_flag m_status;
+
+}
+opj_stream_private_t;
+
+/** @name Exported functions (see also openjpeg.h) */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+ * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ * @param p_nb_bytes the number of bytes to write
+*/
+void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ * @param p_nb_bytes the nb bytes to read.
+ * @return the number of bytes read or -1 if an error occured.
+ */
+void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
+
+/**
+ * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ * @param p_nb_bytes the number of bytes to write
+ * @return the number of bytes written or -1 if an error occured
+*/
+void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ * @param p_nb_bytes the nb bytes to read.
+ * @return the number of bytes read or -1 if an error occured.
+ */
+void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
+
+
+/**
+ * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ */
+void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
+
+/***
+ * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ */
+void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ */
+void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ */
+void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ */
+void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
+
+/**
+ * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to read data from.
+ * @param p_value pointer to the value that will store the data.
+ */
+void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
+
+/**
+ * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ */
+void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
+
+/***
+ * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
+ * @param p_buffer pointer the data buffer to write data to.
+ * @param p_value the value to write
+ */
+void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
+
+/**
+ * Reads some bytes from the stream.
+ * @param p_stream the stream to read data from.
+ * @param p_buffer pointer to the data buffer that will receive the data.
+ * @param p_size number of bytes to read.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
+ */
+OPJ_SIZE_T opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Writes some bytes to the stream.
+ * @param p_stream the stream to write data to.
+ * @param p_buffer pointer to the data buffer holds the data to be writtent.
+ * @param p_size number of bytes to write.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes writtent, or -1 if an error occured.
+ */
+OPJ_SIZE_T opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Writes the content of the stream buffer to the stream.
+ * @param p_stream the stream to write data to.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return true if the data could be flushed, false else.
+ */
+OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Skips a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes skipped, or -1 if an error occured.
+ */
+OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Tells the byte offset on the stream (similar to ftell).
+ *
+ * @param p_stream the stream to get the information from.
+ *
+ * @return the current position o fthe stream.
+ */
+OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
+
+
+/**
+ * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
+ *
+ * @param p_stream the stream to get the information from.
+ *
+ * @return Number of bytes left before the end of the stream.
+ */
+OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
+
+/**
+ * Skips a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes skipped, or -1 if an error occured.
+ */
+OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Skips a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes skipped, or -1 if an error occured.
+ */
+OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Skips a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return OPJ_TRUE if success, or OPJ_FALSE if an error occured.
+ */
+OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Skips a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return the number of bytes skipped, or -1 if an error occured.
+ */
+OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Seeks a number of bytes from the stream.
+ * @param p_stream the stream to skip data from.
+ * @param p_size the number of bytes to skip.
+ * @param p_event_mgr the user event manager to be notified of special events.
+ * @return true if the stream is seekable.
+ */
+OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
+
+/**
+ * Tells if the given stream is seekable.
+ */
+OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream);
+
+/**
+ * FIXME DOC.
+ */
+OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
+
+/**
+ * FIXME DOC.
+ */
+OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
+
+/**
+ * FIXME DOC.
+ */
+OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
+
+/**
+ * FIXME DOC.
+ */
+OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+
+#endif /* __CIO_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.c
new file mode 100644
index 0000000000..e1f8a337d4
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.c
@@ -0,0 +1,925 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
+ * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef __SSE__
+#include <xmmintrin.h>
+#endif
+
+#include "opj_includes.h"
+
+/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
+/*@{*/
+
+#define OPJ_WS(i) v->mem[(i)*2]
+#define OPJ_WD(i) v->mem[(1+(i)*2)]
+
+/** @name Local data structures */
+/*@{*/
+
+typedef struct dwt_local {
+ OPJ_INT32* mem;
+ OPJ_INT32 dn;
+ OPJ_INT32 sn;
+ OPJ_INT32 cas;
+} opj_dwt_t;
+
+typedef union {
+ OPJ_FLOAT32 f[4];
+} opj_v4_t;
+
+typedef struct v4dwt_local {
+ opj_v4_t* wavelet ;
+ OPJ_INT32 dn ;
+ OPJ_INT32 sn ;
+ OPJ_INT32 cas ;
+} opj_v4dwt_t ;
+
+static const OPJ_FLOAT32 opj_dwt_alpha = 1.586134342f; /* 12994 */
+static const OPJ_FLOAT32 opj_dwt_beta = 0.052980118f; /* 434 */
+static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /* -7233 */
+static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /* -3633 */
+
+static const OPJ_FLOAT32 opj_K = 1.230174105f; /* 10078 */
+static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
+
+/*@}*/
+
+/**
+Virtual function type for wavelet transform in 1-D
+*/
+typedef void (*DWT1DFN)(opj_dwt_t* v);
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Forward lazy transform (horizontal)
+*/
+static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
+/**
+Forward lazy transform (vertical)
+*/
+static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
+/**
+Inverse lazy transform (horizontal)
+*/
+static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a);
+/**
+Inverse lazy transform (vertical)
+*/
+static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
+/**
+Forward 5-3 wavelet transform in 1-D
+*/
+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
+/**
+Inverse 5-3 wavelet transform in 1-D
+*/
+static void opj_dwt_decode_1(opj_dwt_t *v);
+static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
+/**
+Forward 9-7 wavelet transform in 1-D
+*/
+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
+/**
+Explicit calculation of the Quantization Stepsizes
+*/
+static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
+/**
+Inverse wavelet transform in 2-D.
+*/
+static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
+
+static OPJ_BOOL opj_dwt_encode_procedure( opj_tcd_tilecomp_t * tilec,
+ void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
+
+static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i);
+
+/* <summary> */
+/* Inverse 9-7 wavelet transform in 1-D. */
+/* </summary> */
+static void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt);
+
+static void opj_v4dwt_interleave_h(opj_v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
+
+static void opj_v4dwt_interleave_v(opj_v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
+
+#ifdef __SSE__
+static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c);
+
+static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
+
+#else
+static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
+
+static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
+
+#endif
+
+/*@}*/
+
+/*@}*/
+
+#define OPJ_S(i) a[(i)*2]
+#define OPJ_D(i) a[(1+(i)*2)]
+#define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
+#define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
+/* new */
+#define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
+#define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
+
+/* <summary> */
+/* This table contains the norms of the 5-3 wavelets for different bands. */
+/* </summary> */
+static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
+ {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
+ {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
+ {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
+ {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
+};
+
+/* <summary> */
+/* This table contains the norms of the 9-7 wavelets for different bands. */
+/* </summary> */
+static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
+ {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
+ {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
+ {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
+ {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
+};
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+/* <summary> */
+/* Forward lazy transform (horizontal). */
+/* </summary> */
+void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+ OPJ_INT32 i;
+ OPJ_INT32 * l_dest = b;
+ OPJ_INT32 * l_src = a+cas;
+
+ for (i=0; i<sn; ++i) {
+ *l_dest++ = *l_src;
+ l_src += 2;
+ }
+
+ l_dest = b + sn;
+ l_src = a + 1 - cas;
+
+ for (i=0; i<dn; ++i) {
+ *l_dest++=*l_src;
+ l_src += 2;
+ }
+}
+
+/* <summary> */
+/* Forward lazy transform (vertical). */
+/* </summary> */
+void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
+ OPJ_INT32 i = sn;
+ OPJ_INT32 * l_dest = b;
+ OPJ_INT32 * l_src = a+cas;
+
+ while (i--) {
+ *l_dest = *l_src;
+ l_dest += x;
+ l_src += 2;
+ } /* b[i*x]=a[2*i+cas]; */
+
+ l_dest = b + sn * x;
+ l_src = a + 1 - cas;
+
+ i = dn;
+ while (i--) {
+ *l_dest = *l_src;
+ l_dest += x;
+ l_src += 2;
+ } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
+}
+
+/* <summary> */
+/* Inverse lazy transform (horizontal). */
+/* </summary> */
+void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a) {
+ OPJ_INT32 *ai = a;
+ OPJ_INT32 *bi = h->mem + h->cas;
+ OPJ_INT32 i = h->sn;
+ while( i-- ) {
+ *bi = *(ai++);
+ bi += 2;
+ }
+ ai = a + h->sn;
+ bi = h->mem + 1 - h->cas;
+ i = h->dn ;
+ while( i-- ) {
+ *bi = *(ai++);
+ bi += 2;
+ }
+}
+
+/* <summary> */
+/* Inverse lazy transform (vertical). */
+/* </summary> */
+void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
+ OPJ_INT32 *ai = a;
+ OPJ_INT32 *bi = v->mem + v->cas;
+ OPJ_INT32 i = v->sn;
+ while( i-- ) {
+ *bi = *ai;
+ bi += 2;
+ ai += x;
+ }
+ ai = a + (v->sn * x);
+ bi = v->mem + 1 - v->cas;
+ i = v->dn ;
+ while( i-- ) {
+ *bi = *ai;
+ bi += 2;
+ ai += x;
+ }
+}
+
+
+/* <summary> */
+/* Forward 5-3 wavelet transform in 1-D. */
+/* </summary> */
+void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+ OPJ_INT32 i;
+
+ if (!cas) {
+ if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
+ for (i = 0; i < dn; i++) OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
+ for (i = 0; i < sn; i++) OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
+ }
+ } else {
+ if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
+ OPJ_S(0) *= 2;
+ else {
+ for (i = 0; i < dn; i++) OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
+ for (i = 0; i < sn; i++) OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
+ }
+ }
+}
+
+/* <summary> */
+/* Inverse 5-3 wavelet transform in 1-D. */
+/* </summary> */
+void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+ OPJ_INT32 i;
+
+ if (!cas) {
+ if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
+ for (i = 0; i < sn; i++) OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
+ for (i = 0; i < dn; i++) OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
+ }
+ } else {
+ if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
+ OPJ_S(0) /= 2;
+ else {
+ for (i = 0; i < sn; i++) OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
+ for (i = 0; i < dn; i++) OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
+ }
+ }
+}
+
+/* <summary> */
+/* Inverse 5-3 wavelet transform in 1-D. */
+/* </summary> */
+void opj_dwt_decode_1(opj_dwt_t *v) {
+ opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
+}
+
+/* <summary> */
+/* Forward 9-7 wavelet transform in 1-D. */
+/* </summary> */
+void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+ OPJ_INT32 i;
+ if (!cas) {
+ if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
+ for (i = 0; i < dn; i++)
+ OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
+ for (i = 0; i < sn; i++)
+ OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
+ for (i = 0; i < dn; i++)
+ OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
+ for (i = 0; i < sn; i++)
+ OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
+ for (i = 0; i < dn; i++)
+ OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038); /*5038 */
+ for (i = 0; i < sn; i++)
+ OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659); /*6660 */
+ }
+ } else {
+ if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
+ for (i = 0; i < dn; i++)
+ OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
+ for (i = 0; i < sn; i++)
+ OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
+ for (i = 0; i < dn; i++)
+ OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
+ for (i = 0; i < sn; i++)
+ OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
+ for (i = 0; i < dn; i++)
+ OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038); /*5038 */
+ for (i = 0; i < sn; i++)
+ OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659); /*6660 */
+ }
+ }
+}
+
+void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
+ OPJ_INT32 p, n;
+ p = opj_int_floorlog2(stepsize) - 13;
+ n = 11 - opj_int_floorlog2(stepsize);
+ bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
+ bandno_stepsize->expn = numbps - p;
+}
+
+/*
+==========================================================
+ DWT interface
+==========================================================
+*/
+
+
+/* <summary> */
+/* Forward 5-3 wavelet transform in 2-D. */
+/* </summary> */
+INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
+{
+ OPJ_INT32 i, j, k;
+ OPJ_INT32 *a = 00;
+ OPJ_INT32 *aj = 00;
+ OPJ_INT32 *bj = 00;
+ OPJ_INT32 w, l;
+
+ OPJ_INT32 rw; /* width of the resolution level computed */
+ OPJ_INT32 rh; /* height of the resolution level computed */
+ OPJ_UINT32 l_data_size;
+
+ opj_tcd_resolution_t * l_cur_res = 0;
+ opj_tcd_resolution_t * l_last_res = 0;
+
+ w = tilec->x1-tilec->x0;
+ l = (OPJ_INT32)tilec->numresolutions-1;
+ a = tilec->data;
+
+ l_cur_res = tilec->resolutions + l;
+ l_last_res = l_cur_res - 1;
+
+ l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * (OPJ_UINT32)sizeof(OPJ_INT32);
+ bj = (OPJ_INT32*)opj_malloc((size_t)l_data_size);
+ if (! bj) {
+ return OPJ_FALSE;
+ }
+ i = l;
+
+ while (i--) {
+ OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
+ OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
+ OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+ OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
+ OPJ_INT32 dn, sn;
+
+ rw = l_cur_res->x1 - l_cur_res->x0;
+ rh = l_cur_res->y1 - l_cur_res->y0;
+ rw1 = l_last_res->x1 - l_last_res->x0;
+ rh1 = l_last_res->y1 - l_last_res->y0;
+
+ cas_row = l_cur_res->x0 & 1;
+ cas_col = l_cur_res->y0 & 1;
+
+ sn = rh1;
+ dn = rh - rh1;
+ for (j = 0; j < rw; ++j) {
+ aj = a + j;
+ for (k = 0; k < rh; ++k) {
+ bj[k] = aj[k*w];
+ }
+
+ (*p_function) (bj, dn, sn, cas_col);
+
+ opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
+ }
+
+ sn = rw1;
+ dn = rw - rw1;
+
+ for (j = 0; j < rh; j++) {
+ aj = a + j * w;
+ for (k = 0; k < rw; k++) bj[k] = aj[k];
+ (*p_function) (bj, dn, sn, cas_row);
+ opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
+ }
+
+ l_cur_res = l_last_res;
+
+ --l_last_res;
+ }
+
+ opj_free(bj);
+ return OPJ_TRUE;
+}
+
+/* Forward 5-3 wavelet transform in 2-D. */
+/* </summary> */
+OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
+{
+ return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
+}
+
+/* <summary> */
+/* Inverse 5-3 wavelet transform in 2-D. */
+/* </summary> */
+OPJ_BOOL opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
+ return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
+}
+
+
+/* <summary> */
+/* Get gain of 5-3 wavelet transform. */
+/* </summary> */
+OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
+ if (orient == 0)
+ return 0;
+ if (orient == 1 || orient == 2)
+ return 1;
+ return 2;
+}
+
+/* <summary> */
+/* Get norm of 5-3 wavelet. */
+/* </summary> */
+OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
+ return opj_dwt_norms[orient][level];
+}
+
+/* <summary> */
+/* Forward 9-7 wavelet transform in 2-D. */
+/* </summary> */
+OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
+{
+ return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
+}
+
+/* <summary> */
+/* Get gain of 9-7 wavelet transform. */
+/* </summary> */
+OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
+ (void)orient;
+ return 0;
+}
+
+/* <summary> */
+/* Get norm of 9-7 wavelet. */
+/* </summary> */
+OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
+ return opj_dwt_norms_real[orient][level];
+}
+
+void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
+ OPJ_UINT32 numbands, bandno;
+ numbands = 3 * tccp->numresolutions - 2;
+ for (bandno = 0; bandno < numbands; bandno++) {
+ OPJ_FLOAT64 stepsize;
+ OPJ_UINT32 resno, level, orient, gain;
+
+ resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
+ orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
+ level = tccp->numresolutions - 1 - resno;
+ gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
+ if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
+ stepsize = 1.0;
+ } else {
+ OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
+ stepsize = (1 << (gain)) / norm;
+ }
+ opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
+ }
+}
+
+/* <summary> */
+/* Determine maximum computed resolution level for inverse wavelet transform */
+/* </summary> */
+OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
+ OPJ_UINT32 mr = 0;
+ OPJ_UINT32 w;
+ while( --i ) {
+ ++r;
+ if( mr < ( w = (OPJ_UINT32)(r->x1 - r->x0) ) )
+ mr = w ;
+ if( mr < ( w = (OPJ_UINT32)(r->y1 - r->y0) ) )
+ mr = w ;
+ }
+ return mr ;
+}
+
+/* <summary> */
+/* Inverse wavelet transform in 2-D. */
+/* </summary> */
+OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
+ opj_dwt_t h;
+ opj_dwt_t v;
+
+ opj_tcd_resolution_t* tr = tilec->resolutions;
+
+ OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - tr->x0); /* width of the resolution level computed */
+ OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - tr->y0); /* height of the resolution level computed */
+
+ OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
+
+ h.mem = (OPJ_INT32*)
+ opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
+ if (! h.mem){
+ return OPJ_FALSE;
+ }
+
+ v.mem = h.mem;
+
+ while( --numres) {
+ OPJ_INT32 * restrict tiledp = tilec->data;
+ OPJ_UINT32 j;
+
+ ++tr;
+ h.sn = (OPJ_INT32)rw;
+ v.sn = (OPJ_INT32)rh;
+
+ rw = (OPJ_UINT32)(tr->x1 - tr->x0);
+ rh = (OPJ_UINT32)(tr->y1 - tr->y0);
+
+ h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
+ h.cas = tr->x0 % 2;
+
+ for(j = 0; j < rh; ++j) {
+ opj_dwt_interleave_h(&h, &tiledp[j*w]);
+ (dwt_1D)(&h);
+ memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
+ }
+
+ v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
+ v.cas = tr->y0 % 2;
+
+ for(j = 0; j < rw; ++j){
+ OPJ_UINT32 k;
+ opj_dwt_interleave_v(&v, &tiledp[j], (OPJ_INT32)w);
+ (dwt_1D)(&v);
+ for(k = 0; k < rh; ++k) {
+ tiledp[k * w + j] = v.mem[k];
+ }
+ }
+ }
+ opj_aligned_free(h.mem);
+ return OPJ_TRUE;
+}
+
+void opj_v4dwt_interleave_h(opj_v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
+ OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
+ OPJ_INT32 count = w->sn;
+ OPJ_INT32 i, k;
+
+ for(k = 0; k < 2; ++k){
+ if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
+ /* Fast code path */
+ for(i = 0; i < count; ++i){
+ OPJ_INT32 j = i;
+ bi[i*8 ] = a[j];
+ j += x;
+ bi[i*8 + 1] = a[j];
+ j += x;
+ bi[i*8 + 2] = a[j];
+ j += x;
+ bi[i*8 + 3] = a[j];
+ }
+ }
+ else {
+ /* Slow code path */
+ for(i = 0; i < count; ++i){
+ OPJ_INT32 j = i;
+ bi[i*8 ] = a[j];
+ j += x;
+ if(j >= size) continue;
+ bi[i*8 + 1] = a[j];
+ j += x;
+ if(j >= size) continue;
+ bi[i*8 + 2] = a[j];
+ j += x;
+ if(j >= size) continue;
+ bi[i*8 + 3] = a[j]; /* This one*/
+ }
+ }
+
+ bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas);
+ a += w->sn;
+ size -= w->sn;
+ count = w->dn;
+ }
+}
+
+void opj_v4dwt_interleave_v(opj_v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
+ opj_v4_t* restrict bi = v->wavelet + v->cas;
+ OPJ_INT32 i;
+
+ for(i = 0; i < v->sn; ++i){
+ memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
+ }
+
+ a += v->sn * x;
+ bi = v->wavelet + 1 - v->cas;
+
+ for(i = 0; i < v->dn; ++i){
+ memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
+ }
+}
+
+#ifdef __SSE__
+
+void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c){
+ __m128* restrict vw = (__m128*) w;
+ OPJ_INT32 i;
+ /* 4x unrolled loop */
+ for(i = 0; i < count >> 2; ++i){
+ *vw = _mm_mul_ps(*vw, c);
+ vw += 2;
+ *vw = _mm_mul_ps(*vw, c);
+ vw += 2;
+ *vw = _mm_mul_ps(*vw, c);
+ vw += 2;
+ *vw = _mm_mul_ps(*vw, c);
+ vw += 2;
+ }
+ count &= 3;
+ for(i = 0; i < count; ++i){
+ *vw = _mm_mul_ps(*vw, c);
+ vw += 2;
+ }
+}
+
+void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){
+ __m128* restrict vl = (__m128*) l;
+ __m128* restrict vw = (__m128*) w;
+ OPJ_INT32 i;
+ __m128 tmp1, tmp2, tmp3;
+ tmp1 = vl[0];
+ for(i = 0; i < m; ++i){
+ tmp2 = vw[-1];
+ tmp3 = vw[ 0];
+ vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
+ tmp1 = tmp3;
+ vw += 2;
+ }
+ vl = vw - 2;
+ if(m >= k){
+ return;
+ }
+ c = _mm_add_ps(c, c);
+ c = _mm_mul_ps(c, vl[0]);
+ for(; m < k; ++m){
+ __m128 tmp = vw[-1];
+ vw[-1] = _mm_add_ps(tmp, c);
+ vw += 2;
+ }
+}
+
+#else
+
+void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
+{
+ OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
+ OPJ_INT32 i;
+ for(i = 0; i < count; ++i){
+ OPJ_FLOAT32 tmp1 = fw[i*8 ];
+ OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
+ OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
+ OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
+ fw[i*8 ] = tmp1 * c;
+ fw[i*8 + 1] = tmp2 * c;
+ fw[i*8 + 2] = tmp3 * c;
+ fw[i*8 + 3] = tmp4 * c;
+ }
+}
+
+void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
+{
+ OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
+ OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
+ OPJ_INT32 i;
+ for(i = 0; i < m; ++i){
+ OPJ_FLOAT32 tmp1_1 = fl[0];
+ OPJ_FLOAT32 tmp1_2 = fl[1];
+ OPJ_FLOAT32 tmp1_3 = fl[2];
+ OPJ_FLOAT32 tmp1_4 = fl[3];
+ OPJ_FLOAT32 tmp2_1 = fw[-4];
+ OPJ_FLOAT32 tmp2_2 = fw[-3];
+ OPJ_FLOAT32 tmp2_3 = fw[-2];
+ OPJ_FLOAT32 tmp2_4 = fw[-1];
+ OPJ_FLOAT32 tmp3_1 = fw[0];
+ OPJ_FLOAT32 tmp3_2 = fw[1];
+ OPJ_FLOAT32 tmp3_3 = fw[2];
+ OPJ_FLOAT32 tmp3_4 = fw[3];
+ fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
+ fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
+ fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
+ fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
+ fl = fw;
+ fw += 8;
+ }
+ if(m < k){
+ OPJ_FLOAT32 c1;
+ OPJ_FLOAT32 c2;
+ OPJ_FLOAT32 c3;
+ OPJ_FLOAT32 c4;
+ c += c;
+ c1 = fl[0] * c;
+ c2 = fl[1] * c;
+ c3 = fl[2] * c;
+ c4 = fl[3] * c;
+ for(; m < k; ++m){
+ OPJ_FLOAT32 tmp1 = fw[-4];
+ OPJ_FLOAT32 tmp2 = fw[-3];
+ OPJ_FLOAT32 tmp3 = fw[-2];
+ OPJ_FLOAT32 tmp4 = fw[-1];
+ fw[-4] = tmp1 + c1;
+ fw[-3] = tmp2 + c2;
+ fw[-2] = tmp3 + c3;
+ fw[-1] = tmp4 + c4;
+ fw += 8;
+ }
+ }
+}
+
+#endif
+
+/* <summary> */
+/* Inverse 9-7 wavelet transform in 1-D. */
+/* </summary> */
+void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt)
+{
+ OPJ_INT32 a, b;
+ if(dwt->cas == 0) {
+ if(!((dwt->dn > 0) || (dwt->sn > 1))){
+ return;
+ }
+ a = 0;
+ b = 1;
+ }else{
+ if(!((dwt->sn > 0) || (dwt->dn > 1))) {
+ return;
+ }
+ a = 1;
+ b = 0;
+ }
+#ifdef __SSE__
+ opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
+ opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
+ opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_delta));
+ opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_gamma));
+ opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_beta));
+ opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_alpha));
+#else
+ opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
+ opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
+ opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
+ opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
+ opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
+ opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
+#endif
+}
+
+
+/* <summary> */
+/* Inverse 9-7 wavelet transform in 2-D. */
+/* </summary> */
+OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres)
+{
+ opj_v4dwt_t h;
+ opj_v4dwt_t v;
+
+ opj_tcd_resolution_t* res = tilec->resolutions;
+
+ OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
+ OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
+
+ OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
+
+ h.wavelet = (opj_v4_t*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t));
+ v.wavelet = h.wavelet;
+
+ while( --numres) {
+ OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
+ OPJ_UINT32 bufsize = (OPJ_UINT32)((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
+ OPJ_INT32 j;
+
+ h.sn = (OPJ_INT32)rw;
+ v.sn = (OPJ_INT32)rh;
+
+ ++res;
+
+ rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
+ rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
+
+ h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
+ h.cas = res->x0 % 2;
+
+ for(j = (OPJ_INT32)rh; j > 3; j -= 4) {
+ OPJ_INT32 k;
+ opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
+ opj_v4dwt_decode(&h);
+
+ for(k = (OPJ_INT32)rw; --k >= 0;){
+ aj[k ] = h.wavelet[k].f[0];
+ aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
+ aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
+ aj[k+(OPJ_INT32)w*3] = h.wavelet[k].f[3];
+ }
+
+ aj += w*4;
+ bufsize -= w*4;
+ }
+
+ if (rh & 0x03) {
+ OPJ_INT32 k;
+ j = rh & 0x03;
+ opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
+ opj_v4dwt_decode(&h);
+ for(k = (OPJ_INT32)rw; --k >= 0;){
+ switch(j) {
+ case 3: aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
+ case 2: aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
+ case 1: aj[k ] = h.wavelet[k].f[0];
+ }
+ }
+ }
+
+ v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
+ v.cas = res->y0 % 2;
+
+ aj = (OPJ_FLOAT32*) tilec->data;
+ for(j = (OPJ_INT32)rw; j > 3; j -= 4){
+ OPJ_UINT32 k;
+
+ opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, 4);
+ opj_v4dwt_decode(&v);
+
+ for(k = 0; k < rh; ++k){
+ memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
+ }
+ aj += 4;
+ }
+
+ if (rw & 0x03){
+ OPJ_UINT32 k;
+
+ j = rw & 0x03;
+
+ opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, j);
+ opj_v4dwt_decode(&v);
+
+ for(k = 0; k < rh; ++k){
+ memcpy(&aj[k*w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
+ }
+ }
+ }
+
+ opj_aligned_free(h.wavelet);
+ return OPJ_TRUE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.h
new file mode 100644
index 0000000000..f8b57bc0c7
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/dwt.h
@@ -0,0 +1,122 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __DWT_H
+#define __DWT_H
+/**
+@file dwt.h
+@brief Implementation of a discrete wavelet transform (DWT)
+
+The functions in DWT.C have for goal to realize forward and inverse discret wavelet
+transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in
+DWT.C are used by some function in TCD.C.
+*/
+
+/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
+/*@{*/
+
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Forward 5-3 wavelet tranform in 2-D.
+Apply a reversible DWT transform to a component of an image.
+@param tilec Tile component information (current tile)
+*/
+OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec);
+
+/**
+Inverse 5-3 wavelet tranform in 2-D.
+Apply a reversible inverse DWT transform to a component of an image.
+@param tilec Tile component information (current tile)
+@param numres Number of resolution levels to decode
+*/
+OPJ_BOOL opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres);
+
+/**
+Get the gain of a subband for the reversible 5-3 DWT.
+@param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
+@return Returns 0 if orient = 0, returns 1 if orient = 1 or 2, returns 2 otherwise
+*/
+OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) ;
+/**
+Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT.
+@param level Level of the wavelet function
+@param orient Band of the wavelet function
+@return Returns the norm of the wavelet function
+*/
+OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient);
+/**
+Forward 9-7 wavelet transform in 2-D.
+Apply an irreversible DWT transform to a component of an image.
+@param tilec Tile component information (current tile)
+*/
+OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec);
+/**
+Inverse 9-7 wavelet transform in 2-D.
+Apply an irreversible inverse DWT transform to a component of an image.
+@param tilec Tile component information (current tile)
+@param numres Number of resolution levels to decode
+*/
+OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres);
+
+/**
+Get the gain of a subband for the irreversible 9-7 DWT.
+@param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
+@return Returns the gain of the 9-7 wavelet transform
+*/
+OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient);
+/**
+Get the norm of a wavelet function of a subband at a specified level for the irreversible 9-7 DWT
+@param level Level of the wavelet function
+@param orient Band of the wavelet function
+@return Returns the norm of the 9-7 wavelet
+*/
+OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient);
+/**
+Explicit calculation of the Quantization Stepsizes
+@param tccp Tile-component coding parameters
+@param prec Precint analyzed
+*/
+void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __DWT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.c
new file mode 100644
index 0000000000..b6034b42d0
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.c
@@ -0,0 +1,146 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ==========================================================
+ Utility functions
+ ==========================================================*/
+
+#ifdef OPJ_CODE_NOT_USED
+#ifndef _WIN32
+static char*
+i2a(unsigned i, char *a, unsigned r) {
+ if (i/r > 0) a = i2a(i/r,a,r);
+ *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
+ return a+1;
+}
+
+/**
+ Transforms integer i into an ascii string and stores the result in a;
+ string is encoded in the base indicated by r.
+ @param i Number to be converted
+ @param a String result
+ @param r Base of value; must be in the range 2 - 36
+ @return Returns a
+*/
+static char *
+_itoa(int i, char *a, int r) {
+ r = ((r < 2) || (r > 36)) ? 10 : r;
+ if(i < 0) {
+ *a = '-';
+ *i2a(-i, a+1, r) = 0;
+ }
+ else *i2a(i, a, r) = 0;
+ return a;
+}
+
+#endif /* !_WIN32 */
+#endif
+
+/* ----------------------------------------------------------------------- */
+/**
+ * Default callback function.
+ * Do nothing.
+ */
+static void opj_default_callback (const char *msg, void *client_data)
+{
+ OPJ_ARG_NOT_USED(msg);
+ OPJ_ARG_NOT_USED(client_data);
+}
+
+/* ----------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+OPJ_BOOL opj_event_msg(opj_event_mgr_t* p_event_mgr, OPJ_INT32 event_type, const char *fmt, ...) {
+#define OPJ_MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
+ opj_msg_callback msg_handler = 00;
+ void * l_data = 00;
+
+ if(p_event_mgr != 00) {
+ switch(event_type) {
+ case EVT_ERROR:
+ msg_handler = p_event_mgr->error_handler;
+ l_data = p_event_mgr->m_error_data;
+ break;
+ case EVT_WARNING:
+ msg_handler = p_event_mgr->warning_handler;
+ l_data = p_event_mgr->m_warning_data;
+ break;
+ case EVT_INFO:
+ msg_handler = p_event_mgr->info_handler;
+ l_data = p_event_mgr->m_info_data;
+ break;
+ default:
+ break;
+ }
+ if(msg_handler == 00) {
+ return OPJ_FALSE;
+ }
+ } else {
+ return OPJ_FALSE;
+ }
+
+ if ((fmt != 00) && (p_event_mgr != 00)) {
+ va_list arg;
+ size_t str_length/*, i, j*/; /* UniPG */
+ char message[OPJ_MSG_SIZE];
+ memset(message, 0, OPJ_MSG_SIZE);
+ /* initialize the optional parameter list */
+ va_start(arg, fmt);
+ /* check the length of the format string */
+ str_length = (strlen(fmt) > OPJ_MSG_SIZE) ? OPJ_MSG_SIZE : strlen(fmt);
+ (void)str_length;
+ /* parse the format string and put the result in 'message' */
+ vsnprintf(message, OPJ_MSG_SIZE, fmt, arg); /* UniPG */
+ /* deinitialize the optional parameter list */
+ va_end(arg);
+
+ /* output the message to the user program */
+ msg_handler(message, l_data);
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_set_default_event_handler(opj_event_mgr_t * p_manager)
+{
+ p_manager->m_error_data = 00;
+ p_manager->m_warning_data = 00;
+ p_manager->m_info_data = 00;
+ p_manager->error_handler = opj_default_callback;
+ p_manager->info_handler = opj_default_callback;
+ p_manager->warning_handler = opj_default_callback;
+}
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.h
new file mode 100644
index 0000000000..88e0395b4f
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/event.h
@@ -0,0 +1,102 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __EVENT_H
+#define __EVENT_H
+/**
+@file event.h
+@brief Implementation of a event callback system
+
+The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user.
+*/
+/**
+Message handler object
+used for
+<ul>
+<li>Error messages
+<li>Warning messages
+<li>Debugging messages
+</ul>
+*/
+typedef struct opj_event_mgr
+{
+ /** Data to call the event manager upon */
+ void * m_error_data;
+ /** Data to call the event manager upon */
+ void * m_warning_data;
+ /** Data to call the event manager upon */
+ void * m_info_data;
+ /** Error message callback if available, NULL otherwise */
+ opj_msg_callback error_handler;
+ /** Warning message callback if available, NULL otherwise */
+ opj_msg_callback warning_handler;
+ /** Debug message callback if available, NULL otherwise */
+ opj_msg_callback info_handler;
+} opj_event_mgr_t;
+
+
+#define EVT_ERROR 1 /**< Error event type */
+#define EVT_WARNING 2 /**< Warning event type */
+#define EVT_INFO 4 /**< Debug event type */
+
+/** @defgroup EVENT EVENT - Implementation of a event callback system */
+/*@{*/
+
+/** @name Exported functions (see also openjpeg.h) */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Write formatted data to a string and send the string to a user callback.
+ *
+ * @param event_mgr Event handler
+ * @param event_type Event type or callback to use to send the message
+ * @param fmt Format-control string (plus optional arguments)
+ *
+ * @return Returns true if successful, returns false otherwise
+ */
+OPJ_BOOL opj_event_msg(opj_event_mgr_t* event_mgr, OPJ_INT32 event_type, const char *fmt, ...);
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Set the event manager with the default callback function for the 3 levels.
+ */
+void opj_set_default_event_handler(opj_event_mgr_t * p_manager);
+
+/*@}*/
+
+/*@}*/
+
+#endif /* __EVENT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.c
new file mode 100644
index 0000000000..4c8aae621e
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.c
@@ -0,0 +1,119 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/**
+ * Default size of the validation list, if not sufficient, data will be reallocated with a double size.
+ */
+#define OPJ_VALIDATION_SIZE 10
+
+opj_procedure_list_t * opj_procedure_list_create()
+{
+ /* memory allocation */
+ opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_malloc(sizeof(opj_procedure_list_t));
+ if (! l_validation)
+ {
+ return 00;
+ }
+ /* initialization */
+ memset(l_validation,0,sizeof(opj_procedure_list_t));
+ l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE;
+ l_validation->m_procedures = (opj_procedure*)opj_malloc(
+ OPJ_VALIDATION_SIZE * sizeof(opj_procedure));
+ if (! l_validation->m_procedures)
+ {
+ opj_free(l_validation);
+ return 00;
+ }
+ memset(l_validation->m_procedures,0,OPJ_VALIDATION_SIZE * sizeof(opj_procedure));
+ return l_validation;
+}
+
+void opj_procedure_list_destroy(opj_procedure_list_t * p_list)
+{
+ if (! p_list)
+ {
+ return;
+ }
+ /* initialization */
+ if (p_list->m_procedures)
+ {
+ opj_free(p_list->m_procedures);
+ }
+ opj_free(p_list);
+}
+
+OPJ_BOOL opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure)
+{
+ if (p_validation_list->m_nb_max_procedures == p_validation_list->m_nb_procedures)
+ {
+ opj_procedure * new_procedures;
+
+ p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE;
+ new_procedures = (opj_procedure*)opj_realloc(
+ p_validation_list->m_procedures,
+ p_validation_list->m_nb_max_procedures * sizeof(opj_procedure));
+ if (! new_procedures)
+ {
+ opj_free(p_validation_list->m_procedures);
+ p_validation_list->m_nb_max_procedures = 0;
+ p_validation_list->m_nb_procedures = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add a new validation procedure\n"); */
+ fprintf(stderr, "Not enough memory to add a new validation procedure\n");
+
+ return OPJ_FALSE;
+ }
+ else
+ {
+ p_validation_list->m_procedures = new_procedures;
+ }
+ }
+ p_validation_list->m_procedures[p_validation_list->m_nb_procedures] = p_procedure;
+ ++p_validation_list->m_nb_procedures;
+
+ return OPJ_TRUE;
+}
+
+OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_validation_list)
+{
+ return p_validation_list->m_nb_procedures;
+}
+
+opj_procedure* opj_procedure_list_get_first_procedure (opj_procedure_list_t * p_validation_list)
+{
+ return p_validation_list->m_procedures;
+}
+
+void opj_procedure_list_clear (opj_procedure_list_t * p_validation_list)
+{
+ p_validation_list->m_nb_procedures = 0;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.h
new file mode 100644
index 0000000000..749ad9e457
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/function_list.h
@@ -0,0 +1,131 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __FUNCTION_LIST_H
+#define __FUNCTION_LIST_H
+
+/**
+ * @file function_list.h
+ * @brief Implementation of a list of procedures.
+
+ * The functions in validation.c aims to have access to a list of procedures.
+*/
+
+/** @defgroup VAL VAL - validation procedure*/
+/*@{*/
+
+/**************************************************************************************************
+ ***************************************** FORWARD DECLARATION ************************************
+ **************************************************************************************************/
+
+/**
+ * declare a function pointer
+ */
+typedef void (*opj_procedure)(void);
+
+/**
+ * A list of procedures.
+*/
+typedef struct opj_procedure_list
+{
+ /**
+ * The number of validation procedures.
+ */
+ OPJ_UINT32 m_nb_procedures;
+ /**
+ * The number of the array of validation procedures.
+ */
+ OPJ_UINT32 m_nb_max_procedures;
+ /**
+ * The array of procedures.
+ */
+ opj_procedure * m_procedures;
+
+} opj_procedure_list_t;
+
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Creates a validation list.
+ *
+ * @return the newly created validation list.
+ */
+opj_procedure_list_t * opj_procedure_list_create(void);
+
+/**
+ * Destroys a validation list.
+ *
+ * @param p_list the list to destroy.
+ */
+void opj_procedure_list_destroy(opj_procedure_list_t * p_list);
+
+/**
+ * Adds a new validation procedure.
+ *
+ * @param p_validation_list the list of procedure to modify.
+ * @param p_procedure the procedure to add.
+ *
+ * @return OPJ_TRUE if the procedure could be added.
+ */
+OPJ_BOOL opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure);
+
+/**
+ * Gets the number of validation procedures.
+ *
+ * @param p_validation_list the list of procedure to modify.
+ *
+ * @return the number of validation procedures.
+ */
+OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_validation_list);
+
+/**
+ * Gets the pointer on the first validation procedure. This function is similar to the C++
+ * iterator class to iterate through all the procedures inside the validation list.
+ * the caller does not take ownership of the pointer.
+ *
+ * @param p_validation_list the list of procedure to get the first procedure from.
+ *
+ * @return a pointer to the first procedure.
+ */
+opj_procedure* opj_procedure_list_get_first_procedure (opj_procedure_list_t * p_validation_list);
+
+
+/**
+ * Clears the list of validation procedures.
+ *
+ * @param p_validation_list the list of procedure to clear.
+ *
+ */
+void opj_procedure_list_clear (opj_procedure_list_t * p_validation_list);
+/*@}*/
+
+#endif /* __FUNCTION_LIST_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.c
new file mode 100644
index 0000000000..2c3540c517
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.c
@@ -0,0 +1,240 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+opj_image_t* opj_image_create0(void) {
+ opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
+ return image;
+}
+
+opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
+ OPJ_UINT32 compno;
+ opj_image_t *image = NULL;
+
+ image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
+ if(image) {
+ image->color_space = clrspc;
+ image->numcomps = numcmpts;
+ /* allocate memory for the per-component information */
+ image->comps = (opj_image_comp_t*)opj_calloc(1,image->numcomps * sizeof(opj_image_comp_t));
+ if(!image->comps) {
+ fprintf(stderr,"Unable to allocate memory for image.\n");
+ opj_image_destroy(image);
+ return NULL;
+ }
+ /* create the individual image components */
+ for(compno = 0; compno < numcmpts; compno++) {
+ opj_image_comp_t *comp = &image->comps[compno];
+ comp->dx = cmptparms[compno].dx;
+ comp->dy = cmptparms[compno].dy;
+ comp->w = cmptparms[compno].w;
+ comp->h = cmptparms[compno].h;
+ comp->x0 = cmptparms[compno].x0;
+ comp->y0 = cmptparms[compno].y0;
+ comp->prec = cmptparms[compno].prec;
+ comp->bpp = cmptparms[compno].bpp;
+ comp->sgnd = cmptparms[compno].sgnd;
+ comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
+ if(!comp->data) {
+ fprintf(stderr,"Unable to allocate memory for image.\n");
+ opj_image_destroy(image);
+ return NULL;
+ }
+ }
+ }
+
+ return image;
+}
+
+void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
+ if(image) {
+ if(image->comps) {
+ OPJ_UINT32 compno;
+
+ /* image components */
+ for(compno = 0; compno < image->numcomps; compno++) {
+ opj_image_comp_t *image_comp = &(image->comps[compno]);
+ if(image_comp->data) {
+ opj_free(image_comp->data);
+ }
+ }
+ opj_free(image->comps);
+ }
+
+ if(image->icc_profile_buf) {
+ opj_free(image->icc_profile_buf);
+ }
+
+ opj_free(image);
+ }
+}
+
+/**
+ * Updates the components characteristics of the image from the coding parameters.
+ *
+ * @param p_image_header the image header to update.
+ * @param p_cp the coding parameters from which to update the image.
+ */
+void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
+{
+ OPJ_UINT32 i, l_width, l_height;
+ OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
+ OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
+ opj_image_comp_t* l_img_comp = NULL;
+
+ l_x0 = opj_int_max((OPJ_INT32)p_cp->tx0 , (OPJ_INT32)p_image_header->x0);
+ l_y0 = opj_int_max((OPJ_INT32)p_cp->ty0 , (OPJ_INT32)p_image_header->y0);
+ l_x1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + p_cp->tw * p_cp->tdx), (OPJ_INT32)p_image_header->x1);
+ l_y1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + p_cp->th * p_cp->tdy), (OPJ_INT32)p_image_header->y1);
+
+ l_img_comp = p_image_header->comps;
+ for (i = 0; i < p_image_header->numcomps; ++i) {
+ l_comp_x0 = opj_int_ceildiv(l_x0, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y0 = opj_int_ceildiv(l_y0, (OPJ_INT32)l_img_comp->dy);
+ l_comp_x1 = opj_int_ceildiv(l_x1, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y1 = opj_int_ceildiv(l_y1, (OPJ_INT32)l_img_comp->dy);
+ l_width = (OPJ_UINT32)opj_int_ceildivpow2(l_comp_x1 - l_comp_x0, (OPJ_INT32)l_img_comp->factor);
+ l_height = (OPJ_UINT32)opj_int_ceildivpow2(l_comp_y1 - l_comp_y0, (OPJ_INT32)l_img_comp->factor);
+ l_img_comp->w = l_width;
+ l_img_comp->h = l_height;
+ l_img_comp->x0 = (OPJ_UINT32)l_comp_x0/*l_x0*/;
+ l_img_comp->y0 = (OPJ_UINT32)l_comp_y0/*l_y0*/;
+ ++l_img_comp;
+ }
+}
+
+
+/**
+ * Copy only header of image and its component header (no data are copied)
+ * if dest image have data, they will be freed
+ *
+ * @param p_image_src the src image
+ * @param p_image_dest the dest image
+ *
+ */
+void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
+{
+ OPJ_UINT32 compno;
+
+ /* preconditions */
+ assert(p_image_src != 00);
+ assert(p_image_dest != 00);
+
+ p_image_dest->x0 = p_image_src->x0;
+ p_image_dest->y0 = p_image_src->y0;
+ p_image_dest->x1 = p_image_src->x1;
+ p_image_dest->y1 = p_image_src->y1;
+
+ if (p_image_dest->comps){
+ for(compno = 0; compno < p_image_dest->numcomps; compno++) {
+ opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
+ if(image_comp->data) {
+ opj_free(image_comp->data);
+ }
+ }
+ opj_free(p_image_dest->comps);
+ p_image_dest->comps = NULL;
+ }
+
+ p_image_dest->numcomps = p_image_src->numcomps;
+
+ p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t));
+ if (!p_image_dest->comps){
+ p_image_dest->comps = NULL;
+ p_image_dest->numcomps = 0;
+ return;
+ }
+
+ for (compno=0; compno < p_image_dest->numcomps; compno++){
+ memcpy( &(p_image_dest->comps[compno]),
+ &(p_image_src->comps[compno]),
+ sizeof(opj_image_comp_t));
+ p_image_dest->comps[compno].data = NULL;
+ }
+
+ p_image_dest->color_space = p_image_src->color_space;
+ p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
+
+ if (p_image_dest->icc_profile_len) {
+ p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len);
+ if (!p_image_dest->icc_profile_buf){
+ p_image_dest->icc_profile_buf = NULL;
+ p_image_dest->icc_profile_len = 0;
+ return;
+ }
+ memcpy( p_image_dest->icc_profile_buf,
+ p_image_src->icc_profile_buf,
+ p_image_src->icc_profile_len);
+ }
+ else
+ p_image_dest->icc_profile_buf = NULL;
+
+ return;
+}
+
+opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
+ OPJ_UINT32 compno;
+ opj_image_t *image = 00;
+
+ image = (opj_image_t*) opj_malloc(sizeof(opj_image_t));
+ if (image)
+ {
+ memset(image,0,sizeof(opj_image_t));
+
+ image->color_space = clrspc;
+ image->numcomps = numcmpts;
+
+ /* allocate memory for the per-component information */
+ image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
+ if (!image->comps) {
+ opj_image_destroy(image);
+ return 00;
+ }
+ memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t));
+
+ /* create the individual image components */
+ for(compno = 0; compno < numcmpts; compno++) {
+ opj_image_comp_t *comp = &image->comps[compno];
+ comp->dx = cmptparms[compno].dx;
+ comp->dy = cmptparms[compno].dy;
+ comp->w = cmptparms[compno].w;
+ comp->h = cmptparms[compno].h;
+ comp->x0 = cmptparms[compno].x0;
+ comp->y0 = cmptparms[compno].y0;
+ comp->prec = cmptparms[compno].prec;
+ comp->sgnd = cmptparms[compno].sgnd;
+ comp->data = 0;
+ }
+ }
+
+ return image;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.h
new file mode 100644
index 0000000000..e0e2772d40
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/image.h
@@ -0,0 +1,68 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __IMAGE_H
+#define __IMAGE_H
+/**
+@file image.h
+@brief Implementation of operations on images (IMAGE)
+
+The functions in IMAGE.C have for goal to realize operations on images.
+*/
+
+struct opj_image;
+struct opj_cp;
+
+/** @defgroup IMAGE IMAGE - Implementation of operations on images */
+/*@{*/
+
+/**
+ * Create an empty image
+ *
+ * @return returns an empty image if successful, returns NULL otherwise
+ */
+opj_image_t* opj_image_create0(void);
+
+
+
+/**
+ * Updates the components characteristics of the image from the coding parameters.
+ *
+ * @param p_image_header the image header to update.
+ * @param p_cp the coding parameters from which to update the image.
+ */
+void opj_image_comp_header_update(opj_image_t * p_image, const struct opj_cp* p_cp);
+
+void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest);
+
+/*@}*/
+
+#endif /* __IMAGE_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/indexbox_manager.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/indexbox_manager.h
new file mode 100644
index 0000000000..ec5525f530
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/indexbox_manager.h
@@ -0,0 +1,148 @@
+/*
+ * $Id: indexbox_manager.h 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2003-2004, Yannick Verschueren
+ * Copyright (c) 2010-2011, Kaori Hagihara
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*! \file
+ * \brief Modification of jpip.c from 2KAN indexer
+ */
+
+#ifndef INDEXBOX_MANAGER_H_
+# define INDEXBOX_MANAGER_H_
+
+#include "openjpeg.h"
+#include "j2k.h" /* needed to use jp2.h */
+#include "jp2.h"
+
+#define JPIP_CIDX 0x63696478 /* Codestream index */
+#define JPIP_CPTR 0x63707472 /* Codestream Finder Box */
+#define JPIP_MANF 0x6d616e66 /* Manifest Box */
+#define JPIP_FAIX 0x66616978 /* Fragment array Index box */
+#define JPIP_MHIX 0x6d686978 /* Main Header Index Table */
+#define JPIP_TPIX 0x74706978 /* Tile-part Index Table box */
+#define JPIP_THIX 0x74686978 /* Tile header Index Table box */
+#define JPIP_PPIX 0x70706978 /* Precinct Packet Index Table box */
+#define JPIP_PHIX 0x70686978 /* Packet Header index Table */
+#define JPIP_FIDX 0x66696478 /* File Index */
+#define JPIP_FPTR 0x66707472 /* File Finder */
+#define JPIP_PRXY 0x70727879 /* Proxy boxes */
+#define JPIP_IPTR 0x69707472 /* Index finder box */
+#define JPIP_PHLD 0x70686c64 /* Place holder */
+
+
+/*
+ * Write tile-part Index table box (superbox)
+ *
+ * @param[in] coff offset of j2k codestream
+ * @param[in] cstr_info codestream information
+ * @param[in] j2klen length of j2k codestream
+ * @param[in] cio file output handle
+ * @return length of tpix box
+ */
+int opj_write_tpix( int coff, opj_codestream_info_t cstr_info, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+
+/*
+ * Write tile header index table box (superbox)
+ *
+ * @param[in] coff offset of j2k codestream
+ * @param[in] cstr_info codestream information pointer
+ * @param[in] cio file output handle
+ * @return length of thix box
+ */
+int opj_write_thix( int coff, opj_codestream_info_t cstr_info, opj_stream_private_t *cio, opj_event_mgr_t * p_manager );
+
+
+/*
+ * Write precinct packet index table box (superbox)
+ *
+ * @param[in] coff offset of j2k codestream
+ * @param[in] cstr_info codestream information
+ * @param[in] EPHused true if EPH option used
+ * @param[in] j2klen length of j2k codestream
+ * @param[in] cio file output handle
+ * @return length of ppix box
+ */
+int opj_write_ppix( int coff, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+
+/*
+ * Write packet header index table box (superbox)
+ *
+ * @param[in] coff offset of j2k codestream
+ * @param[in] cstr_info codestream information
+ * @param[in] EPHused true if EPH option used
+ * @param[in] j2klen length of j2k codestream
+ * @param[in] cio file output handle
+ * @return length of ppix box
+ */
+int opj_write_phix( int coff, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/*
+ * Wriet manifest box (box)
+ *
+ * @param[in] second number to be visited
+ * @param[in] v number of boxes
+ * @param[in] box box to be manifested
+ * @param[in] cio file output handle
+ */
+
+void opj_write_manf(int second,
+ int v,
+ opj_jp2_box_t *box,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/*
+ * Write main header index table (box)
+ *
+ * @param[in] coff offset of j2k codestream
+ * @param[in] cstr_info codestream information
+ * @param[in] cio file output handle
+ * @return length of mainmhix box
+ */
+int opj_write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+int opj_write_phixfaix( int coff, int compno, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+int opj_write_ppixfaix( int coff, int compno, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+int opj_write_tilemhix( int coff, opj_codestream_info_t cstr_info, int tileno, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+int opj_write_tpixfaix( int coff, int compno, opj_codestream_info_t cstr_info, int j2klen, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+#endif /* !INDEXBOX_MANAGER_H_ */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.c
new file mode 100644
index 0000000000..4c1ee780d3
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.c
@@ -0,0 +1,294 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/**
+ * LUP decomposition
+ */
+static OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix,
+ OPJ_UINT32 * permutations,
+ OPJ_FLOAT32 * p_swap_area,
+ OPJ_UINT32 nb_compo);
+/**
+ * LUP solving
+ */
+static void opj_lupSolve(OPJ_FLOAT32 * pResult,
+ OPJ_FLOAT32* pMatrix,
+ OPJ_FLOAT32* pVector,
+ OPJ_UINT32* pPermutations,
+ OPJ_UINT32 nb_compo,
+ OPJ_FLOAT32 * p_intermediate_data);
+
+/**
+ *LUP inversion (call with the result of lupDecompose)
+ */
+static void opj_lupInvert ( OPJ_FLOAT32 * pSrcMatrix,
+ OPJ_FLOAT32 * pDestMatrix,
+ OPJ_UINT32 nb_compo,
+ OPJ_UINT32 * pPermutations,
+ OPJ_FLOAT32 * p_src_temp,
+ OPJ_FLOAT32 * p_dest_temp,
+ OPJ_FLOAT32 * p_swap_area);
+
+/*
+==========================================================
+ Matric inversion interface
+==========================================================
+*/
+/**
+ * Matrix inversion.
+ */
+OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix,
+ OPJ_FLOAT32 * pDestMatrix,
+ OPJ_UINT32 nb_compo)
+{
+ OPJ_BYTE * l_data = 00;
+ OPJ_UINT32 l_permutation_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_UINT32);
+ OPJ_UINT32 l_swap_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+ OPJ_UINT32 l_total_size = l_permutation_size + 3 * l_swap_size;
+ OPJ_UINT32 * lPermutations = 00;
+ OPJ_FLOAT32 * l_double_data = 00;
+
+ l_data = (OPJ_BYTE *) opj_malloc(l_total_size);
+ if (l_data == 0) {
+ return OPJ_FALSE;
+ }
+ lPermutations = (OPJ_UINT32 *) l_data;
+ l_double_data = (OPJ_FLOAT32 *) (l_data + l_permutation_size);
+ memset(lPermutations,0,l_permutation_size);
+
+ if(! opj_lupDecompose(pSrcMatrix,lPermutations,l_double_data,nb_compo)) {
+ opj_free(l_data);
+ return OPJ_FALSE;
+ }
+
+ opj_lupInvert(pSrcMatrix,pDestMatrix,nb_compo,lPermutations,l_double_data,l_double_data + nb_compo,l_double_data + 2*nb_compo);
+ opj_free(l_data);
+
+ return OPJ_TRUE;
+}
+
+
+/*
+==========================================================
+ Local functions
+==========================================================
+*/
+OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix,OPJ_UINT32 * permutations,
+ OPJ_FLOAT32 * p_swap_area,
+ OPJ_UINT32 nb_compo)
+{
+ OPJ_UINT32 * tmpPermutations = permutations;
+ OPJ_UINT32 * dstPermutations;
+ OPJ_UINT32 k2=0,t;
+ OPJ_FLOAT32 temp;
+ OPJ_UINT32 i,j,k;
+ OPJ_FLOAT32 p;
+ OPJ_UINT32 lLastColum = nb_compo - 1;
+ OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+ OPJ_FLOAT32 * lTmpMatrix = matrix;
+ OPJ_FLOAT32 * lColumnMatrix,* lDestMatrix;
+ OPJ_UINT32 offset = 1;
+ OPJ_UINT32 lStride = nb_compo-1;
+
+ /*initialize permutations */
+ for (i = 0; i < nb_compo; ++i)
+ {
+ *tmpPermutations++ = i;
+ }
+ /* now make a pivot with colum switch */
+ tmpPermutations = permutations;
+ for (k = 0; k < lLastColum; ++k) {
+ p = 0.0;
+
+ /* take the middle element */
+ lColumnMatrix = lTmpMatrix + k;
+
+ /* make permutation with the biggest value in the column */
+ for (i = k; i < nb_compo; ++i) {
+ temp = ((*lColumnMatrix > 0) ? *lColumnMatrix : -(*lColumnMatrix));
+ if (temp > p) {
+ p = temp;
+ k2 = i;
+ }
+ /* next line */
+ lColumnMatrix += nb_compo;
+ }
+
+ /* a whole rest of 0 -> non singular */
+ if (p == 0.0) {
+ return OPJ_FALSE;
+ }
+
+ /* should we permute ? */
+ if (k2 != k) {
+ /*exchange of line */
+ /* k2 > k */
+ dstPermutations = tmpPermutations + k2 - k;
+ /* swap indices */
+ t = *tmpPermutations;
+ *tmpPermutations = *dstPermutations;
+ *dstPermutations = t;
+
+ /* and swap entire line. */
+ lColumnMatrix = lTmpMatrix + (k2 - k) * nb_compo;
+ memcpy(p_swap_area,lColumnMatrix,lSwapSize);
+ memcpy(lColumnMatrix,lTmpMatrix,lSwapSize);
+ memcpy(lTmpMatrix,p_swap_area,lSwapSize);
+ }
+
+ /* now update data in the rest of the line and line after */
+ lDestMatrix = lTmpMatrix + k;
+ lColumnMatrix = lDestMatrix + nb_compo;
+ /* take the middle element */
+ temp = *(lDestMatrix++);
+
+ /* now compute up data (i.e. coeff up of the diagonal). */
+ for (i = offset; i < nb_compo; ++i) {
+ /*lColumnMatrix; */
+ /* divide the lower column elements by the diagonal value */
+
+ /* matrix[i][k] /= matrix[k][k]; */
+ /* p = matrix[i][k] */
+ p = *lColumnMatrix / temp;
+ *(lColumnMatrix++) = p;
+
+ for (j = /* k + 1 */ offset; j < nb_compo; ++j) {
+ /* matrix[i][j] -= matrix[i][k] * matrix[k][j]; */
+ *(lColumnMatrix++) -= p * (*(lDestMatrix++));
+ }
+ /* come back to the k+1th element */
+ lDestMatrix -= lStride;
+ /* go to kth element of the next line */
+ lColumnMatrix += k;
+ }
+
+ /* offset is now k+2 */
+ ++offset;
+ /* 1 element less for stride */
+ --lStride;
+ /* next line */
+ lTmpMatrix+=nb_compo;
+ /* next permutation element */
+ ++tmpPermutations;
+ }
+ return OPJ_TRUE;
+}
+
+void opj_lupSolve (OPJ_FLOAT32 * pResult,
+ OPJ_FLOAT32 * pMatrix,
+ OPJ_FLOAT32 * pVector,
+ OPJ_UINT32* pPermutations,
+ OPJ_UINT32 nb_compo,OPJ_FLOAT32 * p_intermediate_data)
+{
+ OPJ_INT32 k;
+ OPJ_UINT32 i,j;
+ OPJ_FLOAT32 sum;
+ OPJ_FLOAT32 u;
+ OPJ_UINT32 lStride = nb_compo+1;
+ OPJ_FLOAT32 * lCurrentPtr;
+ OPJ_FLOAT32 * lIntermediatePtr;
+ OPJ_FLOAT32 * lDestPtr;
+ OPJ_FLOAT32 * lTmpMatrix;
+ OPJ_FLOAT32 * lLineMatrix = pMatrix;
+ OPJ_FLOAT32 * lBeginPtr = pResult + nb_compo - 1;
+ OPJ_FLOAT32 * lGeneratedData;
+ OPJ_UINT32 * lCurrentPermutationPtr = pPermutations;
+
+
+ lIntermediatePtr = p_intermediate_data;
+ lGeneratedData = p_intermediate_data + nb_compo - 1;
+
+ for (i = 0; i < nb_compo; ++i) {
+ sum = 0.0;
+ lCurrentPtr = p_intermediate_data;
+ lTmpMatrix = lLineMatrix;
+ for (j = 1; j <= i; ++j)
+ {
+ /* sum += matrix[i][j-1] * y[j-1]; */
+ sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++));
+ }
+ /*y[i] = pVector[pPermutations[i]] - sum; */
+ *(lIntermediatePtr++) = pVector[*(lCurrentPermutationPtr++)] - sum;
+ lLineMatrix += nb_compo;
+ }
+
+ /* we take the last point of the matrix */
+ lLineMatrix = pMatrix + nb_compo*nb_compo - 1;
+
+ /* and we take after the last point of the destination vector */
+ lDestPtr = pResult + nb_compo;
+
+
+ assert(nb_compo != 0);
+ for (k = (OPJ_INT32)nb_compo - 1; k != -1 ; --k) {
+ sum = 0.0;
+ lTmpMatrix = lLineMatrix;
+ u = *(lTmpMatrix++);
+ lCurrentPtr = lDestPtr--;
+ for (j = (OPJ_UINT32)(k + 1); j < nb_compo; ++j) {
+ /* sum += matrix[k][j] * x[j] */
+ sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++));
+ }
+ /*x[k] = (y[k] - sum) / u; */
+ *(lBeginPtr--) = (*(lGeneratedData--) - sum) / u;
+ lLineMatrix -= lStride;
+ }
+}
+
+
+void opj_lupInvert (OPJ_FLOAT32 * pSrcMatrix,
+ OPJ_FLOAT32 * pDestMatrix,
+ OPJ_UINT32 nb_compo,
+ OPJ_UINT32 * pPermutations,
+ OPJ_FLOAT32 * p_src_temp,
+ OPJ_FLOAT32 * p_dest_temp,
+ OPJ_FLOAT32 * p_swap_area )
+{
+ OPJ_UINT32 j,i;
+ OPJ_FLOAT32 * lCurrentPtr;
+ OPJ_FLOAT32 * lLineMatrix = pDestMatrix;
+ OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+
+ for (j = 0; j < nb_compo; ++j) {
+ lCurrentPtr = lLineMatrix++;
+ memset(p_src_temp,0,lSwapSize);
+ p_src_temp[j] = 1.0;
+ opj_lupSolve(p_dest_temp,pSrcMatrix,p_src_temp, pPermutations, nb_compo , p_swap_area);
+
+ for (i = 0; i < nb_compo; ++i) {
+ *(lCurrentPtr) = p_dest_temp[i];
+ lCurrentPtr+=nb_compo;
+ }
+ }
+}
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.h
new file mode 100644
index 0000000000..2fae8e54bd
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/invert.h
@@ -0,0 +1,64 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __INVERT_H
+#define __INVERT_H
+/**
+@file invert.h
+@brief Implementation of the matrix inversion
+
+The function in INVERT.H compute a matrix inversion with a LUP method
+*/
+
+/** @defgroup INVERT INVERT - Implementation of a matrix inversion */
+/*@{*/
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Calculates a n x n double matrix inversion with a LUP method. Data is aligned, rows after rows (or columns after columns).
+ * The function does not take ownership of any memory block, data must be fred by the user.
+ *
+ * @param pSrcMatrix the matrix to invert.
+ * @param pDestMatrix data to store the inverted matrix.
+ * @param n size of the matrix
+ * @return OPJ_TRUE if the inversion is successful, OPJ_FALSE if the matrix is singular.
+ */
+OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix,
+ OPJ_FLOAT32 * pDestMatrix,
+ OPJ_UINT32 nb_compo);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __INVERT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.c
new file mode 100644
index 0000000000..77a864ebb9
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.c
@@ -0,0 +1,10295 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * Copyright (c) 2006-2007, Parvatha Elangovan
+ * Copyright (c) 2010-2011, Kaori Hagihara
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+#define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
+#define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
+#define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
+#define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
+
+/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+ * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
+ */
+static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k);
+
+/**
+ * The read header procedure.
+ */
+static OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * The default encoding validation procedure without any extension.
+ *
+ * @param p_j2k the jpeg2000 codec to validate.
+ * @param p_stream the input stream to validate.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the parameters are correct.
+ */
+static OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * The default decoding validation procedure without any extension.
+ *
+ * @param p_j2k the jpeg2000 codec to validate.
+ * @param p_stream the input stream to validate.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the parameters are correct.
+ */
+static OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+static void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k);
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+static void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k);
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k);
+
+/**
+ * The mct encoding validation procedure.
+ *
+ * @param p_j2k the jpeg2000 codec to validate.
+ * @param p_stream the input stream to validate.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the parameters are correct.
+ */
+static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Builds the tcd decoder to use to decode tile.
+ */
+static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+/**
+ * Builds the tcd encoder to use to encode tile.
+ */
+static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Creates a tile-coder decoder.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Excutes the given procedures on the given codec.
+ *
+ * @param p_procedure_list the list of procedures to execute
+ * @param p_j2k the jpeg2000 codec to execute the procedures on.
+ * @param p_stream the stream to execute the procedures on.
+ * @param p_manager the user manager.
+ *
+ * @return true if all the procedures were successfully executed.
+ */
+static OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
+ opj_procedure_list_t * p_procedure_list,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Updates the rates of the tcp.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Copies the decoding tile parameters onto all the tile parameters.
+ * Creates also the tile decoder.
+ */
+static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Destroys the memory associated with the decoding of headers.
+ */
+static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads the lookup table containing all the marker, status and action, and returns the handler associated
+ * with the marker value.
+ * @param p_id Marker value to look up
+ *
+ * @return the handler associated with the id.
+*/
+static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
+
+/**
+ * Destroys a tile coding parameter structure.
+ *
+ * @param p_tcp the tile coding parameter to destroy.
+ */
+static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp);
+
+/**
+ * Destroys the data inside a tile coding parameter structure.
+ *
+ * @param p_tcp the tile coding parameter which contain data to destroy.
+ */
+static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp);
+
+/**
+ * Destroys a coding parameter structure.
+ *
+ * @param p_cp the coding parameter to destroy.
+ */
+static void opj_j2k_cp_destroy (opj_cp_t *p_cp);
+
+/**
+ * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
+ *
+ * @param p_j2k J2K codec.
+ * @param p_tile_no FIXME DOC
+ * @param p_comp_no the component number to output.
+ * @param p_data FIXME DOC
+ * @param p_header_size FIXME DOC
+ * @param p_manager the user event manager.
+ *
+ * @return FIXME DOC
+*/
+static OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
+ *
+ * @param p_j2k the J2K codec.
+ * @param p_tile_no the tile index.
+ * @param p_comp_no the component being outputted.
+ *
+ * @return the number of bytes taken by the SPCod element.
+ */
+static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no );
+
+/**
+ * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
+ * @param p_j2k the jpeg2000 codec.
+ * @param compno FIXME DOC
+ * @param p_header_data the data contained in the COM box.
+ * @param p_header_size the size of the data contained in the COM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 compno,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
+ *
+ * @param p_tile_no the tile index.
+ * @param p_comp_no the component being outputted.
+ * @param p_j2k the J2K codec.
+ *
+ * @return the number of bytes taken by the SPCod element.
+ */
+static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no );
+
+/**
+ * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
+ *
+ * @param p_tile_no the tile to output.
+ * @param p_comp_no the component number to output.
+ * @param p_data the data buffer.
+ * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+ *
+*/
+static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Updates the Tile Length Marker.
+ */
+static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
+
+/**
+ * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
+ *
+ * @param p_j2k J2K codec.
+ * @param compno the component number to output.
+ * @param p_header_data the data buffer.
+ * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
+ * @param p_manager the user event manager.
+ *
+*/
+static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 compno,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Copies the tile component parameters of all the component from the first tile component.
+ *
+ * @param p_j2k the J2k codec.
+ */
+static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k );
+
+/**
+ * Copies the tile quantization parameters of all the component from the first tile component.
+ *
+ * @param p_j2k the J2k codec.
+ */
+static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );
+
+/**
+ * Reads the tiles.
+ */
+static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager);
+
+static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
+
+static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
+
+static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Sets up the procedures to do on writing header.
+ * Developers wanting to extend the library can add their own writing procedures.
+ */
+static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k);
+
+static OPJ_BOOL opj_j2k_write_first_tile_part( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ opj_stream_private_t *p_stream,
+ struct opj_event_mgr * p_manager );
+
+static OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ opj_stream_private_t *p_stream,
+ struct opj_event_mgr * p_manager );
+
+/**
+ * Gets the offset of the header.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
+
+/*
+ * -----------------------------------------------------------------------
+ * -----------------------------------------------------------------------
+ * -----------------------------------------------------------------------
+ */
+
+/**
+ * Writes the SOC marker (Start Of Codestream)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a SOC marker (Start of Codestream)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_stream XXX needs data
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the SIZ marker (image and tile size)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a SIZ marker (image and tile size)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_header_data the data contained in the SIZ box.
+ * @param p_header_size the size of the data contained in the SIZ marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Writes the COM marker (comment)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a COM marker (comments)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_header_data the data contained in the COM box.
+ * @param p_header_size the size of the data contained in the COM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+/**
+ * Writes the COD marker (Coding style default)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a COD marker (Coding Styke defaults)
+ * @param p_header_data the data contained in the COD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+#if 0
+/**
+ * Writes the COC marker (Coding style component)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_comp_no the index of the component to output.
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
+/**
+ * Writes the COC marker (Coding style component)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_comp_no the index of the component to output.
+ * @param p_data FIXME DOC
+ * @param p_data_written FIXME DOC
+ * @param p_manager the user event manager.
+*/
+static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager );
+#endif
+
+/**
+ * Gets the maximum size taken by a coc.
+ *
+ * @param p_j2k the jpeg2000 codec to use.
+ */
+static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
+
+/**
+ * Reads a COC marker (Coding Style Component)
+ * @param p_header_data the data contained in the COC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the QCD marker (quantization default)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a QCD marker (Quantization defaults)
+ * @param p_header_data the data contained in the QCD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the QCD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+#if 0
+/**
+ * Writes the QCC marker (quantization component)
+ *
+ * @param p_comp_no the index of the component to output.
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
+/**
+ * Writes the QCC marker (quantization component)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_comp_no the index of the component to output.
+ * @param p_data FIXME DOC
+ * @param p_data_written the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager );
+#endif
+
+/**
+ * Gets the maximum size taken by a qcc.
+ */
+static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);
+
+/**
+ * Reads a QCC marker (Quantization component)
+ * @param p_header_data the data contained in the QCC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the QCC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+/**
+ * Writes the POC marker (Progression Order Change)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+/**
+ * Writes the POC marker (Progression Order Change)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_data FIXME DOC
+ * @param p_data_written the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager );
+/**
+ * Gets the maximum size taken by the writing of a POC.
+ */
+static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
+
+/**
+ * Reads a POC marker (Progression Order Change)
+ *
+ * @param p_header_data the data contained in the POC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the POC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
+ */
+static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k);
+
+/**
+ * Gets the maximum size taken by the headers of the SOT.
+ *
+ * @param p_j2k the jpeg2000 codec to use.
+ */
+static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
+
+/**
+ * Reads a CRG marker (Component registration)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+/**
+ * Reads a TLM marker (Tile Length Marker)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Writes the updated tlm.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a PLM marker (Packet length, main header marker)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+/**
+ * Reads a PLT marker (Packet length, tile-part header)
+ *
+ * @param p_header_data the data contained in the PLT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the PLT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+#if 0
+/**
+ * Reads a PPM marker (Packed packet headers, main header)
+ *
+ * @param p_header_data the data contained in the POC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the POC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL j2k_read_ppm_v2 (
+ opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ struct opj_event_mgr * p_manager
+ );
+#endif
+
+static OPJ_BOOL j2k_read_ppm_v3 (
+ opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a PPT marker (Packed packet headers, tile-part header)
+ *
+ * @param p_header_data the data contained in the PPT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the PPT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+/**
+ * Writes the TLM marker (Tile Length Marker)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the SOT marker (Start of tile-part)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_data FIXME DOC
+ * @param p_data_written FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ const opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a PPT marker (Packed packet headers, tile-part header)
+ *
+ * @param p_header_data the data contained in the PPT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the PPT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+/**
+ * Writes the SOD marker (Start of data)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_tile_coder FIXME DOC
+ * @param p_data FIXME DOC
+ * @param p_data_written FIXME DOC
+ * @param p_total_data_size FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k,
+ opj_tcd_t * p_tile_coder,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ const opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a SOD marker (Start Of Data)
+ *
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream FIXME DOC
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_sod( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )
+{
+ opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
+ ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
+
+ opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
+}
+
+/**
+ * Writes the RGN marker (Region Of Interest)
+ *
+ * @param p_tile_no the tile to output
+ * @param p_comp_no the component to output
+ * @param nb_comps the number of components
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_rgn( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_UINT32 nb_comps,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a RGN marker (Region Of Interest)
+ *
+ * @param p_header_data the data contained in the POC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the POC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the EOC marker (End of Codestream)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+#if 0
+/**
+ * Reads a EOC marker (End Of Codestream)
+ *
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream FIXME DOC
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+#endif
+
+/**
+ * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Inits the Info
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+Add main header marker information
+@param cstr_index Codestream information structure
+@param type marker type
+@param pos byte offset of marker segment
+@param len length of marker segment
+ */
+static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
+/**
+Add tile header marker information
+@param tileno tile index number
+@param cstr_index Codestream information structure
+@param type marker type
+@param pos byte offset of marker segment
+@param len length of marker segment
+ */
+static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
+
+/**
+ * Reads an unknown marker
+ *
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream the stream object to read from.
+ * @param output_marker FIXME DOC
+ * @param p_manager the user event manager.
+ *
+ * @return true if the marker could be deduced.
+*/
+static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ OPJ_UINT32 *output_marker,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the MCT marker (Multiple Component Transform)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_mct_record FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k,
+ opj_mct_data_t * p_mct_record,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a MCT marker (Multiple Component Transform)
+ *
+ * @param p_header_data the data contained in the MCT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the MCT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the MCC marker (Multiple Component Collection)
+ *
+ * @param p_j2k J2K codec.
+ * @param p_mcc_record FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k,
+ opj_simple_mcc_decorrelation_data_t * p_mcc_record,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a MCC marker (Multiple Component Collection)
+ *
+ * @param p_header_data the data contained in the MCC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the MCC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the MCO marker (Multiple component transformation ordering)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a MCO marker (Multiple Component Transform Ordering)
+ *
+ * @param p_header_data the data contained in the MCO box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the MCO marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
+
+static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+
+static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+
+static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+
+/**
+ * Ends the encoding, i.e. frees memory.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the CBD marker (Component bit depth definition)
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a CBD marker (Component bit depth definition)
+ * @param p_header_data the data contained in the CBD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CBD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+#if 0
+/**
+ * Writes COC marker for each component.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
+/**
+ * Writes QCC marker for each component.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+#endif
+
+/**
+ * Writes regions of interests.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes EPC ????
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Checks the progression order changes values. Tells of the poc given as input are valid.
+ * A nice message is outputted at errors.
+ *
+ * @param p_pocs the progression order changes.
+ * @param p_nb_pocs the number of progression order changes.
+ * @param p_nb_resolutions the number of resolutions.
+ * @param numcomps the number of components
+ * @param numlayers the number of layers.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the pocs are valid.
+ */
+static OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
+ OPJ_UINT32 p_nb_pocs,
+ OPJ_UINT32 p_nb_resolutions,
+ OPJ_UINT32 numcomps,
+ OPJ_UINT32 numlayers,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
+ *
+ * @param cp the coding parameters.
+ * @param pino the offset of the given poc (i.e. its position in the coding parameter).
+ * @param tileno the given tile.
+ *
+ * @return the number of tile parts.
+ */
+static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
+
+/**
+ * Calculates the total number of tile parts needed by the encoder to
+ * encode such an image. If not enough memory is available, then the function return false.
+ *
+ * @param p_nb_tiles pointer that will hold the number of tile parts.
+ * @param cp the coding parameters for the image.
+ * @param image the image to encode.
+ * @param p_j2k the p_j2k encoder.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the function was successful, false else.
+ */
+static OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k,
+ opj_cp_t *cp,
+ OPJ_UINT32 * p_nb_tiles,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager);
+
+static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
+
+static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
+
+static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
+
+static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);
+
+static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);
+
+static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
+
+static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
+
+static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+typedef struct j2k_prog_order{
+ OPJ_PROG_ORDER enum_prog;
+ char str_prog[5];
+}j2k_prog_order_t;
+
+j2k_prog_order_t j2k_prog_order_list[] = {
+ {OPJ_CPRL, "CPRL"},
+ {OPJ_LRCP, "LRCP"},
+ {OPJ_PCRL, "PCRL"},
+ {OPJ_RLCP, "RLCP"},
+ {OPJ_RPCL, "RPCL"},
+ {(OPJ_PROG_ORDER)-1, ""}
+};
+
+/**
+ * FIXME DOC
+ */
+static const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
+{
+ 2,
+ 4,
+ 4,
+ 8
+};
+
+typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
+
+const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
+{
+ opj_j2k_read_int16_to_float,
+ opj_j2k_read_int32_to_float,
+ opj_j2k_read_float32_to_float,
+ opj_j2k_read_float64_to_float
+};
+
+const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
+{
+ opj_j2k_read_int16_to_int32,
+ opj_j2k_read_int32_to_int32,
+ opj_j2k_read_float32_to_int32,
+ opj_j2k_read_float64_to_int32
+};
+
+const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
+{
+ opj_j2k_write_float_to_int16,
+ opj_j2k_write_float_to_int32,
+ opj_j2k_write_float_to_float,
+ opj_j2k_write_float_to_float64
+};
+
+typedef struct opj_dec_memory_marker_handler
+{
+ /** marker value */
+ OPJ_UINT32 id;
+ /** value of the state when the marker can appear */
+ OPJ_UINT32 states;
+ /** action linked to the marker */
+ OPJ_BOOL (*handler) ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+}
+opj_dec_memory_marker_handler_t;
+
+const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
+{
+ {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
+ {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
+ {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
+ {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
+ {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
+ {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
+ {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
+ {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
+ {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
+ {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
+ {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
+ {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
+ {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
+ {J2K_MS_SOP, 0, 0},
+ {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
+ {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
+ {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
+ {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
+ {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
+ {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
+#ifdef USE_JPWL
+#ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
+ {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
+ {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
+ {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
+ {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
+#endif
+#endif /* USE_JPWL */
+#ifdef USE_JPSEC
+ {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
+ {J2K_MS_INSEC, 0, j2k_read_insec}
+#endif /* USE_JPSEC */
+ {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
+};
+
+void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_bytes(l_src_data,&l_temp,2);
+
+ l_src_data+=sizeof(OPJ_INT16);
+
+ *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
+ }
+}
+
+void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_bytes(l_src_data,&l_temp,4);
+
+ l_src_data+=sizeof(OPJ_INT32);
+
+ *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
+ }
+}
+
+void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_float(l_src_data,&l_temp);
+
+ l_src_data+=sizeof(OPJ_FLOAT32);
+
+ *(l_dest_data++) = l_temp;
+ }
+}
+
+void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT64 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_double(l_src_data,&l_temp);
+
+ l_src_data+=sizeof(OPJ_FLOAT64);
+
+ *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
+ }
+}
+
+void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_bytes(l_src_data,&l_temp,2);
+
+ l_src_data+=sizeof(OPJ_INT16);
+
+ *(l_dest_data++) = (OPJ_INT32) l_temp;
+ }
+}
+
+void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_bytes(l_src_data,&l_temp,4);
+
+ l_src_data+=sizeof(OPJ_INT32);
+
+ *(l_dest_data++) = (OPJ_INT32) l_temp;
+ }
+}
+
+void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_float(l_src_data,&l_temp);
+
+ l_src_data+=sizeof(OPJ_FLOAT32);
+
+ *(l_dest_data++) = (OPJ_INT32) l_temp;
+ }
+}
+
+void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
+ OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT64 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ opj_read_double(l_src_data,&l_temp);
+
+ l_src_data+=sizeof(OPJ_FLOAT64);
+
+ *(l_dest_data++) = (OPJ_INT32) l_temp;
+ }
+}
+
+void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
+ OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ l_temp = (OPJ_UINT32) *(l_src_data++);
+
+ opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
+
+ l_dest_data+=sizeof(OPJ_INT16);
+ }
+}
+
+void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
+ OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ l_temp = (OPJ_UINT32) *(l_src_data++);
+
+ opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
+
+ l_dest_data+=sizeof(OPJ_INT32);
+ }
+}
+
+void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
+ OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT32 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ l_temp = (OPJ_FLOAT32) *(l_src_data++);
+
+ opj_write_float(l_dest_data,l_temp);
+
+ l_dest_data+=sizeof(OPJ_FLOAT32);
+ }
+}
+
+void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
+{
+ OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
+ OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
+ OPJ_UINT32 i;
+ OPJ_FLOAT64 l_temp;
+
+ for (i=0;i<p_nb_elem;++i) {
+ l_temp = (OPJ_FLOAT64) *(l_src_data++);
+
+ opj_write_double(l_dest_data,l_temp);
+
+ l_dest_data+=sizeof(OPJ_FLOAT64);
+ }
+}
+
+char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
+ j2k_prog_order_t *po;
+ for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
+ if(po->enum_prog == prg_order){
+ return po->str_prog;
+ }
+ }
+ return po->str_prog;
+}
+
+OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
+ OPJ_UINT32 p_nb_pocs,
+ OPJ_UINT32 p_nb_resolutions,
+ OPJ_UINT32 p_num_comps,
+ OPJ_UINT32 p_num_layers,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32* packet_array;
+ OPJ_UINT32 index , resno, compno, layno;
+ OPJ_UINT32 i;
+ OPJ_UINT32 step_c = 1;
+ OPJ_UINT32 step_r = p_num_comps * step_c;
+ OPJ_UINT32 step_l = p_nb_resolutions * step_r;
+ OPJ_BOOL loss = OPJ_FALSE;
+ OPJ_UINT32 layno0 = 0;
+
+ packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
+ if (packet_array == 00) {
+ opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
+ return OPJ_FALSE;
+ }
+ memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
+
+ if (p_nb_pocs == 0) {
+ opj_free(packet_array);
+ return OPJ_TRUE;
+ }
+
+ index = step_r * p_pocs->resno0;
+ /* take each resolution for each poc */
+ for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
+ {
+ OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
+
+ /* take each comp of each resolution for each poc */
+ for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
+ OPJ_UINT32 comp_index = res_index + layno0 * step_l;
+
+ /* and finally take each layer of each res of ... */
+ for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
+ /*index = step_r * resno + step_c * compno + step_l * layno;*/
+ packet_array[comp_index] = 1;
+ comp_index += step_l;
+ }
+
+ res_index += step_c;
+ }
+
+ index += step_r;
+ }
+ ++p_pocs;
+
+ /* iterate through all the pocs */
+ for (i = 1; i < p_nb_pocs ; ++i) {
+ OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
+
+ layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
+ index = step_r * p_pocs->resno0;
+
+ /* take each resolution for each poc */
+ for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
+ OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
+
+ /* take each comp of each resolution for each poc */
+ for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
+ OPJ_UINT32 comp_index = res_index + layno0 * step_l;
+
+ /* and finally take each layer of each res of ... */
+ for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
+ /*index = step_r * resno + step_c * compno + step_l * layno;*/
+ packet_array[comp_index] = 1;
+ comp_index += step_l;
+ }
+
+ res_index += step_c;
+ }
+
+ index += step_r;
+ }
+
+ ++p_pocs;
+ }
+
+ index = 0;
+ for (layno = 0; layno < p_num_layers ; ++layno) {
+ for (resno = 0; resno < p_nb_resolutions; ++resno) {
+ for (compno = 0; compno < p_num_comps; ++compno) {
+ loss |= (packet_array[index]!=1);
+ /*index = step_r * resno + step_c * compno + step_l * layno;*/
+ index += step_c;
+ }
+ }
+ }
+
+ if (loss) {
+ opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
+ }
+
+ opj_free(packet_array);
+
+ return !loss;
+}
+
+/* ----------------------------------------------------------------------- */
+
+OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
+{
+ const OPJ_CHAR *prog = 00;
+ OPJ_INT32 i;
+ OPJ_UINT32 tpnum = 1;
+ opj_tcp_t *tcp = 00;
+ opj_poc_t * l_current_poc = 00;
+
+ /* preconditions */
+ assert(tileno < (cp->tw * cp->th));
+ assert(pino < (cp->tcps[tileno].numpocs + 1));
+
+ /* get the given tile coding parameter */
+ tcp = &cp->tcps[tileno];
+ assert(tcp != 00);
+
+ l_current_poc = &(tcp->pocs[pino]);
+ assert(l_current_poc != 0);
+
+ /* get the progression order as a character string */
+ prog = opj_j2k_convert_progression_order(tcp->prg);
+ assert(strlen(prog) > 0);
+
+ if (cp->m_specific_param.m_enc.m_tp_on == 1) {
+ for (i=0;i<4;++i) {
+ switch (prog[i])
+ {
+ /* component wise */
+ case 'C':
+ tpnum *= l_current_poc->compE;
+ break;
+ /* resolution wise */
+ case 'R':
+ tpnum *= l_current_poc->resE;
+ break;
+ /* precinct wise */
+ case 'P':
+ tpnum *= l_current_poc->prcE;
+ break;
+ /* layer wise */
+ case 'L':
+ tpnum *= l_current_poc->layE;
+ break;
+ }
+ /* whould we split here ? */
+ if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
+ cp->m_specific_param.m_enc.m_tp_pos=i;
+ break;
+ }
+ }
+ }
+ else {
+ tpnum=1;
+ }
+
+ return tpnum;
+}
+
+OPJ_BOOL opj_j2k_calculate_tp( opj_j2k_t *p_j2k,
+ opj_cp_t *cp,
+ OPJ_UINT32 * p_nb_tiles,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 pino,tileno;
+ OPJ_UINT32 l_nb_tiles;
+ opj_tcp_t *tcp;
+
+ /* preconditions */
+ assert(p_nb_tiles != 00);
+ assert(cp != 00);
+ assert(image != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_nb_tiles = cp->tw * cp->th;
+ * p_nb_tiles = 0;
+ tcp = cp->tcps;
+
+ /* INDEX >> */
+ /* TODO mergeV2: check this part which use cstr_info */
+ /*if (p_j2k->cstr_info) {
+ opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
+
+ for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
+ OPJ_UINT32 cur_totnum_tp = 0;
+
+ opj_pi_update_encoding_parameters(image,cp,tileno);
+
+ for (pino = 0; pino <= tcp->numpocs; ++pino)
+ {
+ OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
+
+ *p_nb_tiles = *p_nb_tiles + tp_num;
+
+ cur_totnum_tp += tp_num;
+ }
+
+ tcp->m_nb_tile_parts = cur_totnum_tp;
+
+ l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
+ if (l_info_tile_ptr->tp == 00) {
+ return OPJ_FALSE;
+ }
+
+ memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
+
+ l_info_tile_ptr->num_tps = cur_totnum_tp;
+
+ ++l_info_tile_ptr;
+ ++tcp;
+ }
+ }
+ else */{
+ for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
+ OPJ_UINT32 cur_totnum_tp = 0;
+
+ opj_pi_update_encoding_parameters(image,cp,tileno);
+
+ for (pino = 0; pino <= tcp->numpocs; ++pino) {
+ OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
+
+ *p_nb_tiles = *p_nb_tiles + tp_num;
+
+ cur_totnum_tp += tp_num;
+ }
+ tcp->m_nb_tile_parts = cur_totnum_tp;
+
+ ++tcp;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_soc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ /* 2 bytes will be written */
+ OPJ_BYTE * l_start_stream = 00;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ /* write SOC identifier */
+ opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
+
+ if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
+ return OPJ_FALSE;
+ }
+
+/* UniPG>> */
+#ifdef USE_JPWL
+ /* update markers struct */
+/*
+ OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
+*/
+ assert( 0 && "TODO" );
+#endif /* USE_JPWL */
+/* <<UniPG */
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a SOC marker (Start of Codestream)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_stream FIXME DOC
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_soc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_BYTE l_data [2];
+ OPJ_UINT32 l_marker;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_data,&l_marker,2);
+ if (l_marker != J2K_MS_SOC) {
+ return OPJ_FALSE;
+ }
+
+ /* Next marker should be a SIZ marker in the main header */
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
+
+ /* FIXME move it in a index structure included in p_j2k*/
+ p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
+
+ opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
+
+ /* Add the marker to the codestream index*/
+ if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
+ return OPJ_FALSE;
+ }
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_siz( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_size_len;
+ OPJ_BYTE * l_current_ptr;
+ opj_image_t * l_image = 00;
+ opj_cp_t *cp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ cp = &(p_j2k->m_cp);
+ l_size_len = 40 + 3 * l_image->numcomps;
+ l_img_comp = l_image->comps;
+
+ if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
+ }
+
+ l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ /* write SOC identifier */
+ opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
+ l_current_ptr+=2;
+
+ opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
+ l_current_ptr+=2;
+
+ opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
+ l_current_ptr+=2;
+
+ opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
+ l_current_ptr+=4;
+
+ opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
+ l_current_ptr+=2;
+
+ for (i = 0; i < l_image->numcomps; ++i) {
+ /* TODO here with MCT ? */
+ opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
+ ++l_current_ptr;
+
+ opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
+ ++l_current_ptr;
+
+ opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
+ ++l_current_ptr;
+
+ ++l_img_comp;
+ }
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a SIZ marker (image and tile size)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_header_data the data contained in the SIZ box.
+ * @param p_header_size the size of the data contained in the SIZ marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_nb_comp;
+ OPJ_UINT32 l_nb_comp_remain;
+ OPJ_UINT32 l_remaining_size;
+ OPJ_UINT32 l_nb_tiles;
+ OPJ_UINT32 l_tmp;
+ opj_image_t *l_image = 00;
+ opj_cp_t *l_cp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcp_t * l_current_tile_param = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_cp = &(p_j2k->m_cp);
+
+ /* minimum size == 39 - 3 (= minimum component parameter) */
+ if (p_header_size < 36) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
+ return OPJ_FALSE;
+ }
+
+ l_remaining_size = p_header_size - 36;
+ l_nb_comp = l_remaining_size / 3;
+ l_nb_comp_remain = l_remaining_size % 3;
+ if (l_nb_comp_remain != 0){
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
+ p_header_data+=2;
+ l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
+ p_header_data+=4;
+ opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
+ p_header_data+=2;
+ if (l_tmp < 16385)
+ l_image->numcomps = (OPJ_UINT16) l_tmp;
+ else {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
+ return OPJ_FALSE;
+ }
+
+ if (l_image->numcomps != l_nb_comp) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp);
+ return OPJ_FALSE;
+ }
+
+ /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
+ if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
+ return OPJ_FALSE;
+ }
+ /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
+ if (!(l_cp->tdx * l_cp->tdy)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy);
+ return OPJ_FALSE;
+ }
+
+ /* testcase 1610.pdf.SIGSEGV.59c.681 */
+ if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
+ return OPJ_FALSE;
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters */
+ if (!(l_image->x1 * l_image->y1)) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "JPWL: bad image size (%d x %d)\n",
+ l_image->x1, l_image->y1);
+ if (!JPWL_ASSUME || JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ }
+
+ /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
+ if (l_image->numcomps != ((len - 38) / 3)) {
+ opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
+ l_image->numcomps, ((len - 38) / 3));
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ */ /* we try to correct */
+ /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
+ if (l_image->numcomps < ((len - 38) / 3)) {
+ len = 38 + 3 * l_image->numcomps;
+ opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
+ len);
+ } else {
+ l_image->numcomps = ((len - 38) / 3);
+ opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
+ l_image->numcomps);
+ }
+ }
+ */
+
+ /* update components number in the jpwl_exp_comps filed */
+ l_cp->exp_comps = l_image->numcomps;
+ }
+#endif /* USE_JPWL */
+
+ /* Allocate the resulting image components */
+ l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
+ if (l_image->comps == 00){
+ l_image->numcomps = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+
+ memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
+ l_img_comp = l_image->comps;
+
+ /* Read the component information */
+ for (i = 0; i < l_image->numcomps; ++i){
+ OPJ_UINT32 tmp;
+ opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
+ ++p_header_data;
+ l_img_comp->prec = (tmp & 0x7f) + 1;
+ l_img_comp->sgnd = tmp >> 7;
+ opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
+ ++p_header_data;
+ l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
+ opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
+ ++p_header_data;
+ l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
+ if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
+ l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
+ i, l_img_comp->dx, l_img_comp->dy);
+ return OPJ_FALSE;
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters, again */
+ if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
+ opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
+ i, i, l_image->comps[i].dx, l_image->comps[i].dy);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
+ if (!l_image->comps[i].dx) {
+ l_image->comps[i].dx = 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
+ i, l_image->comps[i].dx);
+ }
+ if (!l_image->comps[i].dy) {
+ l_image->comps[i].dy = 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
+ i, l_image->comps[i].dy);
+ }
+ }
+ }
+#endif /* USE_JPWL */
+ l_img_comp->resno_decoded = 0; /* number of resolution decoded */
+ l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
+ ++l_img_comp;
+ }
+
+ /* Compute the number of tiles */
+ l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+ l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
+
+ /* Check that the number of tiles is valid */
+ if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
+ opj_event_msg( p_manager, EVT_ERROR,
+ "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
+ l_cp->tw, l_cp->th);
+ return OPJ_FALSE;
+ }
+ l_nb_tiles = l_cp->tw * l_cp->th;
+
+ /* Define the tiles which will be decoded */
+ if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters */
+ if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
+ opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad number of tiles (%d x %d)\n",
+ l_cp->tw, l_cp->th);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
+ if (l_cp->tw < 1) {
+ l_cp->tw= 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
+ l_cp->tw);
+ }
+ if (l_cp->tw > l_cp->max_tiles) {
+ l_cp->tw= 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
+ "- setting %d tiles in x => HYPOTHESIS!!!\n",
+ l_cp->max_tiles, l_cp->tw);
+ }
+ if (l_cp->th < 1) {
+ l_cp->th= 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
+ l_cp->th);
+ }
+ if (l_cp->th > l_cp->max_tiles) {
+ l_cp->th= 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
+ "- setting %d tiles in y => HYPOTHESIS!!!\n",
+ l_cp->max_tiles, l_cp->th);
+ }
+ }
+ }
+#endif /* USE_JPWL */
+
+ /* memory allocations */
+ l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
+ if (l_cp->tcps == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+ if (!l_cp->tcps) {
+ opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: could not alloc tcps field of cp\n");
+ if (!JPWL_ASSUME || JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ }
+ }
+#endif /* USE_JPWL */
+
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
+ (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
+ if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
+
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
+ (opj_mct_data_t*)opj_malloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
+
+ if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
+
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
+ (opj_simple_mcc_decorrelation_data_t*)
+ opj_malloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
+
+ if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
+
+ /* set up default dc level shift */
+ for (i=0;i<l_image->numcomps;++i) {
+ if (! l_image->comps[i].sgnd) {
+ p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
+ }
+ }
+
+ l_current_tile_param = l_cp->tcps;
+ for (i = 0; i < l_nb_tiles; ++i) {
+ l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
+ if (l_current_tile_param->tccps == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
+ return OPJ_FALSE;
+ }
+ memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
+
+ ++l_current_tile_param;
+ }
+
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
+ opj_image_comp_header_update(l_image,l_cp);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_com( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_comment_size;
+ OPJ_UINT32 l_total_com_size;
+ const OPJ_CHAR *l_comment;
+ OPJ_BYTE * l_current_ptr = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ l_comment = p_j2k->m_cp.comment;
+ l_comment_size = (OPJ_UINT32)strlen(l_comment);
+ l_total_com_size = l_comment_size + 6;
+
+ if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
+ }
+
+ l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
+ l_current_ptr+=2;
+
+ opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
+ l_current_ptr+=2;
+
+ opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
+ l_current_ptr+=2;
+
+ memcpy( l_current_ptr,l_comment,l_comment_size);
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a COM marker (comments)
+ * @param p_j2k the jpeg2000 file codec.
+ * @param p_header_data the data contained in the COM box.
+ * @param p_header_size the size of the data contained in the COM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_com ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+ (void)p_header_size;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_cod( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_code_size,l_remaining_size;
+ OPJ_BYTE * l_current_data = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
+ l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
+ l_remaining_size = l_code_size;
+
+ if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
+ l_current_data+=2;
+
+ opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
+ ++l_current_data;
+
+ l_remaining_size -= 9;
+
+ if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (l_remaining_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a COD marker (Coding Styke defaults)
+ * @param p_header_data the data contained in the COD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cod ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* loop */
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_tmp;
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_image_t *l_image = 00;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_cp = &(p_j2k->m_cp);
+
+ /* If we are in the first tile-part header of the current tile */
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ /* Make sure room is sufficient */
+ if (p_header_size < 5) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
+ ++p_header_data;
+ opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
+ ++p_header_data;
+ l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
+ opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
+ p_header_data+=2;
+
+ /* If user didn't set a number layer to decode take the max specify in the codestream. */
+ if (l_cp->m_specific_param.m_dec.m_layer) {
+ l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
+ }
+ else {
+ l_tcp->num_layers_to_decode = l_tcp->numlayers;
+ }
+
+ opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
+ ++p_header_data;
+
+ p_header_size -= 5;
+ for (i = 0; i < l_image->numcomps; ++i) {
+ l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
+ }
+
+ if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_header_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* Apply the coding style to other components of the current tile or the m_default_tcp*/
+ opj_j2k_copy_tile_component_parameters(p_j2k);
+
+ /* Index */
+#ifdef WIP_REMOVE_MSD
+ if (p_j2k->cstr_info) {
+ /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
+ p_j2k->cstr_info->prog = l_tcp->prg;
+ p_j2k->cstr_info->numlayers = l_tcp->numlayers;
+ p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
+ for (i = 0; i < l_image->numcomps; ++i) {
+ p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
+ }
+ }
+#endif
+
+ return OPJ_TRUE;
+}
+
+#if 0
+OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 l_coc_size,l_remaining_size;
+ OPJ_UINT32 l_comp_room;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
+
+ l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+
+ if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data;
+ /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
+ = (OPJ_BYTE*)opj_realloc(
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data,
+ l_coc_size);*/
+
+ new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
+ }
+
+ opj_j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+#endif
+
+#if 0
+void opj_j2k_write_coc_in_memory( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_coc_size,l_remaining_size;
+ OPJ_BYTE * l_current_data = 00;
+ opj_image_t *l_image = 00;
+ OPJ_UINT32 l_comp_room;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
+ l_image = p_j2k->m_private_image;
+ l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
+
+ l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+ l_remaining_size = l_coc_size;
+
+ l_current_data = p_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
+ l_current_data+=l_comp_room;
+
+ opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
+ ++l_current_data;
+
+ l_remaining_size -= (5 + l_comp_room);
+ opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
+ * p_data_written = l_coc_size;
+}
+#endif
+
+OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
+{
+ OPJ_UINT32 i,j;
+ OPJ_UINT32 l_nb_comp;
+ OPJ_UINT32 l_nb_tiles;
+ OPJ_UINT32 l_max = 0;
+
+ /* preconditions */
+
+ l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
+ l_nb_comp = p_j2k->m_private_image->numcomps;
+
+ for (i=0;i<l_nb_tiles;++i) {
+ for (j=0;j<l_nb_comp;++j) {
+ l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
+ }
+ }
+
+ return 6 + l_max;
+}
+
+/**
+ * Reads a COC marker (Coding Style Component)
+ * @param p_header_data the data contained in the COC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_coc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_cp_t *l_cp = NULL;
+ opj_tcp_t *l_tcp = NULL;
+ opj_image_t *l_image = NULL;
+ OPJ_UINT32 l_comp_room;
+ OPJ_UINT32 l_comp_no;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+ l_image = p_j2k->m_private_image;
+
+ l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
+
+ /* make sure room is sufficient*/
+ if (p_header_size < l_comp_room + 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
+ return OPJ_FALSE;
+ }
+ p_header_size -= l_comp_room + 1;
+
+ opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
+ p_header_data += l_comp_room;
+ if (l_comp_no >= l_image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
+ ++p_header_data ;
+
+ if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_header_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
+ return OPJ_FALSE;
+ }
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_qcd( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_qcd_size,l_remaining_size;
+ OPJ_BYTE * l_current_data = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
+ l_remaining_size = l_qcd_size;
+
+ if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
+ l_current_data += 2;
+
+ l_remaining_size -= 4;
+
+ if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (l_remaining_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a QCD marker (Quantization defaults)
+ * @param p_header_data the data contained in the QCD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the QCD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_qcd ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_header_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
+ opj_j2k_copy_tile_quantization_parameters(p_j2k);
+
+ return OPJ_TRUE;
+}
+
+#if 0
+OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_qcc_size,l_remaining_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+ l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
+ l_remaining_size = l_qcc_size;
+
+ if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
+ }
+
+ opj_j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+#endif
+
+#if 0
+void opj_j2k_write_qcc_in_memory( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_qcc_size,l_remaining_size;
+ OPJ_BYTE * l_current_data = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+ l_remaining_size = l_qcc_size;
+
+ l_current_data = p_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
+ l_current_data += 2;
+
+ if (p_j2k->m_private_image->numcomps <= 256) {
+ --l_qcc_size;
+
+ opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
+ ++l_current_data;
+
+ /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
+ l_remaining_size -= 6;
+ }
+ else {
+ opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
+ l_current_data+=2;
+
+ l_remaining_size -= 6;
+ }
+
+ opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
+
+ *p_data_written = l_qcc_size;
+}
+#endif
+
+OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
+{
+ return opj_j2k_get_max_coc_size(p_j2k);
+}
+
+/**
+ * Reads a QCC marker (Quantization component)
+ * @param p_header_data the data contained in the QCC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the QCC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_qcc( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_num_comp,l_comp_no;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_num_comp = p_j2k->m_private_image->numcomps;
+
+ if (l_num_comp <= 256) {
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
+ return OPJ_FALSE;
+ }
+ opj_read_bytes(p_header_data,&l_comp_no,1);
+ ++p_header_data;
+ --p_header_size;
+ }
+ else {
+ if (p_header_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
+ return OPJ_FALSE;
+ }
+ opj_read_bytes(p_header_data,&l_comp_no,2);
+ p_header_data+=2;
+ p_header_size-=2;
+ }
+
+#ifdef USE_JPWL
+ if (p_j2k->m_cp.correct) {
+
+ static OPJ_UINT32 backup_compno = 0;
+
+ /* compno is negative or larger than the number of components!!! */
+ if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
+ l_comp_no, l_num_comp);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ l_comp_no = backup_compno % l_num_comp;
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
+ "- setting component number to %d\n",
+ l_comp_no);
+ }
+
+ /* keep your private count of tiles */
+ backup_compno++;
+ };
+#endif /* USE_JPWL */
+
+ if (l_comp_no >= p_j2k->m_private_image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid component number: %d, regarding the number of components %d\n",
+ l_comp_no, p_j2k->m_private_image->numcomps);
+ return OPJ_FALSE;
+ }
+
+ if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_header_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_poc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_nb_comp;
+ OPJ_UINT32 l_nb_poc;
+ OPJ_UINT32 l_poc_size;
+ OPJ_UINT32 l_written_size = 0;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_poc_room;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
+ l_nb_comp = p_j2k->m_private_image->numcomps;
+ l_nb_poc = 1 + l_tcp->numpocs;
+
+ if (l_nb_comp <= 256) {
+ l_poc_room = 1;
+ }
+ else {
+ l_poc_room = 2;
+ }
+ l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
+
+ if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
+ }
+
+ opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_write_poc_in_memory( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i;
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_nb_comp;
+ OPJ_UINT32 l_nb_poc;
+ OPJ_UINT32 l_poc_size;
+ opj_image_t *l_image = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+ opj_poc_t *l_current_poc = 00;
+ OPJ_UINT32 l_poc_room;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
+ l_tccp = &l_tcp->tccps[0];
+ l_image = p_j2k->m_private_image;
+ l_nb_comp = l_image->numcomps;
+ l_nb_poc = 1 + l_tcp->numpocs;
+
+ if (l_nb_comp <= 256) {
+ l_poc_room = 1;
+ }
+ else {
+ l_poc_room = 2;
+ }
+
+ l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
+
+ l_current_data = p_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
+ l_current_data += 2;
+
+ l_current_poc = l_tcp->pocs;
+ for (i = 0; i < l_nb_poc; ++i) {
+ opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
+ l_current_data+=l_poc_room;
+
+ opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
+ l_current_data+=2;
+
+ opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
+ l_current_data+=l_poc_room;
+
+ opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
+ ++l_current_data;
+
+ /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
+ l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
+ l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
+ l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
+
+ ++l_current_poc;
+ }
+
+ *p_data_written = l_poc_size;
+}
+
+OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
+{
+ opj_tcp_t * l_tcp = 00;
+ OPJ_UINT32 l_nb_tiles = 0;
+ OPJ_UINT32 l_max_poc = 0;
+ OPJ_UINT32 i;
+
+ l_tcp = p_j2k->m_cp.tcps;
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+
+ for (i=0;i<l_nb_tiles;++i) {
+ l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);
+ ++l_tcp;
+ }
+
+ ++l_max_poc;
+
+ return 4 + 9 * l_max_poc;
+}
+
+OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_nb_tiles;
+ OPJ_UINT32 l_max = 0;
+ opj_tcp_t * l_tcp = 00;
+
+ l_tcp = p_j2k->m_cp.tcps;
+ l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
+
+ for (i=0;i<l_nb_tiles;++i) {
+ l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);
+
+ ++l_tcp;
+ }
+
+ return 12 * l_max;
+}
+
+OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
+{
+ OPJ_UINT32 l_nb_bytes = 0;
+ OPJ_UINT32 l_nb_comps;
+ OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
+
+ l_nb_comps = p_j2k->m_private_image->numcomps - 1;
+ l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
+
+ if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
+ l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
+ l_nb_bytes += l_nb_comps * l_coc_bytes;
+
+ l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
+ l_nb_bytes += l_nb_comps * l_qcc_bytes;
+ }
+
+ l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
+
+ /*** DEVELOPER CORNER, Add room for your headers ***/
+
+ return l_nb_bytes;
+}
+
+/**
+ * Reads a POC marker (Progression Order Change)
+ *
+ * @param p_header_data the data contained in the POC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the POC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_poc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i, l_nb_comp, l_tmp;
+ opj_image_t * l_image = 00;
+ OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
+ OPJ_UINT32 l_chunk_size, l_comp_room;
+
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_poc_t *l_current_poc = 00;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_nb_comp = l_image->numcomps;
+ if (l_nb_comp <= 256) {
+ l_comp_room = 1;
+ }
+ else {
+ l_comp_room = 2;
+ }
+ l_chunk_size = 5 + 2 * l_comp_room;
+ l_current_poc_nb = p_header_size / l_chunk_size;
+ l_current_poc_remaining = p_header_size % l_chunk_size;
+
+ if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+ l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
+ l_current_poc_nb += l_old_poc_nb;
+
+ if(l_current_poc_nb >= 32)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
+ return OPJ_FALSE;
+ }
+ assert(l_current_poc_nb < 32);
+
+ /* now poc is in use.*/
+ l_tcp->POC = 1;
+
+ l_current_poc = &l_tcp->pocs[l_old_poc_nb];
+ for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
+ opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
+ ++p_header_data;
+ opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
+ p_header_data+=l_comp_room;
+ opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
+ /* make sure layer end is in acceptable bounds */
+ l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
+ p_header_data+=2;
+ opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
+ ++p_header_data;
+ opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
+ p_header_data+=l_comp_room;
+ opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
+ ++p_header_data;
+ l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
+ /* make sure comp is in acceptable bounds */
+ l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
+ ++l_current_poc;
+ }
+
+ l_tcp->numpocs = l_current_poc_nb - 1;
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a CRG marker (Component registration)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_crg ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_nb_comp;
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_nb_comp = p_j2k->m_private_image->numcomps;
+
+ if (p_header_size != l_nb_comp *4) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
+ return OPJ_FALSE;
+ }
+ /* Do not care of this at the moment since only local variables are set here */
+ /*
+ for
+ (i = 0; i < l_nb_comp; ++i)
+ {
+ opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
+ p_header_data+=2;
+ opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
+ p_header_data+=2;
+ }
+ */
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a TLM marker (Tile Length Marker)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_tlm ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if (p_header_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
+ return OPJ_FALSE;
+ }
+ p_header_size -= 2;
+
+ opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
+ ++p_header_data;
+ opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
+ ++p_header_data;
+
+ l_ST = ((l_Stlm >> 4) & 0x3);
+ l_SP = (l_Stlm >> 6) & 0x1;
+
+ l_Ptlm_size = (l_SP + 1) * 2;
+ l_quotient = l_Ptlm_size + l_ST;
+
+ l_tot_num_tp_remaining = p_header_size % l_quotient;
+
+ if (l_tot_num_tp_remaining != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
+ return OPJ_FALSE;
+ }
+ /* FIXME Do not care of this at the moment since only local variables are set here */
+ /*
+ for
+ (i = 0; i < l_tot_num_tp; ++i)
+ {
+ opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
+ p_header_data += l_ST;
+ opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
+ p_header_data += l_Ptlm_size;
+ }*/
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a PLM marker (Packet length, main header marker)
+ *
+ * @param p_header_data the data contained in the TLM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the TLM marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_plm ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
+ return OPJ_FALSE;
+ }
+ /* Do not care of this at the moment since only local variables are set here */
+ /*
+ opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
+ ++p_header_data;
+ --p_header_size;
+
+ while
+ (p_header_size > 0)
+ {
+ opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
+ ++p_header_data;
+ p_header_size -= (1+l_Nplm);
+ if
+ (p_header_size < 0)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
+ return false;
+ }
+ for
+ (i = 0; i < l_Nplm; ++i)
+ {
+ opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
+ ++p_header_data;
+ // take only the last seven bytes
+ l_packet_len |= (l_tmp & 0x7f);
+ if
+ (l_tmp & 0x80)
+ {
+ l_packet_len <<= 7;
+ }
+ else
+ {
+ // store packet length and proceed to next packet
+ l_packet_len = 0;
+ }
+ }
+ if
+ (l_packet_len != 0)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
+ return false;
+ }
+ }
+ */
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a PLT marker (Packet length, tile-part header)
+ *
+ * @param p_header_data the data contained in the PLT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the PLT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_plt ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
+ ++p_header_data;
+ --p_header_size;
+
+ for (i = 0; i < p_header_size; ++i) {
+ opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
+ ++p_header_data;
+ /* take only the last seven bytes */
+ l_packet_len |= (l_tmp & 0x7f);
+ if (l_tmp & 0x80) {
+ l_packet_len <<= 7;
+ }
+ else {
+ /* store packet length and proceed to next packet */
+ l_packet_len = 0;
+ }
+ }
+
+ if (l_packet_len != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+#if 0
+OPJ_BOOL j2k_read_ppm_v2 (
+ opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ struct opj_event_mgr * p_manager
+ )
+{
+
+ opj_cp_t *l_cp = 00;
+ OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ l_cp->ppm = 1;
+
+ opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
+ ++p_header_data;
+ --p_header_size;
+
+ /* First PPM marker */
+ if (l_Z_ppm == 0) {
+ if (p_header_size < 4) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
+ p_header_data+=4;
+ p_header_size-=4;
+
+ /* First PPM marker: Initialization */
+ l_cp->ppm_len = l_N_ppm;
+ l_cp->ppm_data_size = 0;
+
+ l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
+ if (l_cp->ppm_buffer == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
+ return OPJ_FALSE;
+ }
+ memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
+
+ l_cp->ppm_data = l_cp->ppm_buffer;
+ }
+
+ while (1) {
+ if (l_cp->ppm_data_size == l_cp->ppm_len) {
+ if (p_header_size >= 4) {
+ /* read a N_ppm */
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
+ p_header_data+=4;
+ p_header_size-=4;
+ l_cp->ppm_len += l_N_ppm ;
+
+ OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
+ if (! new_ppm_buffer) {
+ opj_free(l_cp->ppm_buffer);
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm_len = 0;
+ l_cp->ppm_data = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
+ return OPJ_FALSE;
+ }
+ l_cp->ppm_buffer = new_ppm_buffer;
+ memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
+ l_cp->ppm_data = l_cp->ppm_buffer;
+ }
+ else {
+ return OPJ_FALSE;
+ }
+ }
+
+ l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
+
+ if (l_remaining_data <= p_header_size) {
+ /* we must store less information than available in the packet */
+ memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
+ l_cp->ppm_data_size = l_cp->ppm_len;
+ p_header_size -= l_remaining_data;
+ p_header_data += l_remaining_data;
+ }
+ else {
+ memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
+ l_cp->ppm_data_size += p_header_size;
+ p_header_data += p_header_size;
+ p_header_size = 0;
+ break;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+#endif
+
+OPJ_BOOL j2k_read_ppm_v3 (
+ opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ struct opj_event_mgr * p_manager
+ )
+{
+ opj_cp_t *l_cp = 00;
+ OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* Minimum size of PPM marker is equal to the size of Zppm element */
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ l_cp->ppm = 1;
+
+ opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
+ ++p_header_data;
+ --p_header_size;
+
+ /* First PPM marker */
+ if (l_Z_ppm == 0) {
+ /* We need now at least the Nppm^0 element */
+ if (p_header_size < 4) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
+ p_header_data+=4;
+ p_header_size-=4;
+
+ /* sanity check: how much bytes is left for Ippm */
+ if( p_header_size < l_N_ppm )
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm = 0; /* do not use PPM */
+ return OPJ_FALSE;
+ }
+
+ /* First PPM marker: Initialization */
+ l_cp->ppm_len = l_N_ppm;
+ l_cp->ppm_data_read = 0;
+
+ l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
+ l_cp->ppm_buffer = l_cp->ppm_data;
+ if (l_cp->ppm_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
+ return OPJ_FALSE;
+ }
+ memset(l_cp->ppm_data,0,l_cp->ppm_len);
+
+ l_cp->ppm_data_current = l_cp->ppm_data;
+
+ /*l_cp->ppm_data = l_cp->ppm_buffer;*/
+ }
+ else {
+ if (p_header_size < 4) {
+ opj_event_msg(p_manager, EVT_WARNING, "Empty PPM marker\n");
+ return OPJ_TRUE;
+ }
+ else {
+ /* Uncompleted Ippm series in the previous PPM marker?*/
+ if (l_cp->ppm_data_read < l_cp->ppm_len) {
+ /* Get the place where add the remaining Ippm series*/
+ l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
+ l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
+ }
+ else {
+ OPJ_BYTE *new_ppm_data;
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
+ p_header_data+=4;
+ p_header_size-=4;
+
+ /* sanity check: how much bytes is left for Ippm */
+ if( p_header_size < l_N_ppm )
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm = 0; /* do not use PPM */
+ return OPJ_FALSE;
+ }
+ /* Increase the size of ppm_data to add the new Ippm series*/
+ assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
+ new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
+ if (! new_ppm_data) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n");
+ return OPJ_FALSE;
+ }
+ l_cp->ppm_data = new_ppm_data;
+ l_cp->ppm_buffer = l_cp->ppm_data;
+
+ /* Keep the position of the place where concatenate the new series*/
+ l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
+ l_cp->ppm_len += l_N_ppm;
+ }
+ }
+ }
+
+ l_remaining_data = p_header_size;
+
+ while (l_remaining_data >= l_N_ppm) {
+ /* read a complete Ippm series*/
+ memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
+ p_header_size -= l_N_ppm;
+ p_header_data += l_N_ppm;
+
+ l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
+
+ if (p_header_size)
+ {
+ if (p_header_size < 4) return OPJ_FALSE;
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
+ p_header_data+=4;
+ p_header_size-=4;
+ }
+ else {
+ l_remaining_data = p_header_size;
+ break;
+ }
+
+ l_remaining_data = p_header_size;
+
+ /* Next Ippm series is a complete series ?*/
+ if (l_remaining_data >= l_N_ppm) {
+ OPJ_BYTE *new_ppm_data;
+ /* Increase the size of ppm_data to add the new Ippm series*/
+ assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
+ new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
+ if (! new_ppm_data) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");
+ return OPJ_FALSE;
+ }
+ l_cp->ppm_data = new_ppm_data;
+ l_cp->ppm_buffer = l_cp->ppm_data;
+
+ /* Keep the position of the place where concatenate the new series */
+ l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
+ l_cp->ppm_len += l_N_ppm;
+ }
+
+ }
+
+ /* Need to read an incomplete Ippm series*/
+ if (l_remaining_data) {
+ OPJ_BYTE *new_ppm_data;
+ assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
+ new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
+ if (! new_ppm_data) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n");
+ return OPJ_FALSE;
+ }
+ l_cp->ppm_data = new_ppm_data;
+ l_cp->ppm_buffer = l_cp->ppm_data;
+
+ /* Keep the position of the place where concatenate the new series*/
+ l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
+ l_cp->ppm_len += l_N_ppm;
+
+ /* Read incomplete Ippm series*/
+ memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
+ p_header_size -= l_remaining_data;
+ p_header_data += l_remaining_data;
+
+ l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
+ }
+
+#ifdef CLEAN_MSD
+
+ if (l_cp->ppm_data_size == l_cp->ppm_len) {
+ if (p_header_size >= 4) {
+ /* read a N_ppm*/
+ opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
+ p_header_data+=4;
+ p_header_size-=4;
+ l_cp->ppm_len += l_N_ppm ;
+
+ OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
+ if (! new_ppm_buffer) {
+ opj_free(l_cp->ppm_buffer);
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
+ return OPJ_FALSE;
+ }
+ l_cp->ppm_buffer = new_ppm_buffer;
+ memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
+
+ l_cp->ppm_data = l_cp->ppm_buffer;
+ }
+ else {
+ return OPJ_FALSE;
+ }
+ }
+
+ l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
+
+ if (l_remaining_data <= p_header_size) {
+ /* we must store less information than available in the packet */
+ memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
+ l_cp->ppm_data_size = l_cp->ppm_len;
+ p_header_size -= l_remaining_data;
+ p_header_data += l_remaining_data;
+ }
+ else {
+ memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
+ l_cp->ppm_data_size += p_header_size;
+ p_header_data += p_header_size;
+ p_header_size = 0;
+ break;
+ }
+ }
+#endif
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a PPT marker (Packed packet headers, tile-part header)
+ *
+ * @param p_header_data the data contained in the PPT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the PPT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_ppt ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_Z_ppt;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* We need to have the Z_ppt element at minimum */
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ if (l_cp->ppm){
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
+ return OPJ_FALSE;
+ }
+
+ l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
+ l_tcp->ppt = 1;
+
+ opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
+ ++p_header_data;
+ --p_header_size;
+
+ /* Allocate buffer to read the packet header */
+ if (l_Z_ppt == 0) {
+ /* First PPT marker */
+ l_tcp->ppt_data_size = 0;
+ l_tcp->ppt_len = p_header_size;
+
+ opj_free(l_tcp->ppt_buffer);
+ l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
+ if (l_tcp->ppt_buffer == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ return OPJ_FALSE;
+ }
+ l_tcp->ppt_data = l_tcp->ppt_buffer;
+
+ /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
+ }
+ else {
+ OPJ_BYTE *new_ppt_buffer;
+ l_tcp->ppt_len += p_header_size;
+
+ new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len);
+ if (! new_ppt_buffer) {
+ opj_free(l_tcp->ppt_buffer);
+ l_tcp->ppt_buffer = NULL;
+ l_tcp->ppt_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ return OPJ_FALSE;
+ }
+ l_tcp->ppt_buffer = new_ppt_buffer;
+ l_tcp->ppt_data = l_tcp->ppt_buffer;
+
+ memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
+ }
+
+ /* Read packet header from buffer */
+ memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
+
+ l_tcp->ppt_data_size += p_header_size;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_tlm( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_tlm_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
+
+ if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ /* change the way data is written to avoid seeking if possible */
+ /* TODO */
+ p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
+
+ opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
+ ++l_current_data;
+
+ /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_sot( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ const opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
+ p_data += 2;
+
+ opj_write_bytes(p_data,10,2); /* Lsot */
+ p_data += 2;
+
+ opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
+ p_data += 2;
+
+ /* Psot */
+ p_data += 4;
+
+ opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
+ ++p_data;
+
+ opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
+ ++p_data;
+
+ /* UniPG>> */
+#ifdef USE_JPWL
+ /* update markers struct */
+/*
+ OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
+*/
+ assert( 0 && "TODO" );
+#endif /* USE_JPWL */
+
+ * p_data_written = 12;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager )
+{
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_tot_len, l_num_parts = 0;
+ OPJ_UINT32 l_current_part;
+ OPJ_UINT32 l_tile_x,l_tile_y;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
+ if (p_header_size != 8) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
+ p_header_data+=2;
+
+ /* testcase 2.pdf.SIGFPE.706.1112 */
+ if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);
+ return OPJ_FALSE;
+ }
+
+ l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
+ l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
+ l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+
+ OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
+ static OPJ_UINT32 backup_tileno = 0;
+
+ /* tileno is negative or larger than the number of tiles!!! */
+ if (tileno > (l_cp->tw * l_cp->th)) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "JPWL: bad tile number (%d out of a maximum of %d)\n",
+ tileno, (l_cp->tw * l_cp->th));
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ tileno = backup_tileno;
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
+ "- setting tile number to %d\n",
+ tileno);
+ }
+
+ /* keep your private count of tiles */
+ backup_tileno++;
+ };
+#endif /* USE_JPWL */
+
+ /* look for the tile in the list of already processed tile (in parts). */
+ /* Optimization possible here with a more complex data structure and with the removing of tiles */
+ /* since the time taken by this function can only grow at the time */
+
+ opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
+ p_header_data+=4;
+
+ /* PSot should be equal to zero or >=14 or <= 2^32-1 */
+ if ((l_tot_len !=0 ) && (l_tot_len < 14) )
+ {
+ if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/
+ {
+ opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len);
+ }
+ else
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
+ return OPJ_FALSE;
+ }
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+
+ /* totlen is negative or larger than the bytes left!!! */
+ if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
+ opj_event_msg(p_manager, EVT_ERROR,
+ "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
+ l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ l_tot_len = 0;
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
+ "- setting Psot to %d => assuming it is the last tile\n",
+ l_tot_len);
+ }
+ };
+#endif /* USE_JPWL */
+
+ /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
+ if (!l_tot_len) {
+ opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
+ "we assuming it is the last tile-part of the codestream.\n");
+ p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
+ }
+
+ opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
+ ++p_header_data;
+
+ opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
+ ++p_header_data;
+
+ if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
+ /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
+ * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
+ if (l_tcp->m_nb_tile_parts) {
+ if (l_current_part >= l_tcp->m_nb_tile_parts){
+ opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
+ "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
+ // p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
+ // return OPJ_FALSE;
+
+ l_num_parts++;
+ }
+ }
+ if( l_current_part >= l_num_parts ) {
+ /* testcase 451.pdf.SIGSEGV.ce9.3723 */
+ opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
+ "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts );
+ p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
+ return OPJ_FALSE;
+ }
+ l_tcp->m_nb_tile_parts = l_num_parts;
+ }
+
+ /* If know the number of tile part header we will check if we didn't read the last*/
+ if (l_tcp->m_nb_tile_parts) {
+ if (l_tcp->m_nb_tile_parts == (l_current_part+1)) {
+ p_j2k->m_specific_param.m_decoder.m_can_decode = 0; /* Process the last tile-part header*/
+ }
+ }
+
+ if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
+ /* Keep the size of data to skip after this marker */
+ p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
+ }
+ else {
+ /* FIXME: need to be computed from the number of bytes remaining in the codestream */
+ p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
+ }
+
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
+
+ /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
+ if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
+ p_j2k->m_specific_param.m_decoder.m_skip_data =
+ (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
+ || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
+ || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
+ || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
+ }
+ else {
+ assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
+ p_j2k->m_specific_param.m_decoder.m_skip_data =
+ (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
+ }
+
+ /* Index */
+ if (p_j2k->cstr_index)
+ {
+ assert(p_j2k->cstr_index->tile_index != 00);
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
+
+ if (l_num_parts != 0){
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
+
+ if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
+ (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
+ }
+ else {
+ opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t));
+ if (! new_tp_index) {
+ opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
+ }
+ }
+ else{
+ /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
+
+ if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
+ (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
+ sizeof(opj_tp_index_t));
+ }
+
+ if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
+ opj_tp_index_t *new_tp_index;
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1;
+ new_tp_index = (opj_tp_index_t *) opj_realloc(
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));
+ if (! new_tp_index) {
+ opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
+ }
+ }
+
+ }
+
+ }
+
+ /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
+ /* if (p_j2k->cstr_info) {
+ if (l_tcp->first) {
+ if (tileno == 0) {
+ p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
+ }
+
+ p_j2k->cstr_info->tile[tileno].tileno = tileno;
+ p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
+ p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
+ p_j2k->cstr_info->tile[tileno].num_tps = numparts;
+
+ if (numparts) {
+ p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
+ }
+ else {
+ p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
+ }
+ }
+ else {
+ p_j2k->cstr_info->tile[tileno].end_pos += totlen;
+ }
+
+ p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
+ p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
+ p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
+ }*/
+ return OPJ_TRUE;
+ }
+
+OPJ_BOOL opj_j2k_write_sod( opj_j2k_t *p_j2k,
+ opj_tcd_t * p_tile_coder,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ const opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_codestream_info_t *l_cstr_info = 00;
+ OPJ_UINT32 l_remaining_data;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
+ p_data += 2;
+
+ /* make room for the EOF marker */
+ l_remaining_data = p_total_data_size - 4;
+
+ /* update tile coder */
+ p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
+ p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
+
+ /* INDEX >> */
+ /* TODO mergeV2: check this part which use cstr_info */
+ /*l_cstr_info = p_j2k->cstr_info;
+ if (l_cstr_info) {
+ if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
+ //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
+ l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
+ }
+ else {*/
+ /*
+ TODO
+ if
+ (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
+ {
+ cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
+ }*/
+ /*}*/
+ /* UniPG>> */
+#ifdef USE_JPWL
+ /* update markers struct */
+ /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
+*/
+ assert( 0 && "TODO" );
+#endif /* USE_JPWL */
+ /* <<UniPG */
+ /*}*/
+ /* << INDEX */
+
+ if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
+ p_tile_coder->tcd_image->tiles->packno = 0;
+ if (l_cstr_info) {
+ l_cstr_info->packno = 0;
+ }
+ }
+
+ *p_data_written = 0;
+
+ if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
+ return OPJ_FALSE;
+ }
+
+ *p_data_written += 2;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_SIZE_T l_current_read_size;
+ opj_codestream_index_t * l_cstr_index = 00;
+ OPJ_BYTE ** l_current_data = 00;
+ opj_tcp_t * l_tcp = 00;
+ OPJ_UINT32 * l_tile_len = 00;
+ OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
+
+ if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
+ /* opj_stream_get_number_byte_left returns OPJ_OFF_T
+ // but we are in the last tile part,
+ // so its result will fit on OPJ_UINT32 unless we find
+ // a file with a single tile part of more than 4 GB...*/
+ p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
+ }
+ else {
+ /* Check to avoid pass the limit of OPJ_UINT32 */
+ if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 )
+ p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
+ else {
+ /* MSD: case commented to support empty SOT marker (PHR data) */
+ }
+ }
+
+ l_current_data = &(l_tcp->m_data);
+ l_tile_len = &l_tcp->m_data_size;
+
+ /* Patch to support new PHR data */
+ if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
+ if (! *l_current_data) {
+ /* LH: oddly enough, in this path, l_tile_len!=0.
+ * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
+ */
+ *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
+ }
+ else {
+ OPJ_BYTE *l_new_current_data = NULL;
+ //BUGID: 0056005 and 0056022.
+ //test file: 366683_fuzz-asan_heap-oob_6bae99_3155_5245.pdf and fuzz-12.pdf.
+ if ((OPJ_UINT32)-1 - p_j2k->m_specific_param.m_decoder.m_sot_length >= *l_tile_len)
+ l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
+ if (! l_new_current_data) {
+ opj_free(*l_current_data);
+ /*nothing more is done as l_current_data will be set to null, and just
+ afterward we enter in the error path
+ and the actual tile_len is updated (committed) at the end of the
+ function. */
+ }
+ *l_current_data = l_new_current_data;
+ }
+
+ if (*l_current_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ l_sot_length_pb_detected = OPJ_TRUE;
+ }
+
+ /* Index */
+ l_cstr_index = p_j2k->cstr_index;
+ if (l_cstr_index) {
+ OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
+
+ OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
+ l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
+ l_current_pos;
+ l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
+ l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
+
+ if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
+ l_cstr_index,
+ J2K_MS_SOD,
+ l_current_pos,
+ p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
+ return OPJ_FALSE;
+ }
+
+ /*l_cstr_index->packno = 0;*/
+ }
+
+ /* Patch to support new PHR data */
+ if (!l_sot_length_pb_detected) {
+ l_current_read_size = opj_stream_read_data(
+ p_stream,
+ *l_current_data + *l_tile_len,
+ p_j2k->m_specific_param.m_decoder.m_sot_length,
+ p_manager);
+ }
+ else
+ {
+ l_current_read_size = 0;
+ }
+
+ if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+ }
+
+ *l_tile_len += (OPJ_UINT32)l_current_read_size;
+
+ return OPJ_TRUE;
+}
+
+ OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_UINT32 nb_comps,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_rgn_size;
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+ OPJ_UINT32 l_comp_room;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_tile_no];
+ l_tccp = &l_tcp->tccps[p_comp_no];
+
+ if (nb_comps <= 256) {
+ l_comp_room = 1;
+ }
+ else {
+ l_comp_room = 2;
+ }
+
+ l_rgn_size = 6 + l_comp_room;
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
+ l_current_data+=l_comp_room;
+
+ opj_write_bytes(l_current_data, 0,1); /* Srgn */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1); /* SPrgn */
+ ++l_current_data;
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_eoc( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
+
+/* UniPG>> */
+#ifdef USE_JPWL
+ /* update markers struct */
+ /*
+ OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
+*/
+#endif /* USE_JPWL */
+
+ if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
+ return OPJ_FALSE;
+ }
+
+ if ( ! opj_stream_flush(p_stream,p_manager) ) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a RGN marker (Region Of Interest)
+ *
+ * @param p_header_data the data contained in the POC box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the POC marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_nb_comp;
+ opj_image_t * l_image = 00;
+
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
+
+ /* preconditions*/
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_nb_comp = l_image->numcomps;
+
+ if (l_nb_comp <= 256) {
+ l_comp_room = 1; }
+ else {
+ l_comp_room = 2; }
+
+ if (p_header_size != 2 + l_comp_room) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
+ p_header_data+=l_comp_room;
+ opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
+ ++p_header_data;
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+ /* totlen is negative or larger than the bytes left!!! */
+ if (l_comp_room >= l_nb_comp) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "JPWL: bad component number in RGN (%d when there are only %d)\n",
+ l_comp_room, l_nb_comp);
+ if (!JPWL_ASSUME || JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ }
+ };
+#endif /* USE_JPWL */
+
+ /* testcase 3635.pdf.asan.77.2930 */
+ if (l_comp_no >= l_nb_comp) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "bad component number in RGN (%d when there are only %d)\n",
+ l_comp_no, l_nb_comp);
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
+ ++p_header_data;
+
+ return OPJ_TRUE;
+
+}
+
+OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp)
+{
+ return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
+}
+
+OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp)
+{
+ (void)p_tcp;
+ return 0;
+}
+
+OPJ_BOOL opj_j2k_update_rates( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ opj_cp_t * l_cp = 00;
+ opj_image_t * l_image = 00;
+ opj_tcp_t * l_tcp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+
+ OPJ_UINT32 i,j,k;
+ OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
+ OPJ_FLOAT32 * l_rates = 0;
+ OPJ_FLOAT32 l_sot_remove;
+ OPJ_UINT32 l_bits_empty, l_size_pixel;
+ OPJ_UINT32 l_tile_size = 0;
+ OPJ_UINT32 l_last_res;
+ OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_image = p_j2k->m_private_image;
+ l_tcp = l_cp->tcps;
+
+ l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
+ l_size_pixel = l_image->numcomps * l_image->comps->prec;
+ l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(l_cp->th * l_cp->tw);
+
+ if (l_cp->m_specific_param.m_enc.m_tp_on) {
+ l_tp_stride_func = opj_j2k_get_tp_stride;
+ }
+ else {
+ l_tp_stride_func = opj_j2k_get_default_stride;
+ }
+
+ for (i=0;i<l_cp->th;++i) {
+ for (j=0;j<l_cp->tw;++j) {
+ OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers;
+
+ /* 4 borders of the tile rescale on the image if necessary */
+ l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0);
+ l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0);
+ l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);
+ l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);
+
+ l_rates = l_tcp->rates;
+
+ /* Modification of the RATE >> */
+ if (*l_rates) {
+ *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
+ /
+ ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
+ )
+ -
+ l_offset;
+ }
+
+ ++l_rates;
+
+ for (k = 1; k < l_tcp->numlayers; ++k) {
+ if (*l_rates) {
+ *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
+ /
+ ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
+ )
+ -
+ l_offset;
+ }
+
+ ++l_rates;
+ }
+
+ ++l_tcp;
+
+ }
+ }
+
+ l_tcp = l_cp->tcps;
+
+ for (i=0;i<l_cp->th;++i) {
+ for (j=0;j<l_cp->tw;++j) {
+ l_rates = l_tcp->rates;
+
+ if (*l_rates) {
+ *l_rates -= l_sot_remove;
+
+ if (*l_rates < 30) {
+ *l_rates = 30;
+ }
+ }
+
+ ++l_rates;
+
+ l_last_res = l_tcp->numlayers - 1;
+
+ for (k = 1; k < l_last_res; ++k) {
+
+ if (*l_rates) {
+ *l_rates -= l_sot_remove;
+
+ if (*l_rates < *(l_rates - 1) + 10) {
+ *l_rates = (*(l_rates - 1)) + 20;
+ }
+ }
+
+ ++l_rates;
+ }
+
+ if (*l_rates) {
+ *l_rates -= (l_sot_remove + 2.f);
+
+ if (*l_rates < *(l_rates - 1) + 10) {
+ *l_rates = (*(l_rates - 1)) + 20;
+ }
+ }
+
+ ++l_tcp;
+ }
+ }
+
+ l_img_comp = l_image->comps;
+ l_tile_size = 0;
+
+ for (i=0;i<l_image->numcomps;++i) {
+ l_tile_size += ( opj_uint_ceildiv(l_cp->tdx,l_img_comp->dx)
+ *
+ opj_uint_ceildiv(l_cp->tdy,l_img_comp->dy)
+ *
+ l_img_comp->prec
+ );
+
+ ++l_img_comp;
+ }
+
+ l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
+
+ l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
+
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
+ (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
+ if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
+ return OPJ_FALSE;
+ }
+
+ if (l_cp->m_specific_param.m_enc.m_cinema) {
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
+ (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
+ if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
+ return OPJ_FALSE;
+ }
+
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
+ }
+
+ return OPJ_TRUE;
+}
+
+#if 0
+OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 i;
+ opj_tcd_t * l_tcd = 00;
+ OPJ_UINT32 l_nb_tiles;
+ opj_tcp_t * l_tcp = 00;
+ OPJ_BOOL l_success;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ l_tcp = p_j2k->m_cp.tcps;
+
+ l_tcd = opj_tcd_create(OPJ_TRUE);
+ if (l_tcd == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ for (i = 0; i < l_nb_tiles; ++i) {
+ if (l_tcp->m_data) {
+ if (! opj_tcd_init_decode_tile(l_tcd, i)) {
+ opj_tcd_destroy(l_tcd);
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
+ /* cleanup */
+
+ if (! l_success) {
+ p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
+ break;
+ }
+ }
+
+ opj_j2k_tcp_destroy(l_tcp);
+ ++l_tcp;
+ }
+
+ opj_tcd_destroy(l_tcd);
+ return OPJ_TRUE;
+}
+#endif
+
+OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_mct_data_group( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 i;
+ opj_simple_mcc_decorrelation_data_t * l_mcc_record;
+ opj_mct_data_t * l_mct_record;
+ opj_tcp_t * l_tcp;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
+ l_mct_record = l_tcp->m_mct_records;
+
+ for (i=0;i<l_tcp->m_nb_mct_records;++i) {
+
+ if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ ++l_mct_record;
+ }
+
+ l_mcc_record = l_tcp->m_mcc_records;
+
+ for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
+
+ if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ ++l_mcc_record;
+ }
+
+ if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+#if 0
+OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 compno;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
+ {
+ if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+#endif
+
+#if 0
+OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 compno;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
+ {
+ if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+#endif
+
+
+OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 compno;
+ const opj_tccp_t *l_tccp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tccp = p_j2k->m_cp.tcps->tccps;
+
+ for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
+ if (l_tccp->roishift) {
+
+ if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ ++l_tccp;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_epc( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ opj_codestream_index_t * l_cstr_index = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_cstr_index = p_j2k->cstr_index;
+ if (l_cstr_index) {
+ l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
+ /* UniPG>> */
+ /* The following adjustment is done to adjust the codestream size */
+ /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
+ /* the first bunch of bytes is not in the codestream */
+ l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
+ /* <<UniPG */
+ }
+
+#ifdef USE_JPWL
+ /* preparation of JPWL marker segments */
+#if 0
+ if(cp->epc_on) {
+
+ /* encode according to JPWL */
+ jpwl_encode(p_j2k, p_stream, image);
+
+ }
+#endif
+ assert( 0 && "TODO" );
+#endif /* USE_JPWL */
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_unk ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ OPJ_UINT32 *output_marker,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_unknown_marker;
+ const opj_dec_memory_marker_handler_t * l_marker_handler;
+ OPJ_UINT32 l_size_unk = 2;
+
+ /* preconditions*/
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
+
+ while(1) {
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* read 2 bytes as the new marker ID*/
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
+
+ if (!(l_unknown_marker < 0xff00)) {
+
+ /* Get the marker handler from the marker ID*/
+ l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
+
+ if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
+ return OPJ_FALSE;
+ }
+ else {
+ if (l_marker_handler->id != J2K_MS_UNK) {
+ /* Add the marker to the codestream index*/
+ if (l_marker_handler->id != J2K_MS_SOT)
+ {
+ OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
+ (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
+ l_size_unk);
+ if (res == OPJ_FALSE) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
+ return OPJ_FALSE;
+ }
+ }
+ break; /* next marker is known and well located */
+ }
+ else
+ l_size_unk += 2;
+ }
+ }
+ }
+
+ *output_marker = l_marker_handler->id ;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_mct_record( opj_j2k_t *p_j2k,
+ opj_mct_data_t * p_mct_record,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 l_mct_size;
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_tmp;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_mct_size = 10 + p_mct_record->m_data_size;
+
+ if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,0,2); /* Zmct */
+ l_current_data += 2;
+
+ /* only one marker atm */
+ l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
+
+ opj_write_bytes(l_current_data,l_tmp,2);
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,0,2); /* Ymct */
+ l_current_data+=2;
+
+ memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a MCT marker (Multiple Component Transform)
+ *
+ * @param p_header_data the data contained in the MCT box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the MCT marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_mct ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i;
+ opj_tcp_t *l_tcp = 00;
+ OPJ_UINT32 l_tmp;
+ OPJ_UINT32 l_indix;
+ opj_mct_data_t * l_mct_data;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+
+ l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
+ &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ if (p_header_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* first marker */
+ opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
+ p_header_data += 2;
+ if (l_tmp != 0) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
+ return OPJ_TRUE;
+ }
+
+ if(p_header_size <= 6) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
+ opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
+ p_header_data += 2;
+
+ l_indix = l_tmp & 0xff;
+ l_mct_data = l_tcp->m_mct_records;
+
+ for (i=0;i<l_tcp->m_nb_mct_records;++i) {
+ if (l_mct_data->m_index == l_indix) {
+ break;
+ }
+ ++l_mct_data;
+ }
+
+ /* NOT FOUND */
+ if (i == l_tcp->m_nb_mct_records) {
+ if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
+ opj_mct_data_t *new_mct_records;
+ l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
+
+ new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
+ if (! new_mct_records) {
+ opj_free(l_tcp->m_mct_records);
+ l_tcp->m_mct_records = NULL;
+ l_tcp->m_nb_max_mct_records = 0;
+ l_tcp->m_nb_mct_records = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
+ return OPJ_FALSE;
+ }
+ l_tcp->m_mct_records = new_mct_records;
+ l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
+ memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
+ }
+
+ l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
+ }
+
+ if (l_mct_data->m_data) {
+ opj_free(l_mct_data->m_data);
+ l_mct_data->m_data = 00;
+ }
+
+ l_mct_data->m_index = l_indix;
+ l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
+ l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
+
+ opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
+ p_header_data+=2;
+ if (l_tmp != 0) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
+ return OPJ_TRUE;
+ }
+
+ p_header_size -= 6;
+
+ l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
+ if (! l_mct_data->m_data) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
+ return OPJ_FALSE;
+ }
+ memcpy(l_mct_data->m_data,p_header_data,p_header_size);
+
+ l_mct_data->m_data_size = p_header_size;
+ ++l_tcp->m_nb_mct_records;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_mcc_record( opj_j2k_t *p_j2k,
+ struct opj_simple_mcc_decorrelation_data * p_mcc_record,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_mcc_size;
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_nb_bytes_for_comp;
+ OPJ_UINT32 l_mask;
+ OPJ_UINT32 l_tmcc;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ if (p_mcc_record->m_nb_comps > 255 ) {
+ l_nb_bytes_for_comp = 2;
+ l_mask = 0x8000;
+ }
+ else {
+ l_nb_bytes_for_comp = 1;
+ l_mask = 0;
+ }
+
+ l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
+ if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
+ {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
+ l_current_data += 2;
+
+ /* first marker */
+ opj_write_bytes(l_current_data,0,2); /* Zmcc */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
+ ++l_current_data;
+
+ /* only one marker atm */
+ opj_write_bytes(l_current_data,0,2); /* Ymcc */
+ l_current_data+=2;
+
+ opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
+ l_current_data+=2;
+
+ opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
+ ++l_current_data;
+
+ opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
+ l_current_data+=2;
+
+ for (i=0;i<p_mcc_record->m_nb_comps;++i) {
+ opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
+ l_current_data+=l_nb_bytes_for_comp;
+ }
+
+ opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
+ l_current_data+=2;
+
+ for (i=0;i<p_mcc_record->m_nb_comps;++i)
+ {
+ opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
+ l_current_data+=l_nb_bytes_for_comp;
+ }
+
+ l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
+
+ if (p_mcc_record->m_decorrelation_array) {
+ l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
+ }
+
+ if (p_mcc_record->m_offset_array) {
+ l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
+ }
+
+ opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
+ l_current_data+=3;
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_mcc ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 i,j;
+ OPJ_UINT32 l_tmp;
+ OPJ_UINT32 l_indix;
+ opj_tcp_t * l_tcp;
+ opj_simple_mcc_decorrelation_data_t * l_mcc_record;
+ opj_mct_data_t * l_mct_data;
+ OPJ_UINT32 l_nb_collections;
+ OPJ_UINT32 l_nb_comps;
+ OPJ_UINT32 l_nb_bytes_by_comp;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
+ &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ if (p_header_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* first marker */
+ opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
+ p_header_data += 2;
+ if (l_tmp != 0) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
+ return OPJ_TRUE;
+ }
+
+ if (p_header_size < 7) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
+ ++p_header_data;
+
+ l_mcc_record = l_tcp->m_mcc_records;
+
+ for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
+ if (l_mcc_record->m_index == l_indix) {
+ break;
+ }
+ ++l_mcc_record;
+ }
+
+ /** NOT FOUND */
+ if (i == l_tcp->m_nb_mcc_records) {
+ if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
+ opj_simple_mcc_decorrelation_data_t *new_mcc_records;
+ l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
+
+ new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
+ l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
+ if (! new_mcc_records) {
+ opj_free(l_tcp->m_mcc_records);
+ l_tcp->m_mcc_records = NULL;
+ l_tcp->m_nb_max_mcc_records = 0;
+ l_tcp->m_nb_mcc_records = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
+ return OPJ_FALSE;
+ }
+ l_tcp->m_mcc_records = new_mcc_records;
+ l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
+ memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
+ }
+ l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
+ }
+ l_mcc_record->m_index = l_indix;
+
+ /* only one marker atm */
+ opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
+ p_header_data+=2;
+ if (l_tmp != 0) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
+ return OPJ_TRUE;
+ }
+
+ opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
+ p_header_data+=2;
+
+ if (l_nb_collections > 1) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
+ return OPJ_TRUE;
+ }
+
+ p_header_size -= 7;
+
+ for (i=0;i<l_nb_collections;++i) {
+ if (p_header_size < 3) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
+ ++p_header_data;
+
+ if (l_tmp != 1) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
+ return OPJ_TRUE;
+ }
+
+ opj_read_bytes(p_header_data,&l_nb_comps,2);
+
+ p_header_data+=2;
+ p_header_size-=3;
+
+ l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
+ l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
+
+ if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
+
+ for (j=0;j<l_mcc_record->m_nb_comps;++j) {
+ opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
+ p_header_data+=l_nb_bytes_by_comp;
+
+ if (l_tmp != j) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
+ return OPJ_TRUE;
+ }
+ }
+
+ opj_read_bytes(p_header_data,&l_nb_comps,2);
+ p_header_data+=2;
+
+ l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
+ l_nb_comps &= 0x7fff;
+
+ if (l_nb_comps != l_mcc_record->m_nb_comps) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
+ return OPJ_TRUE;
+ }
+
+ if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
+
+ for (j=0;j<l_mcc_record->m_nb_comps;++j) {
+ opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
+ p_header_data+=l_nb_bytes_by_comp;
+
+ if (l_tmp != j) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
+ return OPJ_TRUE;
+ }
+ }
+
+ opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
+ p_header_data += 3;
+
+ l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
+ l_mcc_record->m_decorrelation_array = 00;
+ l_mcc_record->m_offset_array = 00;
+
+ l_indix = l_tmp & 0xff;
+ if (l_indix != 0) {
+ l_mct_data = l_tcp->m_mct_records;
+ for (j=0;j<l_tcp->m_nb_mct_records;++j) {
+ if (l_mct_data->m_index == l_indix) {
+ l_mcc_record->m_decorrelation_array = l_mct_data;
+ break;
+ }
+ ++l_mct_data;
+ }
+
+ if (l_mcc_record->m_decorrelation_array == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+ }
+
+ l_indix = (l_tmp >> 8) & 0xff;
+ if (l_indix != 0) {
+ l_mct_data = l_tcp->m_mct_records;
+ for (j=0;j<l_tcp->m_nb_mct_records;++j) {
+ if (l_mct_data->m_index == l_indix) {
+ l_mcc_record->m_offset_array = l_mct_data;
+ break;
+ }
+ ++l_mct_data;
+ }
+
+ if (l_mcc_record->m_offset_array == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+ }
+ }
+
+ if (p_header_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
+ return OPJ_FALSE;
+ }
+
+ ++l_tcp->m_nb_mcc_records;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_mco( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager
+ )
+{
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_mco_size;
+ opj_tcp_t * l_tcp = 00;
+ opj_simple_mcc_decorrelation_data_t * l_mcc_record;
+ OPJ_UINT32 i;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ l_mco_size = 5 + l_tcp->m_nb_mcc_records;
+ if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
+ }
+
+ opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
+ ++l_current_data;
+
+ l_mcc_record = l_tcp->m_mcc_records;
+ for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
+ opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/
+ ++l_current_data;
+
+ ++l_mcc_record;
+ }
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a MCO marker (Multiple Component Transform Ordering)
+ *
+ * @param p_header_data the data contained in the MCO box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the MCO marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_mco ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_tmp, i;
+ OPJ_UINT32 l_nb_stages;
+ opj_tcp_t * l_tcp;
+ opj_tccp_t * l_tccp;
+ opj_image_t * l_image;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
+ &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ if (p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
+ ++p_header_data;
+
+ if (l_nb_stages > 1) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
+ return OPJ_TRUE;
+ }
+
+ if (p_header_size != l_nb_stages + 1) {
+ opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_tccp = l_tcp->tccps;
+
+ for (i=0;i<l_image->numcomps;++i) {
+ l_tccp->m_dc_level_shift = 0;
+ ++l_tccp;
+ }
+
+ if (l_tcp->m_mct_decoding_matrix) {
+ opj_free(l_tcp->m_mct_decoding_matrix);
+ l_tcp->m_mct_decoding_matrix = 00;
+ }
+
+ for (i=0;i<l_nb_stages;++i) {
+ opj_read_bytes(p_header_data,&l_tmp,1);
+ ++p_header_data;
+
+ if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
+{
+ OPJ_UINT32 i;
+ opj_simple_mcc_decorrelation_data_t * l_mcc_record;
+ opj_mct_data_t * l_deco_array, * l_offset_array;
+ OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
+ OPJ_UINT32 l_nb_elem;
+ OPJ_UINT32 * l_offset_data, * l_current_offset_data;
+ opj_tccp_t * l_tccp;
+
+ /* preconditions */
+ assert(p_tcp != 00);
+
+ l_mcc_record = p_tcp->m_mcc_records;
+
+ for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
+ if (l_mcc_record->m_index == p_index) {
+ break;
+ }
+ }
+
+ if (i==p_tcp->m_nb_mcc_records) {
+ /** element discarded **/
+ return OPJ_TRUE;
+ }
+
+ if (l_mcc_record->m_nb_comps != p_image->numcomps) {
+ /** do not support number of comps != image */
+ return OPJ_TRUE;
+ }
+
+ l_deco_array = l_mcc_record->m_decorrelation_array;
+
+ if (l_deco_array) {
+ l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
+ if (l_deco_array->m_data_size != l_data_size) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_elem = p_image->numcomps * p_image->numcomps;
+ l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+ p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
+
+ if (! p_tcp->m_mct_decoding_matrix ) {
+ return OPJ_FALSE;
+ }
+
+ j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
+ }
+
+ l_offset_array = l_mcc_record->m_offset_array;
+
+ if (l_offset_array) {
+ l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
+ if (l_offset_array->m_data_size != l_data_size) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_elem = p_image->numcomps;
+ l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
+ l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
+
+ if (! l_offset_data ) {
+ return OPJ_FALSE;
+ }
+
+ j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
+
+ l_tccp = p_tcp->tccps;
+ l_current_offset_data = l_offset_data;
+
+ for (i=0;i<p_image->numcomps;++i) {
+ l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++);
+ ++l_tccp;
+ }
+
+ opj_free(l_offset_data);
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_cbd_size;
+ OPJ_BYTE * l_current_data = 00;
+ opj_image_t *l_image = 00;
+ opj_image_comp_t * l_comp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
+
+ if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
+ OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
+ if (! new_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
+ }
+
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
+
+ opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
+ l_current_data += 2;
+
+ opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
+ l_current_data+=2;
+
+ l_comp = l_image->comps;
+
+ for (i=0;i<l_image->numcomps;++i) {
+ opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
+ ++l_current_data;
+
+ ++l_comp;
+ }
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a CBD marker (Component bit depth definition)
+ * @param p_header_data the data contained in the CBD box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CBD marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cbd ( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_nb_comp,l_num_comp;
+ OPJ_UINT32 l_comp_def;
+ OPJ_UINT32 i;
+ opj_image_comp_t * l_comp = 00;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ l_num_comp = p_j2k->m_private_image->numcomps;
+
+ if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
+ p_header_data+=2;
+
+ if (l_nb_comp != l_num_comp) {
+ opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
+ return OPJ_FALSE;
+ }
+
+ l_comp = p_j2k->m_private_image->comps;
+ for (i=0;i<l_num_comp;++i) {
+ opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
+ ++p_header_data;
+ l_comp->sgnd = (l_comp_def>>7) & 1;
+ l_comp->prec = (l_comp_def&0x7f) + 1;
+ ++l_comp;
+ }
+
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+/* J2K / JPT decoder interface */
+/* ----------------------------------------------------------------------- */
+
+void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
+{
+ if(j2k && parameters) {
+ j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
+ j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
+
+#ifdef USE_JPWL
+ j2k->m_cp.correct = parameters->jpwl_correct;
+ j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
+ j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
+#endif /* USE_JPWL */
+ }
+}
+
+/* ----------------------------------------------------------------------- */
+/* J2K encoder interface */
+/* ----------------------------------------------------------------------- */
+
+opj_j2k_t* opj_j2k_create_compress(void)
+{
+ opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
+ if (!l_j2k) {
+ return NULL;
+ }
+
+ memset(l_j2k,0,sizeof(opj_j2k_t));
+
+ l_j2k->m_is_decoder = 0;
+ l_j2k->m_cp.m_is_decoder = 0;
+
+ l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
+ if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
+ opj_j2k_destroy(l_j2k);
+ return NULL;
+ }
+
+ l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
+
+ /* validation list creation*/
+ l_j2k->m_validation_list = opj_procedure_list_create();
+ if (! l_j2k->m_validation_list) {
+ opj_j2k_destroy(l_j2k);
+ return NULL;
+ }
+
+ /* execution list creation*/
+ l_j2k->m_procedure_list = opj_procedure_list_create();
+ if (! l_j2k->m_procedure_list) {
+ opj_j2k_destroy(l_j2k);
+ return NULL;
+ }
+
+ return l_j2k;
+}
+
+int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){
+ POC[0].tile = 1;
+ POC[0].resno0 = 0;
+ POC[0].compno0 = 0;
+ POC[0].layno1 = 1;
+ POC[0].resno1 = (OPJ_UINT32)(numres-1);
+ POC[0].compno1 = 3;
+ POC[0].prg1 = OPJ_CPRL;
+ POC[1].tile = 1;
+ POC[1].resno0 = (OPJ_UINT32)(numres-1);
+ POC[1].compno0 = 0;
+ POC[1].layno1 = 1;
+ POC[1].resno1 = (OPJ_UINT32)numres;
+ POC[1].compno1 = 3;
+ POC[1].prg1 = OPJ_CPRL;
+ return 2;
+}
+
+void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)
+{
+ /* Configure cinema parameters */
+ OPJ_FLOAT32 max_rate = 0;
+ OPJ_FLOAT32 temp_rate = 0;
+ int i;
+
+ /* profile (Rsiz) */
+ switch (parameters->cp_cinema){
+ case OPJ_CINEMA2K_24:
+ case OPJ_CINEMA2K_48:
+ parameters->cp_rsiz = OPJ_CINEMA2K;
+ break;
+ case OPJ_CINEMA4K_24:
+ parameters->cp_rsiz = OPJ_CINEMA4K;
+ break;
+ case OPJ_OFF:
+ assert(0);
+ break;
+ }
+
+ /* No tiling */
+ parameters->tile_size_on = OPJ_FALSE;
+ parameters->cp_tdx=1;
+ parameters->cp_tdy=1;
+
+ /* One tile part for each component */
+ parameters->tp_flag = 'C';
+ parameters->tp_on = 1;
+
+ /* Tile and Image shall be at (0,0) */
+ parameters->cp_tx0 = 0;
+ parameters->cp_ty0 = 0;
+ parameters->image_offset_x0 = 0;
+ parameters->image_offset_y0 = 0;
+
+ /* Codeblock size= 32*32 */
+ parameters->cblockw_init = 32;
+ parameters->cblockh_init = 32;
+
+ /* Codeblock style: no mode switch enabled */
+ parameters->mode = 0;
+
+ /* No ROI */
+ parameters->roi_compno = -1;
+
+ /* No subsampling */
+ parameters->subsampling_dx = 1;
+ parameters->subsampling_dy = 1;
+
+ /* 9-7 transform */
+ parameters->irreversible = 1;
+
+ /* Number of layers */
+ if (parameters->tcp_numlayers > 1){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "1 single quality layer"
+ "-> Number of layers forced to 1 (rather than %d)\n",
+ parameters->tcp_numlayers);
+ parameters->tcp_numlayers = 1;
+ }
+
+ /* Resolution levels */
+ switch (parameters->cp_cinema){
+ case OPJ_CINEMA2K_24:
+ case OPJ_CINEMA2K_48:
+ if(parameters->numresolution > 6){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+ "Number of decomposition levels <= 5\n"
+ "-> Number of decomposition levels forced to 5 (rather than %d)\n",
+ parameters->numresolution+1);
+ parameters->numresolution = 6;
+ }
+ break;
+ case OPJ_CINEMA4K_24:
+ if(parameters->numresolution < 2){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+ "Number of decomposition levels >= 1 && <= 6\n"
+ "-> Number of decomposition levels forced to 1 (rather than %d)\n",
+ parameters->numresolution+1);
+ parameters->numresolution = 1;
+ }else if(parameters->numresolution > 7){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+ "Number of decomposition levels >= 1 && <= 6\n"
+ "-> Number of decomposition levels forced to 6 (rather than %d)\n",
+ parameters->numresolution+1);
+ parameters->numresolution = 7;
+ }
+ break;
+ default :
+ break;
+ }
+
+ /* Precincts */
+ parameters->csty |= 0x01;
+ parameters->res_spec = parameters->numresolution-1;
+ for (i = 0; i<parameters->res_spec; i++) {
+ parameters->prcw_init[i] = 256;
+ parameters->prch_init[i] = 256;
+ }
+
+ /* The progression order shall be CPRL */
+ parameters->prog_order = OPJ_CPRL;
+
+ /* Progression order changes for 4K, disallowed for 2K */
+ if (parameters->cp_cinema == OPJ_CINEMA4K_24) {
+ parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
+ } else {
+ parameters->numpocs = 0;
+ }
+
+ /* Limited bit-rate */
+ parameters->cp_disto_alloc = 1;
+ switch (parameters->cp_cinema){
+ case OPJ_CINEMA2K_24:
+ case OPJ_CINEMA4K_24:
+ max_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+ (OPJ_FLOAT32)(CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
+ if (parameters->tcp_rates[0] == 0){
+ parameters->tcp_rates[0] = max_rate;
+ }else{
+ temp_rate =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+ (parameters->tcp_rates[0] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy);
+ if (temp_rate > CINEMA_24_CS ){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "Maximum 1302083 compressed bytes @ 24fps\n"
+ "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
+ parameters->tcp_rates[0], max_rate);
+ parameters->tcp_rates[0]= max_rate;
+ }else{
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile):\n"
+ "INFO : Specified rate (%3.1f) is below the 2k/4k limit @ 24fps.\n",
+ parameters->tcp_rates[0]);
+ }
+ }
+ parameters->max_comp_size = COMP_24_CS;
+ break;
+ case OPJ_CINEMA2K_48:
+ max_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
+ (float)(CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
+ if (parameters->tcp_rates[0] == 0){
+ parameters->tcp_rates[0] = max_rate;
+ }else{
+ temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
+ (parameters->tcp_rates[0] * 8 * (float)image->comps[0].dx * (float)image->comps[0].dy);
+ if (temp_rate > CINEMA_48_CS ){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+ "Maximum 651041 compressed bytes @ 48fps\n"
+ "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
+ parameters->tcp_rates[0], max_rate);
+ parameters->tcp_rates[0]= max_rate;
+ }else{
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile):\n"
+ "INFO : Specified rate (%3.1f) is below the 2k limit @ 48 fps.\n",
+ parameters->tcp_rates[0]);
+ }
+ }
+ parameters->max_comp_size = COMP_48_CS;
+ break;
+ default:
+ break;
+ }
+}
+
+OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager)
+{
+ OPJ_UINT32 i;
+
+ /* Number of components */
+ if (image->numcomps != 3){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+ "3 components"
+ "-> Number of components of input image (%d) is not compliant\n"
+ "-> Non-profile-3 codestream will be generated\n",
+ image->numcomps);
+ return OPJ_FALSE;
+ }
+
+ /* Bitdepth */
+ for (i = 0; i < image->numcomps; i++) {
+ if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){
+ char signed_str[] = "signed";
+ char unsigned_str[] = "unsigned";
+ char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str;
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+ "Precision of each component shall be 12 bits unsigned"
+ "-> At least component %d of input image (%d bits, %s) is not compliant\n"
+ "-> Non-profile-3 codestream will be generated\n",
+ i,image->comps[i].bpp, tmp_str);
+ return OPJ_FALSE;
+ }
+ }
+
+ /* Image size */
+ switch (cinema_mode){
+ case OPJ_CINEMA2K_24:
+ case OPJ_CINEMA2K_48:
+ if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+ "width <= 2048 and height <= 1080\n"
+ "-> Input image size %d x %d is not compliant\n"
+ "-> Non-profile-3 codestream will be generated\n",
+ image->comps[0].w,image->comps[0].h);
+ return OPJ_FALSE;
+ }
+ break;
+ case OPJ_CINEMA4K_24:
+ if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+ "width <= 4096 and height <= 2160\n"
+ "-> Image size %d x %d is not compliant\n"
+ "-> Non-profile-4 codestream will be generated\n",
+ image->comps[0].w,image->comps[0].h);
+ return OPJ_FALSE;
+ }
+ break;
+ default :
+ break;
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
+ opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 i, j, tileno, numpocs_tile;
+ opj_cp_t *cp = 00;
+
+ if(!p_j2k || !parameters || ! image) {
+ return;
+ }
+
+ /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
+ cp = &(p_j2k->m_cp);
+
+ /* set default values for cp */
+ cp->tw = 1;
+ cp->th = 1;
+
+ /* set cinema parameters if required */
+ if (parameters->cp_cinema){
+ opj_j2k_set_cinema_parameters(parameters,image,p_manager);
+ if (!opj_j2k_is_cinema_compliant(image,parameters->cp_cinema,p_manager)) {
+ parameters->cp_rsiz = OPJ_STD_RSIZ;
+ }
+ }
+
+ /*
+ copy user encoding parameters
+ */
+ cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
+ cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size;
+ cp->rsiz = parameters->cp_rsiz;
+ cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u;
+ cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u;
+ cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u;
+
+ /* mod fixed_quality */
+ if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
+ size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
+ cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
+ memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
+ }
+
+ /* tiles */
+ cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
+ cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
+
+ /* tile offset */
+ cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
+ cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
+
+ /* comment string */
+ if(parameters->cp_comment) {
+ cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
+ if(cp->comment) {
+ strcpy(cp->comment, parameters->cp_comment);
+ }
+ }
+
+ /*
+ calculate other encoding parameters
+ */
+
+ if (parameters->tile_size_on) {
+ cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), (OPJ_INT32)cp->tdx);
+ cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), (OPJ_INT32)cp->tdy);
+ } else {
+ cp->tdx = image->x1 - cp->tx0;
+ cp->tdy = image->y1 - cp->ty0;
+ }
+
+ if (parameters->tp_on) {
+ cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
+ cp->m_specific_param.m_enc.m_tp_on = 1;
+ }
+
+#ifdef USE_JPWL
+ /*
+ calculate JPWL encoding parameters
+ */
+
+ if (parameters->jpwl_epc_on) {
+ OPJ_INT32 i;
+
+ /* set JPWL on */
+ cp->epc_on = OPJ_TRUE;
+ cp->info_on = OPJ_FALSE; /* no informative technique */
+
+ /* set EPB on */
+ if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
+ cp->epb_on = OPJ_TRUE;
+
+ cp->hprot_MH = parameters->jpwl_hprot_MH;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
+ cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
+ }
+ /* if tile specs are not specified, copy MH specs */
+ if (cp->hprot_TPH[0] == -1) {
+ cp->hprot_TPH_tileno[0] = 0;
+ cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
+ }
+ for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
+ cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
+ cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
+ cp->pprot[i] = parameters->jpwl_pprot[i];
+ }
+ }
+
+ /* set ESD writing */
+ if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
+ cp->esd_on = OPJ_TRUE;
+
+ cp->sens_size = parameters->jpwl_sens_size;
+ cp->sens_addr = parameters->jpwl_sens_addr;
+ cp->sens_range = parameters->jpwl_sens_range;
+
+ cp->sens_MH = parameters->jpwl_sens_MH;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
+ cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
+ }
+ }
+
+ /* always set RED writing to false: we are at the encoder */
+ cp->red_on = OPJ_FALSE;
+
+ } else {
+ cp->epc_on = OPJ_FALSE;
+ }
+#endif /* USE_JPWL */
+
+ /* initialize the mutiple tiles */
+ /* ---------------------------- */
+ cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
+ if (parameters->numpocs) {
+ /* initialisation of POC */
+ opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
+ /* TODO MSD use the return value*/
+ }
+
+ for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+ opj_tcp_t *tcp = &cp->tcps[tileno];
+ tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
+
+ for (j = 0; j < tcp->numlayers; j++) {
+ if(cp->m_specific_param.m_enc.m_cinema){
+ if (cp->m_specific_param.m_enc.m_fixed_quality) {
+ tcp->distoratio[j] = parameters->tcp_distoratio[j];
+ }
+ tcp->rates[j] = parameters->tcp_rates[j];
+ }else{
+ if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
+ tcp->distoratio[j] = parameters->tcp_distoratio[j];
+ } else {
+ tcp->rates[j] = parameters->tcp_rates[j];
+ }
+ }
+ }
+
+ tcp->csty = (OPJ_UINT32)parameters->csty;
+ tcp->prg = parameters->prog_order;
+ tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
+
+ numpocs_tile = 0;
+ tcp->POC = 0;
+
+ if (parameters->numpocs) {
+ /* initialisation of POC */
+ tcp->POC = 1;
+ for (i = 0; i < parameters->numpocs; i++) {
+ if (tileno + 1 == parameters->POC[i].tile ) {
+ opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
+
+ tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
+ tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
+ tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
+ tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
+ tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
+ tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
+ tcp_poc->tile = parameters->POC[numpocs_tile].tile;
+
+ numpocs_tile++;
+ }
+ }
+
+ tcp->numpocs = numpocs_tile -1 ;
+ }else{
+ tcp->numpocs = 0;
+ }
+
+ tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
+
+ if (parameters->mct_data) {
+
+ OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+ OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
+ OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
+
+ tcp->mct = 2;
+ tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
+ memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
+ memcpy(lTmpBuf,parameters->mct_data,lMctSize);
+
+ tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
+ assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
+
+ tcp->mct_norms = (OPJ_FLOAT64*)
+ opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
+
+ opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
+ opj_free(lTmpBuf);
+
+ for (i = 0; i < image->numcomps; i++) {
+ opj_tccp_t *tccp = &tcp->tccps[i];
+ tccp->m_dc_level_shift = l_dc_shift[i];
+ }
+
+ opj_j2k_setup_mct_encoding(tcp,image);
+ }
+ else {
+ for (i = 0; i < image->numcomps; i++) {
+ opj_tccp_t *tccp = &tcp->tccps[i];
+ opj_image_comp_t * l_comp = &(image->comps[i]);
+
+ if (! l_comp->sgnd) {
+ tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
+ }
+ }
+ }
+
+ for (i = 0; i < image->numcomps; i++) {
+ opj_tccp_t *tccp = &tcp->tccps[i];
+
+ tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
+ tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
+ tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
+ tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
+ tccp->cblksty = (OPJ_UINT32)parameters->mode;
+ tccp->qmfbid = parameters->irreversible ? 0 : 1;
+ tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
+ tccp->numgbits = 2;
+
+ if ((OPJ_INT32)i == parameters->roi_compno) {
+ tccp->roishift = parameters->roi_shift;
+ } else {
+ tccp->roishift = 0;
+ }
+
+ if (parameters->csty & J2K_CCP_CSTY_PRT) {
+ OPJ_INT32 p = 0, it_res;
+ assert( tccp->numresolutions > 0 );
+ for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
+ if (p < parameters->res_spec) {
+
+ if (parameters->prcw_init[p] < 1) {
+ tccp->prcw[it_res] = 1;
+ } else {
+ tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
+ }
+
+ if (parameters->prch_init[p] < 1) {
+ tccp->prch[it_res] = 1;
+ }else {
+ tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
+ }
+
+ } else {
+ OPJ_INT32 res_spec = parameters->res_spec;
+ OPJ_INT32 size_prcw = 0;
+ OPJ_INT32 size_prch = 0;
+
+ assert(res_spec>0); /* issue 189 */
+ size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
+ size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
+
+
+ if (size_prcw < 1) {
+ tccp->prcw[it_res] = 1;
+ } else {
+ tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
+ }
+
+ if (size_prch < 1) {
+ tccp->prch[it_res] = 1;
+ } else {
+ tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
+ }
+ }
+ p++;
+ /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
+ } /*end for*/
+ } else {
+ for (j = 0; j < tccp->numresolutions; j++) {
+ tccp->prcw[j] = 15;
+ tccp->prch[j] = 15;
+ }
+ }
+
+ opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
+ }
+ }
+
+ if (parameters->mct_data) {
+ opj_free(parameters->mct_data);
+ parameters->mct_data = 00;
+ }
+}
+
+static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+{
+ assert(cstr_index != 00);
+
+ /* expand the list? */
+ if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
+ opj_marker_info_t *new_marker;
+ cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum);
+ new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
+ if (! new_marker) {
+ opj_free(cstr_index->marker);
+ cstr_index->marker = NULL;
+ cstr_index->maxmarknum = 0;
+ cstr_index->marknum = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
+ return OPJ_FALSE;
+ }
+ cstr_index->marker = new_marker;
+ }
+
+ /* add the marker */
+ cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
+ cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
+ cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
+ cstr_index->marknum++;
+ return OPJ_TRUE;
+}
+
+static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+{
+ assert(cstr_index != 00);
+ assert(cstr_index->tile_index != 00);
+
+ /* expand the list? */
+ if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
+ opj_marker_info_t *new_marker;
+ cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
+ new_marker = (opj_marker_info_t *) opj_realloc(
+ cstr_index->tile_index[tileno].marker,
+ cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
+ if (! new_marker) {
+ opj_free(cstr_index->tile_index[tileno].marker);
+ cstr_index->tile_index[tileno].marker = NULL;
+ cstr_index->tile_index[tileno].maxmarknum = 0;
+ cstr_index->tile_index[tileno].marknum = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
+ return OPJ_FALSE;
+ }
+ cstr_index->tile_index[tileno].marker = new_marker;
+ }
+
+ /* add the marker */
+ cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
+ cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
+ cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
+ cstr_index->tile_index[tileno].marknum++;
+
+ if (type == J2K_MS_SOT) {
+ OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
+
+ if (cstr_index->tile_index[tileno].tp_index)
+ cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
+
+ }
+ return OPJ_TRUE;
+}
+
+/*
+ * -----------------------------------------------------------------------
+ * -----------------------------------------------------------------------
+ * -----------------------------------------------------------------------
+ */
+
+OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ (void)p_j2k;
+ (void)p_stream;
+ (void)p_manager;
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream,
+ opj_j2k_t* p_j2k,
+ opj_image_t** p_image,
+ opj_event_mgr_t* p_manager )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ /* create an empty image header */
+ p_j2k->m_private_image = opj_image_create0();
+ if (! p_j2k->m_private_image) {
+ return OPJ_FALSE;
+ }
+
+ /* customization of the validation */
+ opj_j2k_setup_decoding_validation(p_j2k);
+
+ /* validation of the parameters codec */
+ if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
+ opj_image_destroy(p_j2k->m_private_image);
+ p_j2k->m_private_image = NULL;
+ return OPJ_FALSE;
+ }
+
+ /* customization of the encoding */
+ opj_j2k_setup_header_reading(p_j2k);
+
+ /* read header */
+ if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+ opj_image_destroy(p_j2k->m_private_image);
+ p_j2k->m_private_image = NULL;
+ return OPJ_FALSE;
+ }
+
+ *p_image = opj_image_create0();
+ if (! (*p_image)) {
+ return OPJ_FALSE;
+ }
+
+ /* Copy codestream image information to the output image */
+ opj_copy_image_header(p_j2k->m_private_image, *p_image);
+
+ /*Allocate and initialize some elements of codestrem index*/
+ if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k)
+{
+ /* preconditions*/
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
+
+ /* DEVELOPER CORNER, add your custom procedures */
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
+
+}
+
+void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k)
+{
+ /* preconditions*/
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
+ opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
+ /* DEVELOPER CORNER, add your custom validation procedure */
+
+}
+
+OPJ_BOOL opj_j2k_mct_validation ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_BOOL l_is_valid = OPJ_TRUE;
+ OPJ_UINT32 i,j;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
+ OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
+
+ for (i=0;i<l_nb_tiles;++i) {
+ if (l_tcp->mct == 2) {
+ opj_tccp_t * l_tccp = l_tcp->tccps;
+ l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
+
+ for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
+ l_is_valid &= ! (l_tccp->qmfbid & 1);
+ ++l_tccp;
+ }
+ }
+ ++l_tcp;
+ }
+ }
+
+ return l_is_valid;
+}
+
+OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_indix = 1;
+ opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
+ opj_simple_mcc_decorrelation_data_t * l_mcc_data;
+ OPJ_UINT32 l_mct_size,l_nb_elem;
+ OPJ_FLOAT32 * l_data, * l_current_data;
+ opj_tccp_t * l_tccp;
+
+ /* preconditions */
+ assert(p_tcp != 00);
+
+ if (p_tcp->mct != 2) {
+ return OPJ_TRUE;
+ }
+
+ if (p_tcp->m_mct_decoding_matrix) {
+ if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
+ opj_mct_data_t *new_mct_records;
+ p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
+
+ new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
+ if (! new_mct_records) {
+ opj_free(p_tcp->m_mct_records);
+ p_tcp->m_mct_records = NULL;
+ p_tcp->m_nb_max_mct_records = 0;
+ p_tcp->m_nb_mct_records = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
+ return OPJ_FALSE;
+ }
+ p_tcp->m_mct_records = new_mct_records;
+ l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
+
+ memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
+ }
+ l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
+
+ if (l_mct_deco_data->m_data) {
+ opj_free(l_mct_deco_data->m_data);
+ l_mct_deco_data->m_data = 00;
+ }
+
+ l_mct_deco_data->m_index = l_indix++;
+ l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
+ l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
+ l_nb_elem = p_image->numcomps * p_image->numcomps;
+ l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
+ l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
+
+ if (! l_mct_deco_data->m_data) {
+ return OPJ_FALSE;
+ }
+
+ j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
+
+ l_mct_deco_data->m_data_size = l_mct_size;
+ ++p_tcp->m_nb_mct_records;
+ }
+
+ if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
+ opj_mct_data_t *new_mct_records;
+ p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
+ new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
+ if (! new_mct_records) {
+ opj_free(p_tcp->m_mct_records);
+ p_tcp->m_mct_records = NULL;
+ p_tcp->m_nb_max_mct_records = 0;
+ p_tcp->m_nb_mct_records = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
+ return OPJ_FALSE;
+ }
+ p_tcp->m_mct_records = new_mct_records;
+ l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
+
+ memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
+
+ if (l_mct_deco_data) {
+ l_mct_deco_data = l_mct_offset_data - 1;
+ }
+ }
+
+ l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
+
+ if (l_mct_offset_data->m_data) {
+ opj_free(l_mct_offset_data->m_data);
+ l_mct_offset_data->m_data = 00;
+ }
+
+ l_mct_offset_data->m_index = l_indix++;
+ l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
+ l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
+ l_nb_elem = p_image->numcomps;
+ l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
+ l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
+
+ if (! l_mct_offset_data->m_data) {
+ return OPJ_FALSE;
+ }
+
+ l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
+ if (! l_data) {
+ opj_free(l_mct_offset_data->m_data);
+ l_mct_offset_data->m_data = 00;
+ return OPJ_FALSE;
+ }
+
+ l_tccp = p_tcp->tccps;
+ l_current_data = l_data;
+
+ for (i=0;i<l_nb_elem;++i) {
+ *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
+ ++l_tccp;
+ }
+
+ j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
+
+ opj_free(l_data);
+
+ l_mct_offset_data->m_data_size = l_mct_size;
+
+ ++p_tcp->m_nb_mct_records;
+
+ if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
+ opj_simple_mcc_decorrelation_data_t *new_mcc_records;
+ p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
+ new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
+ p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
+ if (! new_mcc_records) {
+ opj_free(p_tcp->m_mcc_records);
+ p_tcp->m_mcc_records = NULL;
+ p_tcp->m_nb_max_mcc_records = 0;
+ p_tcp->m_nb_mcc_records = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
+ return OPJ_FALSE;
+ }
+ p_tcp->m_mcc_records = new_mcc_records;
+ l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
+ memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
+
+ }
+
+ l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
+ l_mcc_data->m_decorrelation_array = l_mct_deco_data;
+ l_mcc_data->m_is_irreversible = 1;
+ l_mcc_data->m_nb_comps = p_image->numcomps;
+ l_mcc_data->m_index = l_indix++;
+ l_mcc_data->m_offset_array = l_mct_offset_data;
+ ++p_tcp->m_nb_mcc_records;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ /* add here initialization of cp
+ copy paste of setup_decoder */
+ (void)p_j2k;
+ (void)p_stream;
+ (void)p_manager;
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ /* add here initialization of cp
+ copy paste of setup_encoder */
+ (void)p_j2k;
+ (void)p_stream;
+ (void)p_manager;
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_encoding_validation ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_BOOL l_is_valid = OPJ_TRUE;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ /* STATE checking */
+ /* make sure the state is at 0 */
+ l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
+
+ /* POINTER validation */
+ /* make sure a p_j2k codec is present */
+ l_is_valid &= (p_j2k->m_procedure_list != 00);
+ /* make sure a validation list is present */
+ l_is_valid &= (p_j2k->m_validation_list != 00);
+
+ if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
+ return OPJ_FALSE;
+ }
+
+ if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
+ return OPJ_FALSE;
+ }
+
+ /* PARAMETER VALIDATION */
+ return l_is_valid;
+}
+
+OPJ_BOOL opj_j2k_decoding_validation ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_BOOL l_is_valid = OPJ_TRUE;
+
+ /* preconditions*/
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ /* STATE checking */
+ /* make sure the state is at 0 */
+#ifdef TODO_MSD
+ l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
+#endif
+ l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
+
+ /* POINTER validation */
+ /* make sure a p_j2k codec is present */
+ /* make sure a procedure list is present */
+ l_is_valid &= (p_j2k->m_procedure_list != 00);
+ /* make sure a validation list is present */
+ l_is_valid &= (p_j2k->m_validation_list != 00);
+
+ /* PARAMETER VALIDATION */
+ return l_is_valid;
+}
+
+OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 l_current_marker;
+ OPJ_UINT32 l_marker_size;
+ const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* We enter in the main header */
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
+
+ /* Try to read the SOC marker, the codestream must begin with SOC marker */
+ if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
+ return OPJ_FALSE;
+ }
+
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+
+ /* Try to read until the SOT is detected */
+ while (l_current_marker != J2K_MS_SOT) {
+
+ /* Check if the current marker ID is valid */
+ if (l_current_marker < 0xff00) {
+ opj_event_msg(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
+ return OPJ_FALSE;
+ }
+
+ /* Get the marker handler from the marker ID */
+ l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
+
+ /* Manage case where marker is unknown */
+ if (l_marker_handler->id == J2K_MS_UNK) {
+ if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
+ opj_event_msg(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
+ return OPJ_FALSE;
+ }
+
+ if (l_current_marker == J2K_MS_SOT)
+ break; /* SOT marker is detected main header is completely read */
+ else /* Get the marker handler from the marker ID */
+ l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
+ }
+
+ /* Check if the marker is known and if it is the right place to find it */
+ if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
+ return OPJ_FALSE;
+ }
+
+ /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* read 2 bytes as the marker size */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
+ l_marker_size -= 2; /* Subtract the size of the marker ID already read */
+
+ /* Check if the marker size is compatible with the header data size */
+ if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
+ OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
+ if (! new_header_data) {
+ opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
+ p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
+ }
+
+ /* Try to read the rest of the marker segment from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read the marker segment with the correct marker handler */
+ if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
+ return OPJ_FALSE;
+ }
+
+ /* Add the marker to the codestream index*/
+ if (OPJ_FALSE == opj_j2k_add_mhmarker(
+ p_j2k->cstr_index,
+ l_marker_handler->id,
+ (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
+ l_marker_size + 4 )) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* read 2 bytes as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+
+ opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
+
+ /* Position of the last element if the main header */
+ p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
+
+ /* Next step: read a tile-part header */
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
+ opj_procedure_list_t * p_procedure_list,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
+ OPJ_BOOL l_result = OPJ_TRUE;
+ OPJ_UINT32 l_nb_proc, i;
+
+ /* preconditions*/
+ assert(p_procedure_list != 00);
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
+ l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
+
+ for (i=0;i<l_nb_proc;++i) {
+ l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
+ ++l_procedure;
+ }
+
+ /* and clear the procedure list at the end.*/
+ opj_procedure_list_clear(p_procedure_list);
+ return l_result;
+}
+
+/* FIXME DOC*/
+static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_tcp_t * l_tcp = 00;
+ opj_tcp_t * l_default_tcp = 00;
+ OPJ_UINT32 l_nb_tiles;
+ OPJ_UINT32 i,j;
+ opj_tccp_t *l_current_tccp = 00;
+ OPJ_UINT32 l_tccp_size;
+ OPJ_UINT32 l_mct_size;
+ opj_image_t * l_image;
+ OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
+ opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
+ opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
+ OPJ_UINT32 l_offset;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ l_image = p_j2k->m_private_image;
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ l_tcp = p_j2k->m_cp.tcps;
+ l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
+ l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
+ l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+
+ /* For each tile */
+ for (i=0; i<l_nb_tiles; ++i) {
+ /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
+ l_current_tccp = l_tcp->tccps;
+ /*Copy default coding parameters into the current tile coding parameters*/
+ memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
+ /* Initialize some values of the current tile coding parameters*/
+ l_tcp->ppt = 0;
+ l_tcp->ppt_data = 00;
+ /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
+ l_tcp->tccps = l_current_tccp;
+
+ /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
+ if (l_default_tcp->m_mct_decoding_matrix) {
+ l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
+ if (! l_tcp->m_mct_decoding_matrix ) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
+ }
+
+ /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
+ l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(opj_mct_data_t);
+ l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
+ if (! l_tcp->m_mct_records) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
+
+ /* Copy the mct record data from dflt_tile_cp to the current tile*/
+ l_src_mct_rec = l_default_tcp->m_mct_records;
+ l_dest_mct_rec = l_tcp->m_mct_records;
+
+ for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
+
+ if (l_src_mct_rec->m_data) {
+
+ l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
+ if(! l_dest_mct_rec->m_data) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
+ }
+
+ ++l_src_mct_rec;
+ ++l_dest_mct_rec;
+ }
+
+ /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
+ l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t);
+ l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
+ if (! l_tcp->m_mcc_records) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
+
+ /* Copy the mcc record data from dflt_tile_cp to the current tile*/
+ l_src_mcc_rec = l_default_tcp->m_mcc_records;
+ l_dest_mcc_rec = l_tcp->m_mcc_records;
+
+ for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
+
+ if (l_src_mcc_rec->m_decorrelation_array) {
+ l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records);
+ l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
+ }
+
+ if (l_src_mcc_rec->m_offset_array) {
+ l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records);
+ l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
+ }
+
+ ++l_src_mcc_rec;
+ ++l_dest_mcc_rec;
+ }
+
+ /* Copy all the dflt_tile_compo_cp to the current tile cp */
+ memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
+
+ /* Move to next tile cp*/
+ ++l_tcp;
+ }
+
+ /* Create the current tile decoder*/
+ p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */
+ if (! p_j2k->m_tcd ) {
+ return OPJ_FALSE;
+ }
+
+ if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
+ opj_tcd_destroy(p_j2k->m_tcd);
+ p_j2k->m_tcd = 00;
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
+{
+ const opj_dec_memory_marker_handler_t *e;
+ for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
+ if (e->id == p_id) {
+ break; /* we find a handler corresponding to the marker ID*/
+ }
+ }
+ return e;
+}
+
+void opj_j2k_destroy (opj_j2k_t *p_j2k)
+{
+ if (p_j2k == 00) {
+ return;
+ }
+
+ if (p_j2k->m_is_decoder) {
+
+ if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
+ opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
+ opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
+ p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
+ opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
+ p_j2k->m_specific_param.m_decoder.m_header_data = 00;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
+ }
+ }
+ else {
+
+ if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ }
+ }
+
+ opj_tcd_destroy(p_j2k->m_tcd);
+
+ opj_j2k_cp_destroy(&(p_j2k->m_cp));
+ memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
+
+ opj_procedure_list_destroy(p_j2k->m_procedure_list);
+ p_j2k->m_procedure_list = 00;
+
+ opj_procedure_list_destroy(p_j2k->m_validation_list);
+ p_j2k->m_procedure_list = 00;
+
+ j2k_destroy_cstr_index(p_j2k->cstr_index);
+ p_j2k->cstr_index = NULL;
+
+ opj_image_destroy(p_j2k->m_private_image);
+ p_j2k->m_private_image = NULL;
+
+ opj_image_destroy(p_j2k->m_output_image);
+ p_j2k->m_output_image = NULL;
+
+ opj_free(p_j2k);
+}
+
+void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
+{
+ if (p_cstr_ind) {
+
+ if (p_cstr_ind->marker) {
+ opj_free(p_cstr_ind->marker);
+ p_cstr_ind->marker = NULL;
+ }
+
+ if (p_cstr_ind->tile_index) {
+ OPJ_UINT32 it_tile = 0;
+
+ for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
+
+ if(p_cstr_ind->tile_index[it_tile].packet_index) {
+ opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
+ p_cstr_ind->tile_index[it_tile].packet_index = NULL;
+ }
+
+ if(p_cstr_ind->tile_index[it_tile].tp_index){
+ opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
+ p_cstr_ind->tile_index[it_tile].tp_index = NULL;
+ }
+
+ if(p_cstr_ind->tile_index[it_tile].marker){
+ opj_free(p_cstr_ind->tile_index[it_tile].marker);
+ p_cstr_ind->tile_index[it_tile].marker = NULL;
+
+ }
+ }
+
+ opj_free( p_cstr_ind->tile_index);
+ p_cstr_ind->tile_index = NULL;
+ }
+
+ opj_free(p_cstr_ind);
+ }
+}
+
+void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp)
+{
+ if (p_tcp == 00) {
+ return;
+ }
+
+ if (p_tcp->ppt_buffer != 00) {
+ opj_free(p_tcp->ppt_buffer);
+ p_tcp->ppt_buffer = 00;
+ }
+
+ if (p_tcp->tccps != 00) {
+ opj_free(p_tcp->tccps);
+ p_tcp->tccps = 00;
+ }
+
+ if (p_tcp->m_mct_coding_matrix != 00) {
+ opj_free(p_tcp->m_mct_coding_matrix);
+ p_tcp->m_mct_coding_matrix = 00;
+ }
+
+ if (p_tcp->m_mct_decoding_matrix != 00) {
+ opj_free(p_tcp->m_mct_decoding_matrix);
+ p_tcp->m_mct_decoding_matrix = 00;
+ }
+
+ if (p_tcp->m_mcc_records) {
+ opj_free(p_tcp->m_mcc_records);
+ p_tcp->m_mcc_records = 00;
+ p_tcp->m_nb_max_mcc_records = 0;
+ p_tcp->m_nb_mcc_records = 0;
+ }
+
+ if (p_tcp->m_mct_records) {
+ opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
+ OPJ_UINT32 i;
+
+ for (i=0;i<p_tcp->m_nb_mct_records;++i) {
+ if (l_mct_data->m_data) {
+ opj_free(l_mct_data->m_data);
+ l_mct_data->m_data = 00;
+ }
+
+ ++l_mct_data;
+ }
+
+ opj_free(p_tcp->m_mct_records);
+ p_tcp->m_mct_records = 00;
+ }
+
+ if (p_tcp->mct_norms != 00) {
+ opj_free(p_tcp->mct_norms);
+ p_tcp->mct_norms = 00;
+ }
+
+ opj_j2k_tcp_data_destroy(p_tcp);
+
+}
+
+void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp)
+{
+ if (p_tcp->m_data) {
+ opj_free(p_tcp->m_data);
+ p_tcp->m_data = NULL;
+ p_tcp->m_data_size = 0;
+ }
+}
+
+void opj_j2k_cp_destroy (opj_cp_t *p_cp)
+{
+ OPJ_UINT32 l_nb_tiles;
+ opj_tcp_t * l_current_tile = 00;
+ OPJ_UINT32 i;
+
+ if (p_cp == 00)
+ {
+ return;
+ }
+ if (p_cp->tcps != 00)
+ {
+ l_current_tile = p_cp->tcps;
+ l_nb_tiles = p_cp->th * p_cp->tw;
+
+ for (i = 0; i < l_nb_tiles; ++i)
+ {
+ opj_j2k_tcp_destroy(l_current_tile);
+ ++l_current_tile;
+ }
+ opj_free(p_cp->tcps);
+ p_cp->tcps = 00;
+ }
+ opj_free(p_cp->ppm_buffer);
+ p_cp->ppm_buffer = 00;
+ p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
+ opj_free(p_cp->comment);
+ p_cp->comment = 00;
+ if (! p_cp->m_is_decoder)
+ {
+ opj_free(p_cp->m_specific_param.m_enc.m_matrice);
+ p_cp->m_specific_param.m_enc.m_matrice = 00;
+ }
+}
+
+OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_go_on,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 l_current_marker = J2K_MS_SOT;
+ OPJ_UINT32 l_marker_size;
+ const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
+ opj_tcp_t * l_tcp = NULL;
+ OPJ_UINT32 l_nb_tiles;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* Reach the End Of Codestream ?*/
+ if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
+ l_current_marker = J2K_MS_EOC;
+ }
+ /* We need to encounter a SOT marker (a new tile-part header) */
+ else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
+ return OPJ_FALSE;
+ }
+
+ /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
+ while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
+
+ /* Try to read until the Start Of Data is detected */
+ while (l_current_marker != J2K_MS_SOD) {
+
+ if(opj_stream_get_number_byte_left(p_stream) == 0)
+ {
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+ break;
+ }
+
+ /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from the buffer as the marker size */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
+
+ /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
+ if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) {
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+ break;
+ }
+
+ /* Why this condition? FIXME */
+ if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
+ p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
+ }
+ l_marker_size -= 2; /* Subtract the size of the marker ID already read */
+
+ /* Get the marker handler from the marker ID */
+ l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
+
+ /* Check if the marker is known and if it is the right place to find it */
+ if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
+ return OPJ_FALSE;
+ }
+/* FIXME manage case of unknown marker as in the main header ? */
+
+ /* Check if the marker size is compatible with the header data size */
+ if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
+ OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
+ if (! new_header_data) {
+ opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
+ p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
+ }
+
+ /* Try to read the rest of the marker segment from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ if (!l_marker_handler->handler) {
+ /* See issue #175 */
+ opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
+ return OPJ_FALSE;
+ }
+ /* Read the marker segment with the correct marker handler */
+ if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
+ return OPJ_FALSE;
+ }
+
+ /* Add the marker to the codestream index*/
+ if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
+ p_j2k->cstr_index,
+ l_marker_handler->id,
+ (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
+ l_marker_size + 4 )) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* Keep the position of the last SOT marker read */
+ if ( l_marker_handler->id == J2K_MS_SOT ) {
+ OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
+ if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
+ {
+ p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
+ }
+ }
+
+ if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
+ /* Skip the rest of the tile part header*/
+ if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+ l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
+ }
+ else {
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+ /* Read 2 bytes from the buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+ }
+ if(opj_stream_get_number_byte_left(p_stream) == 0
+ && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
+ break;
+
+ /* If we didn't skip data before, we need to read the SOD marker*/
+ if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
+ /* Try to read the SOD marker and skip data ? FIXME */
+ if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ while(1) //liang
+ {
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ if((l_current_marker & 0xff00) == 0xff00) break;
+ }
+ }
+ }
+ else {
+ /* Indicate we will try to read a new tile-part header*/
+ p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
+ p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+ }
+
+ /* Current marker is the EOC marker ?*/
+ if (l_current_marker == J2K_MS_EOC) {
+ if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
+ p_j2k->m_current_tile_number = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
+ }
+ }
+
+ /* FIXME DOC ???*/
+ if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
+ l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+
+ while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
+ ++p_j2k->m_current_tile_number;
+ ++l_tcp;
+ }
+
+ if (p_j2k->m_current_tile_number == l_nb_tiles) {
+ *p_go_on = OPJ_FALSE;
+ return OPJ_TRUE;
+ }
+ }
+
+ /*FIXME ???*/
+ if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
+ p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+
+ *p_tile_index = p_j2k->m_current_tile_number;
+ *p_go_on = OPJ_TRUE;
+ *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd);
+ *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
+ *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
+ *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
+ *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
+ *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
+
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 l_current_marker;
+ OPJ_BYTE l_data [2];
+ opj_tcp_t * l_tcp;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
+ || (p_tile_index != p_j2k->m_current_tile_number) ) {
+ return OPJ_FALSE;
+ }
+
+ l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
+ if (! l_tcp->m_data) {
+ opj_j2k_tcp_destroy(l_tcp);
+ return OPJ_FALSE;
+ }
+
+ if (! opj_tcd_decode_tile( p_j2k->m_tcd,
+ l_tcp->m_data,
+ l_tcp->m_data_size,
+ p_tile_index,
+ p_j2k->cstr_index) ) {
+ opj_j2k_tcp_destroy(l_tcp);
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
+ return OPJ_FALSE;
+ }
+
+ if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
+ return OPJ_FALSE;
+ }
+
+ /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
+ * we destroy just the data which will be re-read in read_tile_header*/
+ /*opj_j2k_tcp_destroy(l_tcp);
+ p_j2k->m_tcd->tcp = 0;*/
+ opj_j2k_tcp_data_destroy(l_tcp);
+
+ p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
+ p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/
+
+ if(opj_stream_get_number_byte_left(p_stream) == 0
+ && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){
+ return OPJ_TRUE;
+ }
+
+ if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
+ if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_data,&l_current_marker,2);
+
+ if (l_current_marker == J2K_MS_EOC) {
+ p_j2k->m_current_tile_number = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
+ }
+ else if (l_current_marker != J2K_MS_SOT)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
+
+ if(opj_stream_get_number_byte_left(p_stream) == 0) {
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+ return OPJ_TRUE;
+ }
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
+{
+ OPJ_UINT32 i,j,k = 0;
+ OPJ_UINT32 l_width_src,l_height_src;
+ OPJ_UINT32 l_width_dest,l_height_dest;
+ OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
+ OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
+ OPJ_UINT32 l_start_x_dest , l_start_y_dest;
+ OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
+ OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
+
+ opj_image_comp_t * l_img_comp_src = 00;
+ opj_image_comp_t * l_img_comp_dest = 00;
+
+ opj_tcd_tilecomp_t * l_tilec = 00;
+ opj_image_t * l_image_src = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+ OPJ_INT32 * l_dest_ptr;
+ opj_tcd_resolution_t* l_res= 00;
+
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_image_src = p_tcd->image;
+ l_img_comp_src = l_image_src->comps;
+
+ l_img_comp_dest = p_output_image->comps;
+
+ for (i=0; i<l_image_src->numcomps; i++) {
+
+ /* Allocate output component buffer if necessary */
+ if (!l_img_comp_dest->data) {
+
+ l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
+ if (! l_img_comp_dest->data) {
+ return OPJ_FALSE;
+ }
+ }
+
+ /* Copy info from decoded comp image to output image */
+ l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
+
+ /*-----*/
+ /* Compute the precision of the output buffer */
+ l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp_src->prec & 7; /* (%8) */
+ l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
+
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+ /*-----*/
+
+ /* Current tile component size*/
+ /*if (i == 0) {
+ fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
+ l_res->x0, l_res->x1, l_res->y0, l_res->y1);
+ }*/
+
+ l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0);
+ l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0);
+
+ /* Border of the current output component*/
+ l_x0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->x0, (OPJ_INT32)l_img_comp_dest->factor);
+ l_y0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->y0, (OPJ_INT32)l_img_comp_dest->factor);
+ l_x1_dest = l_x0_dest + l_img_comp_dest->w;
+ l_y1_dest = l_y0_dest + l_img_comp_dest->h;
+
+ /*if (i == 0) {
+ fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
+ l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
+ }*/
+
+ /*-----*/
+ /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
+ * of the input buffer (decoded tile component) which will be move
+ * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
+ * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
+ * by this input area.
+ * */
+ assert( l_res->x0 >= 0);
+ assert( l_res->x1 >= 0);
+ if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
+ l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest;
+ l_offset_x0_src = 0;
+
+ if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
+ l_width_dest = l_width_src;
+ l_offset_x1_src = 0;
+ }
+ else {
+ l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ;
+ l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
+ }
+ }
+ else {
+ l_start_x_dest = 0 ;
+ l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0;
+
+ if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
+ l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
+ l_offset_x1_src = 0;
+ }
+ else {
+ l_width_dest = l_img_comp_dest->w ;
+ l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest;
+ }
+ }
+
+ if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
+ l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest;
+ l_offset_y0_src = 0;
+
+ if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
+ l_height_dest = l_height_src;
+ l_offset_y1_src = 0;
+ }
+ else {
+ l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ;
+ l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
+ }
+ }
+ else {
+ l_start_y_dest = 0 ;
+ l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0;
+
+ if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
+ l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
+ l_offset_y1_src = 0;
+ }
+ else {
+ l_height_dest = l_img_comp_dest->h ;
+ l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest;
+ }
+ }
+
+ if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
+ return OPJ_FALSE;
+ }
+ /* testcase 2977.pdf.asan.67.2198 */
+ if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
+ return OPJ_FALSE;
+ }
+ /*-----*/
+
+ /* Compute the input buffer offset */
+ l_start_offset_src = l_offset_x0_src + l_offset_y0_src * (OPJ_INT32)l_width_src;
+ l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
+ l_end_offset_src = l_offset_y1_src * (OPJ_INT32)l_width_src - l_offset_x0_src;
+
+ /* Compute the output buffer offset */
+ l_start_offset_dest = (OPJ_INT32)(l_start_x_dest + l_start_y_dest * l_img_comp_dest->w);
+ l_line_offset_dest = (OPJ_INT32)(l_img_comp_dest->w - l_width_dest);
+
+ /* Move the output buffer to the first place where we will write*/
+ l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
+
+ /*if (i == 0) {
+ fprintf(stdout, "COMPO[%d]:\n",i);
+ fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
+ "\t tile offset:%d, %d, %d, %d\n"
+ "\t buffer offset: %d; %d, %d\n",
+ l_res->x0, l_res->y0, l_width_src, l_height_src,
+ l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
+ l_start_offset_src, l_line_offset_src, l_end_offset_src);
+
+ fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
+ "\t start offset: %d, line offset= %d\n",
+ l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
+ }*/
+
+ switch (l_size_comp) {
+ case 1:
+ {
+ OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
+ l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
+
+ if (l_img_comp_src->sgnd) {
+ for (j = 0 ; j < l_height_dest ; ++j) {
+ for ( k = 0 ; k < l_width_dest ; ++k) {
+ *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
+ }
+
+ l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
+ l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
+ }
+ }
+ else {
+ for ( j = 0 ; j < l_height_dest ; ++j ) {
+ for ( k = 0 ; k < l_width_dest ; ++k) {
+ *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
+ }
+
+ l_dest_ptr+= l_line_offset_dest;
+ l_src_ptr += l_line_offset_src;
+ }
+ }
+
+ l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
+ p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
+ }
+ break;
+ case 2:
+ {
+ OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
+ l_src_ptr += l_start_offset_src;
+
+ if (l_img_comp_src->sgnd) {
+ for (j=0;j<l_height_dest;++j) {
+ for (k=0;k<l_width_dest;++k) {
+ *(l_dest_ptr++) = *(l_src_ptr++);
+ }
+
+ l_dest_ptr+= l_line_offset_dest;
+ l_src_ptr += l_line_offset_src ;
+ }
+ }
+ else {
+ for (j=0;j<l_height_dest;++j) {
+ for (k=0;k<l_width_dest;++k) {
+ *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
+ }
+
+ l_dest_ptr+= l_line_offset_dest;
+ l_src_ptr += l_line_offset_src ;
+ }
+ }
+
+ l_src_ptr += l_end_offset_src;
+ p_data = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ case 4:
+ {
+ OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
+ l_src_ptr += l_start_offset_src;
+
+ for (j=0;j<l_height_dest;++j) {
+ for (k=0;k<l_width_dest;++k) {
+ *(l_dest_ptr++) = (*(l_src_ptr++));
+ }
+
+ l_dest_ptr+= l_line_offset_dest;
+ l_src_ptr += l_line_offset_src ;
+ }
+
+ l_src_ptr += l_end_offset_src;
+ p_data = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ }
+
+ ++l_img_comp_dest;
+ ++l_img_comp_src;
+ ++l_tilec;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
+ opj_event_mgr_t * p_manager )
+{
+ opj_cp_t * l_cp = &(p_j2k->m_cp);
+ opj_image_t * l_image = p_j2k->m_private_image;
+
+ OPJ_UINT32 it_comp;
+ OPJ_INT32 l_comp_x1, l_comp_y1;
+ opj_image_comp_t* l_img_comp = NULL;
+
+ /* Check if we are read the main header */
+ if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
+ opj_event_msg(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
+ return OPJ_FALSE;
+ }
+
+ if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
+ opj_event_msg(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
+
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+
+ return OPJ_TRUE;
+ }
+
+ /* ----- */
+ /* Check if the positions provided by the user are correct */
+
+ /* Left */
+ assert(p_start_x >= 0 );
+ assert(p_start_y >= 0 );
+
+ if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
+ p_start_x, l_image->x1);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_start_x < l_image->x0){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
+ p_start_x, l_image->x0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_image->x0 = l_image->x0;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx;
+ p_image->x0 = (OPJ_UINT32)p_start_x;
+ }
+
+ /* Up */
+ if ((OPJ_UINT32)p_start_y > l_image->y1){
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
+ p_start_y, l_image->y1);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_start_y < l_image->y0){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
+ p_start_y, l_image->y0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_image->y0 = l_image->y0;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy;
+ p_image->y0 = (OPJ_UINT32)p_start_y;
+ }
+
+ /* Right */
+ assert((OPJ_UINT32)p_end_x > 0);
+ assert((OPJ_UINT32)p_end_y > 0);
+ if ((OPJ_UINT32)p_end_x < l_image->x0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
+ p_end_x, l_image->x0);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_end_x > l_image->x1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
+ p_end_x, l_image->x1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_image->x1 = l_image->x1;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
+ p_image->x1 = (OPJ_UINT32)p_end_x;
+ }
+
+ /* Bottom */
+ if ((OPJ_UINT32)p_end_y < l_image->y0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
+ p_end_y, l_image->y0);
+ return OPJ_FALSE;
+ }
+ if ((OPJ_UINT32)p_end_y > l_image->y1){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
+ p_end_y, l_image->y1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+ p_image->y1 = l_image->y1;
+ }
+ else{
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
+ p_image->y1 = (OPJ_UINT32)p_end_y;
+ }
+ /* ----- */
+
+ p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
+
+ l_img_comp = p_image->comps;
+ for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
+ {
+ OPJ_INT32 l_h,l_w;
+
+ l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+ l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
+
+ l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
+ - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
+ if (l_w < 0){
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
+ it_comp, l_w);
+ return OPJ_FALSE;
+ }
+ l_img_comp->w = (OPJ_UINT32)l_w;
+
+ l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
+ - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
+ if (l_h < 0){
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
+ it_comp, l_h);
+ return OPJ_FALSE;
+ }
+ l_img_comp->h = (OPJ_UINT32)l_h;
+
+ l_img_comp++;
+ }
+
+ opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
+ p_image->x0, p_image->y0, p_image->x1, p_image->y1);
+
+ return OPJ_TRUE;
+}
+
+opj_j2k_t* opj_j2k_create_decompress(void)
+{
+ opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
+ if (!l_j2k) {
+ return 00;
+ }
+ memset(l_j2k,0,sizeof(opj_j2k_t));
+
+ l_j2k->m_is_decoder = 1;
+ l_j2k->m_cp.m_is_decoder = 1;
+
+ l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_malloc(sizeof(opj_tcp_t));
+ if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+ memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_t));
+
+ l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
+ if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ l_j2k->m_specific_param.m_decoder.m_header_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
+
+ l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
+
+ l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
+
+ /* codestream index creation */
+ l_j2k->cstr_index = opj_j2k_create_cstr_index();
+
+ /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
+ if (!l_j2k->cstr_index){
+ opj_j2k_destroy(l_j2k);
+ return NULL;
+ }
+
+ l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
+*/
+
+ /* validation list creation */
+ l_j2k->m_validation_list = opj_procedure_list_create();
+ if (! l_j2k->m_validation_list) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ /* execution list creation */
+ l_j2k->m_procedure_list = opj_procedure_list_create();
+ if (! l_j2k->m_procedure_list) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ return l_j2k;
+}
+
+opj_codestream_index_t* opj_j2k_create_cstr_index(void)
+{
+ opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
+ opj_calloc(1,sizeof(opj_codestream_index_t));
+ if (!cstr_index)
+ return NULL;
+
+ cstr_index->maxmarknum = 100;
+ cstr_index->marknum = 0;
+ cstr_index->marker = (opj_marker_info_t*)
+ opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
+ if (!cstr_index-> marker)
+ return NULL;
+
+ cstr_index->tile_index = NULL;
+
+ return cstr_index;
+}
+
+OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no )
+{
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_tile_no];
+ l_tccp = &l_tcp->tccps[p_comp_no];
+
+ /* preconditions again */
+ assert(p_tile_no < (l_cp->tw * l_cp->th));
+ assert(p_comp_no < p_j2k->m_private_image->numcomps);
+
+ if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
+ return 5 + l_tccp->numresolutions;
+ }
+ else {
+ return 5;
+ }
+}
+
+OPJ_BOOL opj_j2k_write_SPCod_SPCoc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_header_size,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 i;
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_header_size != 00);
+ assert(p_manager != 00);
+ assert(p_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_tile_no];
+ l_tccp = &l_tcp->tccps[p_comp_no];
+
+ /* preconditions again */
+ assert(p_tile_no < (l_cp->tw * l_cp->th));
+ assert(p_comp_no <(p_j2k->m_private_image->numcomps));
+
+ if (*p_header_size < 5) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
+ ++p_data;
+
+ opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
+ ++p_data;
+
+ opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
+ ++p_data;
+
+ opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
+ ++p_data;
+
+ opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
+ ++p_data;
+
+ *p_header_size = *p_header_size - 5;
+
+ if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
+
+ if (*p_header_size < l_tccp->numresolutions) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ for (i = 0; i < l_tccp->numresolutions; ++i) {
+ opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
+ ++p_data;
+ }
+
+ *p_header_size = *p_header_size - l_tccp->numresolutions;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_SPCod_SPCoc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 compno,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 i, l_tmp;
+ opj_cp_t *l_cp = NULL;
+ opj_tcp_t *l_tcp = NULL;
+ opj_tccp_t *l_tccp = NULL;
+ OPJ_BYTE * l_current_ptr = NULL;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ /* precondition again */
+ assert(compno < p_j2k->m_private_image->numcomps);
+
+ l_tccp = &l_tcp->tccps[compno];
+ l_current_ptr = p_header_data;
+
+ /* make sure room is sufficient */
+ if (*p_header_size < 5) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
+ ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
+ if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
+ l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
+ return OPJ_FALSE;
+ }
+ ++l_current_ptr;
+
+ /* If user wants to remove more resolutions than the codestream contains, return error */
+ if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
+ "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
+ ++l_current_ptr;
+ l_tccp->cblkw += 2;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
+ ++l_current_ptr;
+ l_tccp->cblkh += 2;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
+ ++l_current_ptr;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
+ ++l_current_ptr;
+
+ *p_header_size = *p_header_size - 5;
+
+ /* use custom precinct size ? */
+ if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
+ if (*p_header_size < l_tccp->numresolutions) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ for (i = 0; i < l_tccp->numresolutions; ++i) {
+ opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
+ ++l_current_ptr;
+ l_tccp->prcw[i] = l_tmp & 0xf;
+ l_tccp->prch[i] = l_tmp >> 4;
+ }
+
+ *p_header_size = *p_header_size - l_tccp->numresolutions;
+ }
+ else {
+ /* set default size for the precinct width and height */
+ for (i = 0; i < l_tccp->numresolutions; ++i) {
+ l_tccp->prcw[i] = 15;
+ l_tccp->prch[i] = 15;
+ }
+ }
+
+#ifdef WIP_REMOVE_MSD
+ /* INDEX >> */
+ if (p_j2k->cstr_info && compno == 0) {
+ OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
+
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
+
+ memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
+ memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
+ }
+ /* << INDEX */
+#endif
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k )
+{
+ /* loop */
+ OPJ_UINT32 i;
+ opj_cp_t *l_cp = NULL;
+ opj_tcp_t *l_tcp = NULL;
+ opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
+ OPJ_UINT32 l_prc_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ l_ref_tccp = &l_tcp->tccps[0];
+ l_copied_tccp = l_ref_tccp + 1;
+ l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
+
+ for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
+ l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
+ l_copied_tccp->cblkw = l_ref_tccp->cblkw;
+ l_copied_tccp->cblkh = l_ref_tccp->cblkh;
+ l_copied_tccp->cblksty = l_ref_tccp->cblksty;
+ l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
+ memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
+ memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
+ ++l_copied_tccp;
+ }
+}
+
+OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no )
+{
+ OPJ_UINT32 l_num_bands;
+
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_tile_no];
+ l_tccp = &l_tcp->tccps[p_comp_no];
+
+ /* preconditions again */
+ assert(p_tile_no < l_cp->tw * l_cp->th);
+ assert(p_comp_no < p_j2k->m_private_image->numcomps);
+
+ l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
+
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
+ return 1 + l_num_bands;
+ }
+ else {
+ return 1 + 2*l_num_bands;
+ }
+}
+
+OPJ_BOOL opj_j2k_write_SQcd_SQcc( opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_tile_no,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_header_size,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 l_header_size;
+ OPJ_UINT32 l_band_no, l_num_bands;
+ OPJ_UINT32 l_expn,l_mant;
+
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_header_size != 00);
+ assert(p_manager != 00);
+ assert(p_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = &l_cp->tcps[p_tile_no];
+ l_tccp = &l_tcp->tccps[p_comp_no];
+
+ /* preconditions again */
+ assert(p_tile_no < l_cp->tw * l_cp->th);
+ assert(p_comp_no <p_j2k->m_private_image->numcomps);
+
+ l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
+
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
+ l_header_size = 1 + l_num_bands;
+
+ if (*p_header_size < l_header_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
+ return OPJ_FALSE;
+ }
+
+ opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
+ ++p_data;
+
+ for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
+ l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
+ opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
+ ++p_data;
+ }
+ }
+ else {
+ l_header_size = 1 + 2*l_num_bands;
+
+ if (*p_header_size < l_header_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
+ return OPJ_FALSE;
+ }
+
+ opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
+ ++p_data;
+
+ for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
+ l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
+ l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
+
+ opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
+ p_data += 2;
+ }
+ }
+
+ *p_header_size = *p_header_size - l_header_size;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE* p_header_data,
+ OPJ_UINT32 * p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* loop*/
+ OPJ_UINT32 l_band_no;
+ opj_cp_t *l_cp = 00;
+ opj_tcp_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+ OPJ_BYTE * l_current_ptr = 00;
+ OPJ_UINT32 l_tmp, l_num_band;
+
+ /* preconditions*/
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ /* come from tile part header or main header ?*/
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ /* precondition again*/
+ assert(p_comp_no < p_j2k->m_private_image->numcomps);
+
+ l_tccp = &l_tcp->tccps[p_comp_no];
+ l_current_ptr = p_header_data;
+
+ if (*p_header_size < 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
+ return OPJ_FALSE;
+ }
+ *p_header_size -= 1;
+
+ opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
+ ++l_current_ptr;
+
+ l_tccp->qntsty = l_tmp & 0x1f;
+ l_tccp->numgbits = l_tmp >> 5;
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
+ l_num_band = 1;
+ }
+ else {
+ l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
+ (*p_header_size) :
+ (*p_header_size) / 2;
+
+ if( l_num_band > OPJ_J2K_MAXBANDS ) {
+ opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
+ "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
+ "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS);
+ /*return OPJ_FALSE;*/
+ }
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+
+ /* if JPWL is on, we check whether there are too many subbands */
+ if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
+ opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad number of subbands in Sqcx (%d)\n",
+ l_num_band);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ l_num_band = 1;
+ opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
+ "- setting number of bands to %d => HYPOTHESIS!!!\n",
+ l_num_band);
+ };
+
+ };
+#endif /* USE_JPWL */
+
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
+ for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
+ opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
+ ++l_current_ptr;
+ if (l_band_no < OPJ_J2K_MAXBANDS){
+ l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
+ l_tccp->stepsizes[l_band_no].mant = 0;
+ }
+ }
+ *p_header_size = *p_header_size - l_num_band;
+ }
+ else {
+ for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
+ opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
+ l_current_ptr+=2;
+ if (l_band_no < OPJ_J2K_MAXBANDS){
+ l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
+ l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
+ }
+ }
+ *p_header_size = *p_header_size - 2*l_num_band;
+ }
+
+ /* Add Antonin : if scalar_derived -> compute other stepsizes */
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
+ for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
+ l_tccp->stepsizes[l_band_no].expn =
+ ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ?
+ (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
+ l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k )
+{
+ OPJ_UINT32 i;
+ opj_cp_t *l_cp = NULL;
+ opj_tcp_t *l_tcp = NULL;
+ opj_tccp_t *l_ref_tccp = NULL;
+ opj_tccp_t *l_copied_tccp = NULL;
+ OPJ_UINT32 l_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ l_ref_tccp = &l_tcp->tccps[0];
+ l_copied_tccp = l_ref_tccp + 1;
+ l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
+
+ for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
+ l_copied_tccp->qntsty = l_ref_tccp->qntsty;
+ l_copied_tccp->numgbits = l_ref_tccp->numgbits;
+ memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
+ ++l_copied_tccp;
+ }
+}
+
+static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream)
+{
+ if (l_default_tile)
+ {
+ OPJ_INT32 compno;
+
+ fprintf(out_stream, "\t default tile {\n");
+ fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
+ fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
+ fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
+ fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
+
+ for (compno = 0; compno < numcomps; compno++) {
+ opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+ OPJ_UINT32 resno;
+ OPJ_INT32 bandno, numbands;
+
+ /* coding style*/
+ fprintf(out_stream, "\t\t comp %d {\n", compno);
+ fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
+ fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
+ fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
+ fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
+ fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
+ fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
+
+ fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
+ for (resno = 0; resno < l_tccp->numresolutions; resno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
+ }
+ fprintf(out_stream, "\n");
+
+ /* quantization style*/
+ fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
+ fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
+ fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
+ for (bandno = 0; bandno < numbands; bandno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
+ l_tccp->stepsizes[bandno].expn);
+ }
+ fprintf(out_stream, "\n");
+
+ /* RGN value*/
+ fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
+
+ fprintf(out_stream, "\t\t }\n");
+ } /*end of component of default tile*/
+ fprintf(out_stream, "\t }\n"); /*end of default tile*/
+ }
+}
+
+void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
+{
+ /* Check if the flag is compatible with j2k file*/
+ if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
+ fprintf(out_stream, "Wrong flag\n");
+ return;
+ }
+
+ /* Dump the image_header */
+ if (flag & OPJ_IMG_INFO){
+ if (p_j2k->m_private_image)
+ j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
+ }
+
+ /* Dump the codestream info from main header */
+ if (flag & OPJ_J2K_MH_INFO){
+ opj_j2k_dump_MH_info(p_j2k, out_stream);
+ }
+ /* Dump all tile/codestream info */
+ if (flag & OPJ_J2K_TCH_INFO){
+ OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ OPJ_UINT32 i;
+ opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
+ for (i=0;i<l_nb_tiles;++i) {
+ opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
+ ++l_tcp;
+ }
+ }
+
+ /* Dump the codestream info of the current tile */
+ if (flag & OPJ_J2K_TH_INFO){
+
+ }
+
+ /* Dump the codestream index from main header */
+ if (flag & OPJ_J2K_MH_IND){
+ opj_j2k_dump_MH_index(p_j2k, out_stream);
+ }
+
+ /* Dump the codestream index of the current tile */
+ if (flag & OPJ_J2K_TH_IND){
+
+ }
+
+}
+
+void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
+{
+ opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
+ OPJ_UINT32 it_marker, it_tile, it_tile_part;
+
+ fprintf(out_stream, "Codestream index from main header: {\n");
+
+ fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
+ "\t Main header end position=%" PRIi64 "\n",
+ cstr_index->main_head_start, cstr_index->main_head_end);
+
+ fprintf(out_stream, "\t Marker list: {\n");
+
+ if (cstr_index->marker){
+ for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
+ fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
+ cstr_index->marker[it_marker].type,
+ cstr_index->marker[it_marker].pos,
+ cstr_index->marker[it_marker].len );
+ }
+ }
+
+ fprintf(out_stream, "\t }\n");
+
+ if (cstr_index->tile_index){
+
+ /* Simple test to avoid to write empty information*/
+ OPJ_UINT32 l_acc_nb_of_tile_part = 0;
+ for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
+ l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
+ }
+
+ if (l_acc_nb_of_tile_part)
+ {
+ fprintf(out_stream, "\t Tile index: {\n");
+
+ for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
+ OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
+
+ fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
+
+ if (cstr_index->tile_index[it_tile].tp_index){
+ for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
+ fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
+ it_tile_part,
+ cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
+ cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
+ cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
+ }
+ }
+
+ if (cstr_index->tile_index[it_tile].marker){
+ for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
+ fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
+ cstr_index->tile_index[it_tile].marker[it_marker].type,
+ cstr_index->tile_index[it_tile].marker[it_marker].pos,
+ cstr_index->tile_index[it_tile].marker[it_marker].len );
+ }
+ }
+ }
+ fprintf(out_stream,"\t }\n");
+ }
+ }
+
+ fprintf(out_stream,"}\n");
+
+}
+
+
+void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
+{
+
+ fprintf(out_stream, "Codestream info from main header: {\n");
+
+ fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
+ fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
+ fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
+ opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
+ fprintf(out_stream, "}\n");
+}
+
+void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
+{
+ char tab[2];
+
+ if (dev_dump_flag){
+ fprintf(stdout, "[DEV] Dump an image_header struct {\n");
+ tab[0] = '\0';
+ }
+ else {
+ fprintf(out_stream, "Image info {\n");
+ tab[0] = '\t';tab[1] = '\0';
+ }
+
+ fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
+ fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
+ fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
+
+ if (img_header->comps){
+ OPJ_UINT32 compno;
+ for (compno = 0; compno < img_header->numcomps; compno++) {
+ fprintf(out_stream, "%s\t component %d {\n", tab, compno);
+ j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
+ fprintf(out_stream,"%s}\n",tab);
+ }
+ }
+
+ fprintf(out_stream, "}\n");
+}
+
+void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
+{
+ char tab[3];
+
+ if (dev_dump_flag){
+ fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
+ tab[0] = '\0';
+ } else {
+ tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
+ }
+
+ fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
+ fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
+ fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
+
+ if (dev_dump_flag)
+ fprintf(out_stream, "}\n");
+}
+
+opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
+{
+ OPJ_UINT32 compno;
+ OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
+ opj_tcp_t *l_default_tile;
+ opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
+ if (!cstr_info)
+ return NULL;
+
+ cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
+
+ cstr_info->tx0 = p_j2k->m_cp.tx0;
+ cstr_info->ty0 = p_j2k->m_cp.ty0;
+ cstr_info->tdx = p_j2k->m_cp.tdx;
+ cstr_info->tdy = p_j2k->m_cp.tdy;
+ cstr_info->tw = p_j2k->m_cp.tw;
+ cstr_info->th = p_j2k->m_cp.th;
+
+ cstr_info->tile_info = NULL; /* Not fill from the main header*/
+
+ l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ cstr_info->m_default_tile_info.csty = l_default_tile->csty;
+ cstr_info->m_default_tile_info.prg = l_default_tile->prg;
+ cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
+ cstr_info->m_default_tile_info.mct = l_default_tile->mct;
+
+ cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
+ if (!cstr_info->m_default_tile_info.tccp_info)
+ {
+ opj_destroy_cstr_info(&cstr_info);
+ return NULL;
+ }
+
+ for (compno = 0; compno < numcomps; compno++) {
+ opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+ opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
+ OPJ_INT32 bandno, numbands;
+
+ /* coding style*/
+ l_tccp_info->csty = l_tccp->csty;
+ l_tccp_info->numresolutions = l_tccp->numresolutions;
+ l_tccp_info->cblkw = l_tccp->cblkw;
+ l_tccp_info->cblkh = l_tccp->cblkh;
+ l_tccp_info->cblksty = l_tccp->cblksty;
+ l_tccp_info->qmfbid = l_tccp->qmfbid;
+ if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS)
+ {
+ memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
+ memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
+ }
+
+ /* quantization style*/
+ l_tccp_info->qntsty = l_tccp->qntsty;
+ l_tccp_info->numgbits = l_tccp->numgbits;
+
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
+ if (numbands < OPJ_J2K_MAXBANDS) {
+ for (bandno = 0; bandno < numbands; bandno++) {
+ l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant;
+ l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn;
+ }
+ }
+
+ /* RGN value*/
+ l_tccp_info->roishift = l_tccp->roishift;
+ }
+
+ return cstr_info;
+}
+
+opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
+{
+ opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
+ opj_calloc(1,sizeof(opj_codestream_index_t));
+ if (!l_cstr_index)
+ return NULL;
+
+ l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
+ l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
+ l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
+
+ l_cstr_index->marknum = p_j2k->cstr_index->marknum;
+ l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
+ if (!l_cstr_index->marker){
+ opj_free( l_cstr_index);
+ return NULL;
+ }
+
+ if (p_j2k->cstr_index->marker)
+ memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
+ else{
+ opj_free(l_cstr_index->marker);
+ l_cstr_index->marker = NULL;
+ }
+
+ l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
+ l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
+ if (!l_cstr_index->tile_index){
+ opj_free( l_cstr_index->marker);
+ opj_free( l_cstr_index);
+ return NULL;
+ }
+
+ if (!p_j2k->cstr_index->tile_index){
+ opj_free(l_cstr_index->tile_index);
+ l_cstr_index->tile_index = NULL;
+ }
+ else {
+ OPJ_UINT32 it_tile = 0;
+ for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
+
+ /* Tile Marker*/
+ l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
+
+ l_cstr_index->tile_index[it_tile].marker =
+ (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
+
+ if (!l_cstr_index->tile_index[it_tile].marker) {
+ OPJ_UINT32 it_tile_free;
+
+ for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
+ opj_free(l_cstr_index->tile_index[it_tile_free].marker);
+ }
+
+ opj_free( l_cstr_index->tile_index);
+ opj_free( l_cstr_index->marker);
+ opj_free( l_cstr_index);
+ return NULL;
+ }
+
+ if (p_j2k->cstr_index->tile_index[it_tile].marker)
+ memcpy( l_cstr_index->tile_index[it_tile].marker,
+ p_j2k->cstr_index->tile_index[it_tile].marker,
+ l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
+ else{
+ opj_free(l_cstr_index->tile_index[it_tile].marker);
+ l_cstr_index->tile_index[it_tile].marker = NULL;
+ }
+
+ /* Tile part index*/
+ l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
+
+ l_cstr_index->tile_index[it_tile].tp_index =
+ (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
+
+ if(!l_cstr_index->tile_index[it_tile].tp_index){
+ OPJ_UINT32 it_tile_free;
+
+ for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
+ opj_free(l_cstr_index->tile_index[it_tile_free].marker);
+ opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
+ }
+
+ opj_free( l_cstr_index->tile_index);
+ opj_free( l_cstr_index->marker);
+ opj_free( l_cstr_index);
+ return NULL;
+ }
+
+ if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
+ memcpy( l_cstr_index->tile_index[it_tile].tp_index,
+ p_j2k->cstr_index->tile_index[it_tile].tp_index,
+ l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
+ }
+ else{
+ opj_free(l_cstr_index->tile_index[it_tile].tp_index);
+ l_cstr_index->tile_index[it_tile].tp_index = NULL;
+ }
+
+ /* Packet index (NOT USED)*/
+ l_cstr_index->tile_index[it_tile].nb_packet = 0;
+ l_cstr_index->tile_index[it_tile].packet_index = NULL;
+
+ }
+ }
+
+ return l_cstr_index;
+}
+
+OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
+{
+ OPJ_UINT32 it_tile=0;
+
+ p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
+ p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
+ if (!p_j2k->cstr_index->tile_index)
+ return OPJ_FALSE;
+
+ for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
+ p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
+ p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
+ p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
+ opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
+ if (!p_j2k->cstr_index->tile_index[it_tile].marker)
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_BOOL l_go_on = OPJ_TRUE;
+ OPJ_UINT32 l_current_tile_no;
+ OPJ_UINT32 l_data_size,l_max_data_size;
+ OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
+ OPJ_UINT32 l_nb_comps;
+ OPJ_BYTE * l_current_data;
+ OPJ_UINT32 nr_tiles = 0;
+
+ l_current_data = (OPJ_BYTE*)opj_malloc(1000);
+ if (! l_current_data) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n");
+ return OPJ_FALSE;
+ }
+ l_max_data_size = 1000;
+
+ while (OPJ_TRUE) {
+ if (! opj_j2k_read_tile_header( p_j2k,
+ &l_current_tile_no,
+ &l_data_size,
+ &l_tile_x0, &l_tile_y0,
+ &l_tile_x1, &l_tile_y1,
+ &l_nb_comps,
+ &l_go_on,
+ p_stream,
+ p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ if (! l_go_on) {
+ break;
+ }
+
+ if (l_data_size > l_max_data_size) {
+ OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
+ if (! l_new_current_data) {
+ opj_free(l_current_data);
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
+ return OPJ_FALSE;
+ }
+ l_current_data = l_new_current_data;
+ l_max_data_size = l_data_size;
+ }
+
+ if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
+ opj_free(l_current_data);
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
+ return OPJ_FALSE;
+ }
+ opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
+
+ if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
+
+ if(opj_stream_get_number_byte_left(p_stream) == 0
+ && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
+ break;
+ if(++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw)
+ break;
+ }
+
+ opj_free(l_current_data);
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
+ */
+static void opj_j2k_setup_decoding (opj_j2k_t *p_j2k)
+{
+ /* preconditions*/
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
+ /* DEVELOPER CORNER, add your custom procedures */
+
+}
+
+/*
+ * Read and decode one tile.
+ */
+static OPJ_BOOL opj_j2k_decode_one_tile ( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_BOOL l_go_on = OPJ_TRUE;
+ OPJ_UINT32 l_current_tile_no;
+ OPJ_UINT32 l_tile_no_to_dec;
+ OPJ_UINT32 l_data_size,l_max_data_size;
+ OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
+ OPJ_UINT32 l_nb_comps;
+ OPJ_BYTE * l_current_data;
+
+ l_current_data = (OPJ_BYTE*)opj_malloc(1000);
+ if (! l_current_data) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n");
+ return OPJ_FALSE;
+ }
+ l_max_data_size = 1000;
+
+ /*Allocate and initialize some elements of codestrem index if not already done*/
+ if( !p_j2k->cstr_index->tile_index)
+ {
+ if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ /* Move into the codestream to the first SOT used to decode the desired tile */
+ l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
+ if (p_j2k->cstr_index->tile_index)
+ if(p_j2k->cstr_index->tile_index->tp_index)
+ {
+ if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
+ /* the index for this tile has not been built,
+ * so move to the last SOT read */
+ if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
+ opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ else{
+ if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
+ if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+ }
+
+ while (OPJ_TRUE) {
+ if (! opj_j2k_read_tile_header( p_j2k,
+ &l_current_tile_no,
+ &l_data_size,
+ &l_tile_x0, &l_tile_y0,
+ &l_tile_x1, &l_tile_y1,
+ &l_nb_comps,
+ &l_go_on,
+ p_stream,
+ p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ if (! l_go_on) {
+ break;
+ }
+
+ if (l_data_size > l_max_data_size) {
+ OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
+ if (! l_new_current_data) {
+ opj_free(l_current_data);
+ l_current_data = NULL;
+ /* TODO: LH: why tile numbering policy used in messages differs from
+ the one used in opj_j2k_decode_tiles() ? */
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+ return OPJ_FALSE;
+ }
+ l_current_data = l_new_current_data;
+ l_max_data_size = l_data_size;
+ }
+
+ if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+
+ if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
+
+ if(l_current_tile_no == l_tile_no_to_dec)
+ {
+ /* move into the codestream to the the first SOT (FIXME or not move?)*/
+ if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+ return OPJ_FALSE;
+ }
+ break;
+ }
+ else {
+ opj_event_msg(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
+ }
+
+ }
+
+ opj_free(l_current_data);
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
+ */
+static void opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k)
+{
+ /* preconditions*/
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
+ /* DEVELOPER CORNER, add your custom procedures */
+
+}
+
+OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
+ opj_stream_private_t * p_stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 compno;
+
+ if (!p_image)
+ return OPJ_FALSE;
+
+ p_j2k->m_output_image = opj_image_create0();
+ if (! (p_j2k->m_output_image)) {
+ return OPJ_FALSE;
+ }
+ opj_copy_image_header(p_image, p_j2k->m_output_image);
+
+ /* customization of the decoding */
+ opj_j2k_setup_decoding(p_j2k);
+
+ /* Decode the codestream */
+ if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+ opj_image_destroy(p_j2k->m_private_image);
+ p_j2k->m_private_image = NULL;
+ return OPJ_FALSE;
+ }
+
+ /* Move data and copy one information from codec to output image*/
+ for (compno = 0; compno < p_image->numcomps; compno++) {
+ p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
+ p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
+#if 0
+ char fn[256];
+ sprintf( fn, "/tmp/%d.raw", compno );
+ FILE *debug = fopen( fn, "wb" );
+ fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug );
+ fclose( debug );
+#endif
+ p_j2k->m_output_image->comps[compno].data = NULL;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager,
+ OPJ_UINT32 tile_index )
+{
+ OPJ_UINT32 compno;
+ OPJ_UINT32 l_tile_x, l_tile_y;
+ opj_image_comp_t* l_img_comp;
+
+ if (!p_image) {
+ opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
+ return OPJ_FALSE;
+ }
+
+ if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
+ opj_event_msg(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
+ return OPJ_FALSE;
+ }
+
+ /* Compute the dimension of the desired tile*/
+ l_tile_x = tile_index % p_j2k->m_cp.tw;
+ l_tile_y = tile_index / p_j2k->m_cp.tw;
+
+ p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
+ if (p_image->x0 < p_j2k->m_private_image->x0)
+ p_image->x0 = p_j2k->m_private_image->x0;
+ p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
+ if (p_image->x1 > p_j2k->m_private_image->x1)
+ p_image->x1 = p_j2k->m_private_image->x1;
+
+ p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
+ if (p_image->y0 < p_j2k->m_private_image->y0)
+ p_image->y0 = p_j2k->m_private_image->y0;
+ p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
+ if (p_image->y1 > p_j2k->m_private_image->y1)
+ p_image->y1 = p_j2k->m_private_image->y1;
+
+ l_img_comp = p_image->comps;
+ for (compno=0; compno < p_image->numcomps; ++compno)
+ {
+ OPJ_INT32 l_comp_x1, l_comp_y1;
+
+ l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
+
+ l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+ l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
+
+ l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor));
+ l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor));
+
+ l_img_comp++;
+ }
+
+ /* Destroy the previous output image*/
+ if (p_j2k->m_output_image)
+ opj_image_destroy(p_j2k->m_output_image);
+
+ /* Create the ouput image from the information previously computed*/
+ p_j2k->m_output_image = opj_image_create0();
+ if (! (p_j2k->m_output_image)) {
+ return OPJ_FALSE;
+ }
+ opj_copy_image_header(p_image, p_j2k->m_output_image);
+
+ p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
+
+ /* customization of the decoding */
+ opj_j2k_setup_decoding_tile(p_j2k);
+
+ /* Decode the codestream */
+ if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+ opj_image_destroy(p_j2k->m_private_image);
+ p_j2k->m_private_image = NULL;
+ return OPJ_FALSE;
+ }
+
+ /* Move data and copy one information from codec to output image*/
+ for (compno = 0; compno < p_image->numcomps; compno++) {
+ p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
+
+ if (p_image->comps[compno].data)
+ opj_free(p_image->comps[compno].data);
+
+ p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
+
+ p_j2k->m_output_image->comps[compno].data = NULL;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 it_comp;
+
+ p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
+
+ if (p_j2k->m_private_image) {
+ if (p_j2k->m_private_image->comps) {
+ if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
+ if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
+ for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
+ OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
+ if ( res_factor >= max_res){
+ opj_event_msg(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_private_image->comps[it_comp].factor = res_factor;
+ }
+ return OPJ_TRUE;
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_nb_tiles;
+ OPJ_UINT32 l_max_tile_size, l_current_tile_size;
+ OPJ_BYTE * l_current_data;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ l_current_data = (OPJ_BYTE*)opj_malloc(1000);
+ if (! l_current_data) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
+ return OPJ_FALSE;
+ }
+ l_max_tile_size = 1000;
+
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ for (i=0;i<l_nb_tiles;++i) {
+ if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
+ if (l_current_tile_size > l_max_tile_size) {
+ OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
+ if (! l_new_current_data) {
+ opj_free(l_current_data);
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
+ return OPJ_FALSE;
+ }
+ l_current_data = l_new_current_data;
+ l_max_tile_size = l_current_tile_size;
+ }
+
+ opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
+
+ if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ opj_free(l_current_data);
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager)
+{
+ /* customization of the encoding */
+ opj_j2k_setup_end_compress(p_j2k);
+
+ if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
+ {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager)
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ p_j2k->m_private_image = opj_image_create0();
+ opj_copy_image_header(p_image, p_j2k->m_private_image);
+
+ /* TODO_MSD: Find a better way */
+ if (p_image->comps) {
+ OPJ_UINT32 it_comp;
+ for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
+ if (p_image->comps[it_comp].data) {
+ p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
+ p_image->comps[it_comp].data = NULL;
+
+ }
+ }
+ }
+
+ /* customization of the validation */
+ opj_j2k_setup_encoding_validation (p_j2k);
+
+ /* validation of the parameters codec */
+ if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* customization of the encoding */
+ opj_j2k_setup_header_writing(p_j2k);
+
+ /* write header */
+ if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ (void)p_stream;
+ if (p_tile_index != p_j2k->m_current_tile_number) {
+ opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." );
+ return OPJ_FALSE;
+ }
+
+ opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
+
+ p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
+ p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
+ p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
+
+ /* initialisation before tile encoding */
+ if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
+{
+ OPJ_UINT32 i,j,k = 0;
+ OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_t * l_tilec = 00;
+ opj_image_t * l_image = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+ OPJ_INT32 * l_src_ptr;
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_image = p_tcd->image;
+ l_img_comp = l_image->comps;
+
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+ l_size_comp = l_img_comp->prec >> 3; /* (/8) */
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
+ l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
+ l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, (OPJ_INT32)l_img_comp->dy);
+ l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_stride = l_image_width - l_width;
+ l_src_ptr = l_img_comp->data + ((OPJ_UINT32)l_tilec->x0 - l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - l_offset_y) * l_image_width;
+
+ switch (l_size_comp) {
+ case 1:
+ {
+ OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
+ ++l_dest_ptr;
+ ++l_src_ptr;
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr)&0xff);
+ ++l_dest_ptr;
+ ++l_src_ptr;
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+
+ p_data = (OPJ_BYTE*) l_dest_ptr;
+ }
+ break;
+ case 2:
+ {
+ OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+
+ p_data = (OPJ_BYTE*) l_dest_ptr;
+ }
+ break;
+ case 4:
+ {
+ OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = *(l_src_ptr++);
+ }
+ l_src_ptr += l_stride;
+ }
+
+ p_data = (OPJ_BYTE*) l_dest_ptr;
+ }
+ break;
+ }
+
+ ++l_img_comp;
+ ++l_tilec;
+ }
+}
+
+OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ opj_tcd_t * l_tcd = 00;
+ OPJ_UINT32 l_nb_bytes_written;
+ OPJ_BYTE * l_current_data = 00;
+ OPJ_UINT32 l_tile_size = 0;
+ OPJ_UINT32 l_available_data;
+
+ /* preconditions */
+ assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
+
+ l_tcd = p_j2k->m_tcd;
+
+ l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
+ l_available_data = l_tile_size;
+ l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
+
+ if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written = 0;
+ if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+ l_current_data += l_nb_bytes_written;
+ l_available_data -= l_nb_bytes_written;
+
+ l_nb_bytes_written = 0;
+ if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_available_data -= l_nb_bytes_written;
+ l_nb_bytes_written = l_tile_size - l_available_data;
+
+ if ( opj_stream_write_data( p_stream,
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
+ l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
+ return OPJ_FALSE;
+ }
+
+ ++p_j2k->m_current_tile_number;
+
+ return OPJ_TRUE;
+}
+
+void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k)
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ /* DEVELOPER CORNER, insert your custom procedures */
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );
+
+ if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
+ }
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
+}
+
+void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k)
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
+ opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
+
+ /* DEVELOPER CORNER, add your custom validation procedure */
+ opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
+}
+
+void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
+
+ if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
+ /* No need for COC or QCC, QCD and COD are used
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc );
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc );
+ */
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
+
+ if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24) {
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
+ }
+ }
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);
+
+ if (p_j2k->m_cp.comment != 00) {
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
+ }
+
+ /* DEVELOPER CORNER, insert your custom procedures */
+ if (p_j2k->m_cp.rsiz & OPJ_MCT) {
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
+ }
+ /* End of Developer Corner */
+
+ if (p_j2k->cstr_index) {
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
+ }
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
+}
+
+OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ opj_stream_private_t *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 l_nb_bytes_written = 0;
+ OPJ_UINT32 l_current_nb_bytes_written;
+ OPJ_BYTE * l_begin_data = 00;
+
+ opj_tcd_t * l_tcd = 00;
+ opj_cp_t * l_cp = 00;
+
+ l_tcd = p_j2k->m_tcd;
+ l_cp = &(p_j2k->m_cp);
+
+ l_tcd->cur_pino = 0;
+
+ /*Get number of tile parts*/
+ p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
+
+ /* INDEX >> */
+ /* << INDEX */
+
+ l_current_nb_bytes_written = 0;
+ l_begin_data = p_data;
+ if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
+ {
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+
+ if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
+#if 0
+ for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
+ l_current_nb_bytes_written = 0;
+ opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+
+ l_current_nb_bytes_written = 0;
+ opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ }
+#endif
+
+ if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
+ l_current_nb_bytes_written = 0;
+ opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ }
+ }
+
+ l_current_nb_bytes_written = 0;
+ if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ * p_data_written = l_nb_bytes_written;
+
+ /* Writing Psot in SOT marker */
+ opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
+
+ if (l_cp->m_specific_param.m_enc.m_cinema){
+ opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_all_tile_parts( opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_total_data_size,
+ opj_stream_private_t *p_stream,
+ struct opj_event_mgr * p_manager
+ )
+{
+ OPJ_UINT32 tilepartno=0;
+ OPJ_UINT32 l_nb_bytes_written = 0;
+ OPJ_UINT32 l_current_nb_bytes_written;
+ OPJ_UINT32 l_part_tile_size;
+ OPJ_UINT32 tot_num_tp;
+ OPJ_UINT32 pino;
+
+ OPJ_BYTE * l_begin_data;
+ opj_tcp_t *l_tcp = 00;
+ opj_tcd_t * l_tcd = 00;
+ opj_cp_t * l_cp = 00;
+
+ l_tcd = p_j2k->m_tcd;
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
+
+ /*Get number of tile parts*/
+ tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
+
+ /* start writing remaining tile parts */
+ ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
+ for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
+ p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
+ l_current_nb_bytes_written = 0;
+ l_part_tile_size = 0;
+ l_begin_data = p_data;
+
+ if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ l_part_tile_size += l_current_nb_bytes_written;
+
+ l_current_nb_bytes_written = 0;
+ if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ p_data += l_current_nb_bytes_written;
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ l_part_tile_size += l_current_nb_bytes_written;
+
+ /* Writing Psot in SOT marker */
+ opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
+
+ if (l_cp->m_specific_param.m_enc.m_cinema) {
+ opj_j2k_update_tlm(p_j2k,l_part_tile_size);
+ }
+
+ ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
+ }
+
+ for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
+ l_tcd->cur_pino = pino;
+
+ /*Get number of tile parts*/
+ tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
+ for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
+ p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
+ l_current_nb_bytes_written = 0;
+ l_part_tile_size = 0;
+ l_begin_data = p_data;
+
+ if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ l_part_tile_size += l_current_nb_bytes_written;
+
+ l_current_nb_bytes_written = 0;
+
+ if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_bytes_written += l_current_nb_bytes_written;
+ p_data += l_current_nb_bytes_written;
+ p_total_data_size -= l_current_nb_bytes_written;
+ l_part_tile_size += l_current_nb_bytes_written;
+
+ /* Writing Psot in SOT marker */
+ opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
+
+ if (l_cp->m_specific_param.m_enc.m_cinema) {
+ opj_j2k_update_tlm(p_j2k,l_part_tile_size);
+ }
+
+ ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
+ }
+ }
+
+ *p_data_written = l_nb_bytes_written;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ OPJ_UINT32 l_tlm_size;
+ OPJ_OFF_T l_tlm_position, l_current_position;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
+ l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
+ l_current_position = opj_stream_tell(p_stream);
+
+ if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) {
+ return OPJ_FALSE;
+ }
+
+ if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_end_encoding( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ opj_tcd_destroy(p_j2k->m_tcd);
+ p_j2k->m_tcd = 00;
+
+ if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
+ }
+
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Destroys the memory associated with the decoding of headers.
+ */
+static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
+ }
+
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_init_info( opj_j2k_t *p_j2k,
+ struct opj_stream_private *p_stream,
+ struct opj_event_mgr * p_manager )
+{
+ opj_codestream_info_t * l_cstr_info = 00;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+ (void)l_cstr_info;
+
+ /* TODO mergeV2: check this part which use cstr_info */
+ /*l_cstr_info = p_j2k->cstr_info;
+
+ if (l_cstr_info) {
+ OPJ_UINT32 compno;
+ l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
+
+ l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
+ l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
+
+ l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
+
+ l_cstr_info->tw = p_j2k->m_cp.tw;
+ l_cstr_info->th = p_j2k->m_cp.th;
+
+ l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
+ /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
+ /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
+ /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
+
+ /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
+
+ l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
+
+ l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
+
+ for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
+ l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
+ }
+
+ l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
+
+ /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
+
+ /*l_cstr_info->maxmarknum = 100;
+ l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
+ l_cstr_info->marknum = 0;
+ }*/
+
+ return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
+}
+
+/**
+ * Creates a tile-coder decoder.
+ *
+ * @param p_stream the stream to write data to.
+ * @param p_j2k J2K codec.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_create_tcd( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_stream != 00);
+
+ p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
+
+ if (! p_j2k->m_tcd) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
+ return OPJ_FALSE;
+ }
+
+ if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
+ opj_tcd_destroy(p_j2k->m_tcd);
+ p_j2k->m_tcd = 00;
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager )
+{
+ if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
+ return OPJ_FALSE;
+ }
+ else {
+ if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+#ifdef _FOXIT_MEM_MANAGER_
+/** Allocate number of bytes */
+void* FXMEM_DefaultAlloc(int byte_size, int flags);
+void* FXMEM_DefaultRealloc(void* pointer, int new_size, int flags);
+void FXMEM_DefaultFree(void* pointer, int flags);
+
+void* opj_malloc(size_t size)
+{
+ if (size >= (size_t)-0x100 || (int)size < 0) return NULL;
+
+ return FXMEM_DefaultAlloc(size, 0);
+}
+
+void* opj_calloc(size_t _NumOfElements, size_t _SizeOfElements)
+{
+ void* buffer = NULL;
+
+ if (_NumOfElements != 0 && _NumOfElements >= (size_t)-0x100 / _SizeOfElements) return NULL;
+ if ((int)_NumOfElements < 0 || (int)_SizeOfElements < 0) return NULL;
+
+ buffer = FXMEM_DefaultAlloc(_NumOfElements * _SizeOfElements, 0);
+ if (!buffer) return 0;
+
+ memset(buffer, 0, _NumOfElements * _SizeOfElements);
+ return buffer;
+}
+
+void* opj_realloc(void * m, size_t s)
+{
+ if (s >= (size_t)-0x100 || (int)s < 0) return NULL;
+ return FXMEM_DefaultRealloc(m, s, 0);
+}
+
+void opj_free(void * m)
+{
+ FXMEM_DefaultFree(m, 0);
+}
+
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.h
new file mode 100644
index 0000000000..4228bd66dc
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.h
@@ -0,0 +1,844 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006-2007, Parvatha Elangovan
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __J2K_H
+#define __J2K_H
+/**
+@file j2k.h
+@brief The JPEG-2000 Codestream Reader/Writer (J2K)
+
+The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
+*/
+
+/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
+/*@{*/
+
+#define J2K_CP_CSTY_PRT 0x01
+#define J2K_CP_CSTY_SOP 0x02
+#define J2K_CP_CSTY_EPH 0x04
+#define J2K_CCP_CSTY_PRT 0x01
+#define J2K_CCP_CBLKSTY_LAZY 0x01 /**< Selective arithmetic coding bypass */
+#define J2K_CCP_CBLKSTY_RESET 0x02 /**< Reset context probabilities on coding pass boundaries */
+#define J2K_CCP_CBLKSTY_TERMALL 0x04 /**< Termination on each coding pass */
+#define J2K_CCP_CBLKSTY_VSC 0x08 /**< Vertically stripe causal context */
+#define J2K_CCP_CBLKSTY_PTERM 0x10 /**< Predictable termination */
+#define J2K_CCP_CBLKSTY_SEGSYM 0x20 /**< Segmentation symbols are used */
+#define J2K_CCP_QNTSTY_NOQNT 0
+#define J2K_CCP_QNTSTY_SIQNT 1
+#define J2K_CCP_QNTSTY_SEQNT 2
+
+#define OPJ_J2K_DEFAULT_CBLK_DATA_SIZE 8192
+
+/* ----------------------------------------------------------------------- */
+
+#define J2K_MS_SOC 0xff4f /**< SOC marker value */
+#define J2K_MS_SOT 0xff90 /**< SOT marker value */
+#define J2K_MS_SOD 0xff93 /**< SOD marker value */
+#define J2K_MS_EOC 0xffd9 /**< EOC marker value */
+#define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
+#define J2K_MS_COD 0xff52 /**< COD marker value */
+#define J2K_MS_COC 0xff53 /**< COC marker value */
+#define J2K_MS_RGN 0xff5e /**< RGN marker value */
+#define J2K_MS_QCD 0xff5c /**< QCD marker value */
+#define J2K_MS_QCC 0xff5d /**< QCC marker value */
+#define J2K_MS_POC 0xff5f /**< POC marker value */
+#define J2K_MS_TLM 0xff55 /**< TLM marker value */
+#define J2K_MS_PLM 0xff57 /**< PLM marker value */
+#define J2K_MS_PLT 0xff58 /**< PLT marker value */
+#define J2K_MS_PPM 0xff60 /**< PPM marker value */
+#define J2K_MS_PPT 0xff61 /**< PPT marker value */
+#define J2K_MS_SOP 0xff91 /**< SOP marker value */
+#define J2K_MS_EPH 0xff92 /**< EPH marker value */
+#define J2K_MS_CRG 0xff63 /**< CRG marker value */
+#define J2K_MS_COM 0xff64 /**< COM marker value */
+#define J2K_MS_CBD 0xff78 /**< CBD marker value */
+#define J2K_MS_MCC 0xff75 /**< MCC marker value */
+#define J2K_MS_MCT 0xff74 /**< MCT marker value */
+#define J2K_MS_MCO 0xff77 /**< MCO marker value */
+
+#define J2K_MS_UNK 0 /**< UNKNOWN marker value */
+
+/* UniPG>> */
+#ifdef USE_JPWL
+#define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
+#define J2K_MS_EPB 0xff66 /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */
+#define J2K_MS_ESD 0xff67 /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */
+#define J2K_MS_RED 0xff69 /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
+#endif /* USE_JPWL */
+#ifdef USE_JPSEC
+#define J2K_MS_SEC 0xff65 /**< SEC marker value (Part 8: Secure JPEG 2000) */
+#define J2K_MS_INSEC 0xff94 /**< INSEC marker value (Part 8: Secure JPEG 2000) */
+#endif /* USE_JPSEC */
+/* <<UniPG */
+
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Values that specify the status of the decoding process when decoding the main header.
+ * These values may be combined with a | operator.
+ * */
+typedef enum J2K_STATUS {
+ J2K_STATE_NONE = 0x0000, /**< a SOC marker is expected */
+ J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
+ J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
+ J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */
+ J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
+ J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
+ J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */
+ J2K_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
+
+ J2K_STATE_EOC = 0x0100, /**< the decoding process has encountered the EOC marker */
+ J2K_STATE_ERR = 0x8000 /**< the decoding process has encountered an error (FIXME warning V1 = 0x0080)*/
+} J2K_STATUS;
+
+/**
+ * Type of elements storing in the MCT data
+ */
+typedef enum MCT_ELEMENT_TYPE
+{
+ MCT_TYPE_INT16 = 0, /** MCT data is stored as signed shorts*/
+ MCT_TYPE_INT32 = 1, /** MCT data is stored as signed integers*/
+ MCT_TYPE_FLOAT = 2, /** MCT data is stored as floats*/
+ MCT_TYPE_DOUBLE = 3 /** MCT data is stored as doubles*/
+} J2K_MCT_ELEMENT_TYPE;
+
+/**
+ * Type of MCT array
+ */
+typedef enum MCT_ARRAY_TYPE
+{
+ MCT_TYPE_DEPENDENCY = 0,
+ MCT_TYPE_DECORRELATION = 1,
+ MCT_TYPE_OFFSET = 2
+} J2K_MCT_ARRAY_TYPE;
+
+/* ----------------------------------------------------------------------- */
+
+/**
+T2 encoding mode
+*/
+typedef enum T2_MODE {
+ THRESH_CALC = 0, /** Function called in Rate allocation process*/
+ FINAL_PASS = 1 /** Function called in Tier 2 process*/
+}J2K_T2_MODE;
+
+/**
+ * Quantization stepsize
+ */
+typedef struct opj_stepsize {
+ /** exponent */
+ OPJ_INT32 expn;
+ /** mantissa */
+ OPJ_INT32 mant;
+} opj_stepsize_t;
+
+/**
+Tile-component coding parameters
+*/
+typedef struct opj_tccp
+{
+ /** coding style */
+ OPJ_UINT32 csty;
+ /** number of resolutions */
+ OPJ_UINT32 numresolutions;
+ /** code-blocks width */
+ OPJ_UINT32 cblkw;
+ /** code-blocks height */
+ OPJ_UINT32 cblkh;
+ /** code-block coding style */
+ OPJ_UINT32 cblksty;
+ /** discrete wavelet transform identifier */
+ OPJ_UINT32 qmfbid;
+ /** quantisation style */
+ OPJ_UINT32 qntsty;
+ /** stepsizes used for quantization */
+ opj_stepsize_t stepsizes[OPJ_J2K_MAXBANDS];
+ /** number of guard bits */
+ OPJ_UINT32 numgbits;
+ /** Region Of Interest shift */
+ OPJ_INT32 roishift;
+ /** precinct width */
+ OPJ_UINT32 prcw[OPJ_J2K_MAXRLVLS];
+ /** precinct height */
+ OPJ_UINT32 prch[OPJ_J2K_MAXRLVLS];
+ /** the dc_level_shift **/
+ OPJ_INT32 m_dc_level_shift;
+}
+opj_tccp_t;
+
+
+
+/**
+ * FIXME DOC
+ */
+typedef struct opj_mct_data
+{
+ J2K_MCT_ELEMENT_TYPE m_element_type;
+ J2K_MCT_ARRAY_TYPE m_array_type;
+ OPJ_UINT32 m_index;
+ OPJ_BYTE * m_data;
+ OPJ_UINT32 m_data_size;
+}
+opj_mct_data_t;
+
+/**
+ * FIXME DOC
+ */
+typedef struct opj_simple_mcc_decorrelation_data
+{
+ OPJ_UINT32 m_index;
+ OPJ_UINT32 m_nb_comps;
+ opj_mct_data_t * m_decorrelation_array;
+ opj_mct_data_t * m_offset_array;
+ OPJ_UINT32 m_is_irreversible : 1;
+}
+opj_simple_mcc_decorrelation_data_t;
+
+/**
+Tile coding parameters :
+this structure is used to store coding/decoding parameters common to all
+tiles (information like COD, COC in main header)
+*/
+typedef struct opj_tcp
+{
+ /** coding style */
+ OPJ_UINT32 csty;
+ /** progression order */
+ OPJ_PROG_ORDER prg;
+ /** number of layers */
+ OPJ_UINT32 numlayers;
+ OPJ_UINT32 num_layers_to_decode;
+ /** multi-component transform identifier */
+ OPJ_UINT32 mct;
+ /** rates of layers */
+ OPJ_FLOAT32 rates[100];
+ /** number of progression order changes */
+ OPJ_UINT32 numpocs;
+ /** progression order changes */
+ opj_poc_t pocs[32];
+ /** packet header store there for futur use in t2_decode_packet */
+ OPJ_BYTE *ppt_data;
+ /** used to keep a track of the allocated memory */
+ OPJ_BYTE *ppt_buffer;
+ /** Number of bytes stored inside ppt_data*/
+ OPJ_UINT32 ppt_data_size;
+ /** size of ppt_data*/
+ OPJ_UINT32 ppt_len;
+ /** add fixed_quality */
+ OPJ_FLOAT32 distoratio[100];
+ /** tile-component coding parameters */
+ opj_tccp_t *tccps;
+ /** number of tile parts for the tile. */
+ OPJ_UINT32 m_nb_tile_parts;
+ /** data for the tile */
+ OPJ_BYTE * m_data;
+ /** size of data */
+ OPJ_UINT32 m_data_size;
+ /** encoding norms */
+ OPJ_FLOAT64 * mct_norms;
+ /** the mct decoding matrix */
+ OPJ_FLOAT32 * m_mct_decoding_matrix;
+ /** the mct coding matrix */
+ OPJ_FLOAT32 * m_mct_coding_matrix;
+ /** mct records */
+ opj_mct_data_t * m_mct_records;
+ /** the number of mct records. */
+ OPJ_UINT32 m_nb_mct_records;
+ /** the max number of mct records. */
+ OPJ_UINT32 m_nb_max_mct_records;
+ /** mcc records */
+ opj_simple_mcc_decorrelation_data_t * m_mcc_records;
+ /** the number of mct records. */
+ OPJ_UINT32 m_nb_mcc_records;
+ /** the max number of mct records. */
+ OPJ_UINT32 m_nb_max_mcc_records;
+
+
+ /***** FLAGS *******/
+ /** If ppt == 1 --> there was a PPT marker for the present tile */
+ OPJ_UINT32 ppt : 1;
+ /** indicates if a POC marker has been used O:NO, 1:YES */
+ OPJ_UINT32 POC : 1;
+} opj_tcp_t;
+
+
+
+
+typedef struct opj_encoding_param
+{
+ /** Digital cinema profile*/
+ OPJ_CINEMA_MODE m_cinema;
+ /** Maximum rate for each component. If == 0, component size limitation is not considered */
+ OPJ_UINT32 m_max_comp_size;
+ /** Position of tile part flag in progression order*/
+ OPJ_INT32 m_tp_pos;
+ /** fixed layer */
+ OPJ_INT32 *m_matrice;
+ /** Flag determining tile part generation*/
+ OPJ_BYTE m_tp_flag;
+ /** allocation by rate/distortion */
+ OPJ_UINT32 m_disto_alloc : 1;
+ /** allocation by fixed layer */
+ OPJ_UINT32 m_fixed_alloc : 1;
+ /** add fixed_quality */
+ OPJ_UINT32 m_fixed_quality : 1;
+ /** Enabling Tile part generation*/
+ OPJ_UINT32 m_tp_on : 1;
+}
+opj_encoding_param_t;
+
+typedef struct opj_decoding_param
+{
+ /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
+ OPJ_UINT32 m_reduce;
+ /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
+ OPJ_UINT32 m_layer;
+}
+opj_decoding_param_t;
+
+
+/**
+ * Coding parameters
+ */
+typedef struct opj_cp
+{
+ /** Size of the image in bits*/
+ /*int img_size;*/
+ /** Rsiz*/
+ OPJ_RSIZ_CAPABILITIES rsiz;
+ /** XTOsiz */
+ OPJ_UINT32 tx0; /* MSD see norm */
+ /** YTOsiz */
+ OPJ_UINT32 ty0; /* MSD see norm */
+ /** XTsiz */
+ OPJ_UINT32 tdx;
+ /** YTsiz */
+ OPJ_UINT32 tdy;
+ /** comment */
+ OPJ_CHAR *comment;
+ /** number of tiles in width */
+ OPJ_UINT32 tw;
+ /** number of tiles in heigth */
+ OPJ_UINT32 th;
+
+ /** packet header store there for futur use in t2_decode_packet */
+ OPJ_BYTE *ppm_data;
+ /** size of the ppm_data*/
+ OPJ_UINT32 ppm_len;
+ /** size of the ppm_data*/
+ OPJ_UINT32 ppm_data_read;
+
+ OPJ_BYTE *ppm_data_current;
+
+ /** packet header storage original buffer */
+ OPJ_BYTE *ppm_buffer;
+ /** pointer remaining on the first byte of the first header if ppm is used */
+ OPJ_BYTE *ppm_data_first;
+ /** Number of bytes actually stored inside the ppm_data */
+ OPJ_UINT32 ppm_data_size;
+ /** use in case of multiple marker PPM (number of info already store) */
+ OPJ_INT32 ppm_store;
+ /** use in case of multiple marker PPM (case on non-finished previous info) */
+ OPJ_INT32 ppm_previous;
+
+ /** tile coding parameters */
+ opj_tcp_t *tcps;
+
+ union
+ {
+ opj_decoding_param_t m_dec;
+ opj_encoding_param_t m_enc;
+ }
+ m_specific_param;
+
+
+/* UniPG>> */
+#ifdef USE_JPWL
+ /** enables writing of EPC in MH, thus activating JPWL */
+ OPJ_BOOL epc_on;
+ /** enables writing of EPB, in case of activated JPWL */
+ OPJ_BOOL epb_on;
+ /** enables writing of ESD, in case of activated JPWL */
+ OPJ_BOOL esd_on;
+ /** enables writing of informative techniques of ESD, in case of activated JPWL */
+ OPJ_BOOL info_on;
+ /** enables writing of RED, in case of activated JPWL */
+ OPJ_BOOL red_on;
+ /** error protection method for MH (0,1,16,32,37-128) */
+ int hprot_MH;
+ /** tile number of header protection specification (>=0) */
+ int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
+ /** error protection methods for TPHs (0,1,16,32,37-128) */
+ int hprot_TPH[JPWL_MAX_NO_TILESPECS];
+ /** tile number of packet protection specification (>=0) */
+ int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
+ /** packet number of packet protection specification (>=0) */
+ int pprot_packno[JPWL_MAX_NO_PACKSPECS];
+ /** error protection methods for packets (0,1,16,32,37-128) */
+ int pprot[JPWL_MAX_NO_PACKSPECS];
+ /** enables writing of ESD, (0/2/4 bytes) */
+ int sens_size;
+ /** sensitivity addressing size (0=auto/2/4 bytes) */
+ int sens_addr;
+ /** sensitivity range (0-3) */
+ int sens_range;
+ /** sensitivity method for MH (-1,0-7) */
+ int sens_MH;
+ /** tile number of sensitivity specification (>=0) */
+ int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
+ /** sensitivity methods for TPHs (-1,0-7) */
+ int sens_TPH[JPWL_MAX_NO_TILESPECS];
+ /** enables JPWL correction at the decoder */
+ OPJ_BOOL correct;
+ /** expected number of components at the decoder */
+ int exp_comps;
+ /** maximum number of tiles at the decoder */
+ OPJ_UINT32 max_tiles;
+#endif /* USE_JPWL */
+
+ /******** FLAGS *********/
+ /** if ppm == 1 --> there was a PPM marker*/
+ OPJ_UINT32 ppm : 1;
+ /** tells if the parameter is a coding or decoding one */
+ OPJ_UINT32 m_is_decoder : 1;
+/* <<UniPG */
+} opj_cp_t;
+
+
+typedef struct opj_j2k_dec
+{
+ /** locate in which part of the codestream the decoder is (main header, tile header, end) */
+ OPJ_UINT32 m_state;
+ /**
+ * store decoding parameters common to all tiles (information like COD, COC in main header)
+ */
+ opj_tcp_t *m_default_tcp;
+ OPJ_BYTE *m_header_data;
+ OPJ_UINT32 m_header_data_size;
+ /** to tell the tile part length */
+ OPJ_UINT32 m_sot_length;
+ /** Only tiles index in the correct range will be decoded.*/
+ OPJ_UINT32 m_start_tile_x;
+ OPJ_UINT32 m_start_tile_y;
+ OPJ_UINT32 m_end_tile_x;
+ OPJ_UINT32 m_end_tile_y;
+ /**
+ * Decoded area set by the user
+ */
+ OPJ_UINT32 m_DA_x0;
+ OPJ_UINT32 m_DA_y0;
+ OPJ_UINT32 m_DA_x1;
+ OPJ_UINT32 m_DA_y1;
+
+ /** Index of the tile to decode (used in get_tile) */
+ OPJ_INT32 m_tile_ind_to_dec;
+ /** Position of the last SOT marker read */
+ OPJ_OFF_T m_last_sot_read_pos;
+
+ /**
+ * Indicate that the current tile-part is assume as the last tile part of the codestream.
+ * It is useful in the case of PSot is equal to zero. The sot length will be compute in the
+ * SOD reader function. FIXME NOT USED for the moment
+ */
+ OPJ_BOOL m_last_tile_part;
+ /** to tell that a tile can be decoded. */
+ OPJ_UINT32 m_can_decode : 1;
+ OPJ_UINT32 m_discard_tiles : 1;
+ OPJ_UINT32 m_skip_data : 1;
+
+} opj_j2k_dec_t;
+
+typedef struct opj_j2k_enc
+{
+ /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
+ OPJ_UINT32 m_current_poc_tile_part_number; /* tp_num */
+
+ /** Tile part number currently coding, taking into account POC. m_current_tile_part_number holds the total number of tile parts while encoding the last tile part.*/
+ OPJ_UINT32 m_current_tile_part_number; /*cur_tp_num */
+
+ /**
+ locate the start position of the TLM marker
+ after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
+ */
+ OPJ_OFF_T m_tlm_start;
+ /**
+ * Stores the sizes of the tlm.
+ */
+ OPJ_BYTE * m_tlm_sot_offsets_buffer;
+ /**
+ * The current offset of the tlm buffer.
+ */
+ OPJ_BYTE * m_tlm_sot_offsets_current;
+
+ /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
+ /** used in TLMmarker*/
+ OPJ_UINT32 m_total_tile_parts; /* totnum_tp */
+
+ /* encoded data for a tile */
+ OPJ_BYTE * m_encoded_tile_data;
+
+ /* size of the encoded_data */
+ OPJ_UINT32 m_encoded_tile_size;
+
+ /* encoded data for a tile */
+ OPJ_BYTE * m_header_tile_data;
+
+ /* size of the encoded_data */
+ OPJ_UINT32 m_header_tile_data_size;
+
+
+} opj_j2k_enc_t;
+
+
+
+struct opj_tcd;
+/**
+JPEG-2000 codestream reader/writer
+*/
+typedef struct opj_j2k
+{
+ /* J2K codestream is decoded*/
+ OPJ_BOOL m_is_decoder;
+
+ /* FIXME DOC*/
+ union
+ {
+ opj_j2k_dec_t m_decoder;
+ opj_j2k_enc_t m_encoder;
+ }
+ m_specific_param;
+
+ /** pointer to the internal/private encoded / decoded image */
+ opj_image_t* m_private_image;
+
+ /* pointer to the output image (decoded)*/
+ opj_image_t* m_output_image;
+
+ /** Coding parameters */
+ opj_cp_t m_cp;
+
+ /** the list of procedures to exec **/
+ opj_procedure_list_t * m_procedure_list;
+
+ /** the list of validation procedures to follow to make sure the code is valid **/
+ opj_procedure_list_t * m_validation_list;
+
+ /** helper used to write the index file */
+ opj_codestream_index_t *cstr_index;
+
+ /** number of the tile curently concern by coding/decoding */
+ OPJ_UINT32 m_current_tile_number;
+
+ /** the current tile coder/decoder **/
+ struct opj_tcd * m_tcd;
+
+}
+opj_j2k_t;
+
+
+
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Setup the decoder decoding parameters using user parameters.
+Decoding parameters are returned in j2k->cp.
+@param j2k J2K decompressor handle
+@param parameters decompression parameters
+*/
+void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
+
+/**
+ * Creates a J2K compression structure
+ *
+ * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
+*/
+opj_j2k_t* opj_j2k_create_compress(void);
+
+
+void opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
+ opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager);
+
+/**
+Converts an enum type progression order to string type
+*/
+char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+/**
+ * Ends the decompression procedures and possibiliy add data to be read after the
+ * codestream.
+ */
+OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Reads a jpeg2000 codestream header structure.
+ *
+ * @param p_stream the stream to read data from.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_image FIXME DOC
+ * @param p_manager the user event manager.
+ *
+ * @return true if the box is valid.
+ */
+OPJ_BOOL opj_j2k_read_header( opj_stream_private_t *p_stream,
+ opj_j2k_t* p_j2k,
+ opj_image_t** p_image,
+ opj_event_mgr_t* p_manager );
+
+
+/**
+ * Destroys a jpeg2000 codec.
+ *
+ * @param p_j2k the jpeg20000 structure to destroy.
+ */
+void opj_j2k_destroy (opj_j2k_t *p_j2k);
+
+/**
+ * Destroys a codestream index structure.
+ *
+ * @param p_cstr_ind the codestream index parameter to destroy.
+ */
+void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind);
+
+/**
+ * Decode tile data.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_tile_index
+ * @param p_data FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+OPJ_BOOL opj_j2k_decode_tile ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a tile header.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_tile_index FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_tile_x0 FIXME DOC
+ * @param p_tile_y0 FIXME DOC
+ * @param p_tile_x1 FIXME DOC
+ * @param p_tile_y1 FIXME DOC
+ * @param p_nb_comps FIXME DOC
+ * @param p_go_on FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+OPJ_BOOL opj_j2k_read_tile_header ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0,
+ OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1,
+ OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_go_on,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+
+/**
+ * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
+ *
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_image FIXME DOC
+ * @param p_start_x the left position of the rectangle to decode (in image coordinates).
+ * @param p_start_y the up position of the rectangle to decode (in image coordinates).
+ * @param p_end_x the right position of the rectangle to decode (in image coordinates).
+ * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
+ * @param p_manager the user event manager
+ *
+ * @return true if the area could be set.
+ */
+OPJ_BOOL opj_j2k_set_decode_area( opj_j2k_t *p_j2k,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Creates a J2K decompression structure.
+ *
+ * @return a handle to a J2K decompressor if successful, NULL otherwise.
+ */
+opj_j2k_t* opj_j2k_create_decompress(void);
+
+
+/**
+ * Dump some elements from the J2K decompression structure .
+ *
+ *@param p_j2k the jpeg2000 codec.
+ *@param flag flag to describe what elments are dump.
+ *@param out_stream output stream where dump the elements.
+ *
+*/
+void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream);
+
+
+
+/**
+ * Dump an image header structure.
+ *
+ *@param image the image header to dump.
+ *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
+ *@param out_stream output stream where dump the elements.
+ */
+void j2k_dump_image_header(opj_image_t* image, OPJ_BOOL dev_dump_flag, FILE* out_stream);
+
+/**
+ * Dump a component image header structure.
+ *
+ *@param comp the component image header to dump.
+ *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
+ *@param out_stream output stream where dump the elements.
+ */
+void j2k_dump_image_comp_header(opj_image_comp_t* comp, OPJ_BOOL dev_dump_flag, FILE* out_stream);
+
+/**
+ * Get the codestream info from a JPEG2000 codec.
+ *
+ *@param p_j2k the component image header to dump.
+ *
+ *@return the codestream information extract from the jpg2000 codec
+ */
+opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k);
+
+/**
+ * Get the codestream index from a JPEG2000 codec.
+ *
+ *@param p_j2k the component image header to dump.
+ *
+ *@return the codestream index extract from the jpg2000 codec
+ */
+opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k);
+
+/**
+ * Decode an image from a JPEG-2000 codestream
+ * @param j2k J2K decompressor handle
+ * @param p_stream FIXME DOC
+ * @param p_image FIXME DOC
+ * @param p_manager FIXME DOC
+ * @return FIXME DOC
+*/
+OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k,
+ opj_stream_private_t *p_stream,
+ opj_image_t *p_image,
+ opj_event_mgr_t *p_manager);
+
+
+OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager,
+ OPJ_UINT32 tile_index );
+
+OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager);
+
+
+/**
+ * Writes a tile.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_tile_index FIXME DOC
+ * @param p_data FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+OPJ_BOOL opj_j2k_write_tile ( opj_j2k_t * p_j2k,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Encodes an image into a JPEG-2000 codestream
+ */
+OPJ_BOOL opj_j2k_encode( opj_j2k_t * p_j2k,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
+ *
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream the stream object.
+ * @param p_image FIXME DOC
+ * @param p_manager the user event manager.
+ *
+ * @return true if the codec is valid.
+ */
+OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Ends the compression procedures and possibiliy add data to be read after the
+ * codestream.
+ */
+OPJ_BOOL opj_j2k_end_compress( opj_j2k_t *p_j2k,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager);
+
+OPJ_BOOL opj_j2k_setup_mct_encoding (opj_tcp_t * p_tcp, opj_image_t * p_image);
+
+
+#endif /* __J2K_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.c
new file mode 100644
index 0000000000..0a16d1e0e0
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.c
@@ -0,0 +1,2825 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2010-2011, Kaori Hagihara
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "opj_includes.h"
+
+/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
+/*@{*/
+
+#define OPJ_BOX_SIZE 1024
+
+/** @name Local static functions */
+/*@{*/
+
+/*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
+
+/**
+ * Reads a IHDR box - Image Header box
+ *
+ * @param p_image_header_data pointer to actual data (already read from file)
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_image_header_size the size of the image header
+ * @param p_manager the user event manager.
+ *
+ * @return true if the image header is valid, false else.
+ */
+static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
+ OPJ_BYTE *p_image_header_data,
+ OPJ_UINT32 p_image_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the Image Header box - Image Header box.
+ *
+ * @param jp2 jpeg2000 file codec.
+ * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
+ *
+ * @return the data being copied.
+*/
+static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written );
+
+/**
+ * Writes the Bit per Component box.
+ *
+ * @param jp2 jpeg2000 file codec.
+ * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
+ *
+ * @return the data being copied.
+*/
+static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written );
+
+/**
+ * Reads a Bit per Component box.
+ *
+ * @param p_bpc_header_data pointer to actual data (already read from file)
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_bpc_header_size the size of the bpc header
+ * @param p_manager the user event manager.
+ *
+ * @return true if the bpc header is valid, fale else.
+ */
+static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
+ OPJ_BYTE * p_bpc_header_data,
+ OPJ_UINT32 p_bpc_header_size,
+ opj_event_mgr_t * p_manager );
+
+static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
+ OPJ_BYTE * p_cdef_header_data,
+ OPJ_UINT32 p_cdef_header_size,
+ opj_event_mgr_t * p_manager );
+
+static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
+
+/**
+ * Writes the Colour Specification box.
+ *
+ * @param jp2 jpeg2000 file codec.
+ * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
+ *
+ * @return the data being copied.
+*/
+static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written );
+
+/**
+ * Writes a FTYP box - File type box
+ *
+ * @param cio the stream to write data to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager the user event manager.
+ *
+ * @return true if writing was successful.
+ */
+static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a a FTYP box - File type box
+ *
+ * @param p_header_data the data contained in the FTYP box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the FTYP box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the FTYP box is valid.
+ */
+static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
+ *
+ * @param p_header_data the data contained in the file header box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the file header box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the JP2 Header box was successfully reconized.
+*/
+static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
+ OPJ_BYTE *p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
+ *
+ * @param cio the stream to write data to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager user event manager.
+ *
+ * @return true if writing was successful.
+*/
+static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+#ifdef USE_JPIP
+/**
+ * Write index Finder box
+ * @param cio the stream to write to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager user event manager.
+*/
+static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Write index Finder box
+ * @param cio the stream to write to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager user event manager.
+ */
+static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Write file Index (superbox)
+ * @param cio the stream to write to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager user event manager.
+ */
+static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+#endif /* USE_JPIP */
+
+/**
+ * Reads a jpeg2000 file signature box.
+ *
+ * @param p_header_data the data contained in the signature box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the signature box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the file signature box is valid.
+ */
+static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Writes a jpeg2000 file signature box.
+ *
+ * @param cio the stream to write data to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager the user event manager.
+ *
+ * @return true if writing was successful.
+ */
+static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+Apply collected palette data
+@param color Collector for profile, cdef and pclr data
+@param image
+*/
+static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
+
+static void opj_jp2_free_pclr(opj_jp2_color_t *color);
+
+/**
+ * Collect palette data
+ *
+ * @param jp2 JP2 handle
+ * @param p_pclr_header_data FIXME DOC
+ * @param p_pclr_header_size FIXME DOC
+ * @param p_manager
+ *
+ * @return Returns true if successful, returns false otherwise
+*/
+static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
+ OPJ_BYTE * p_pclr_header_data,
+ OPJ_UINT32 p_pclr_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Collect component mapping data
+ *
+ * @param jp2 JP2 handle
+ * @param p_cmap_header_data FIXME DOC
+ * @param p_cmap_header_size FIXME DOC
+ * @param p_manager FIXME DOC
+ *
+ * @return Returns true if successful, returns false otherwise
+*/
+
+static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
+ OPJ_BYTE * p_cmap_header_data,
+ OPJ_UINT32 p_cmap_header_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads the Color Specification box.
+ *
+ * @param p_colr_header_data pointer to actual data (already read from file)
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_colr_header_size the size of the color header
+ * @param p_manager the user event manager.
+ *
+ * @return true if the bpc header is valid, fale else.
+*/
+static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
+ OPJ_BYTE * p_colr_header_data,
+ OPJ_UINT32 p_colr_header_size,
+ opj_event_mgr_t * p_manager );
+
+/*@}*/
+
+/*@}*/
+
+/**
+ * Sets up the procedures to do on writing header after the codestream.
+ * Developpers wanting to extend the library can add their own writing procedures.
+ */
+static void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2);
+
+/**
+ * Sets up the procedures to do on reading header after the codestream.
+ * Developpers wanting to extend the library can add their own writing procedures.
+ */
+static void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2);
+
+/**
+ * Reads a jpeg2000 file header structure.
+ *
+ * @param jp2 the jpeg2000 file header structure.
+ * @param stream the stream to read data from.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the box is valid.
+ */
+static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Excutes the given procedures on the given codec.
+ *
+ * @param p_procedure_list the list of procedures to execute
+ * @param jp2 the jpeg2000 file codec to execute the procedures on.
+ * @param stream the stream to execute the procedures on.
+ * @param p_manager the user manager.
+ *
+ * @return true if all the procedures were successfully executed.
+ */
+static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
+ opj_procedure_list_t * p_procedure_list,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
+ *
+ * @param cio the input stream to read data from.
+ * @param box the box structure to fill.
+ * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
+ * @param p_manager user event manager.
+ *
+ * @return true if the box is reconized, false otherwise
+*/
+static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
+ OPJ_UINT32 * p_number_bytes_read,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+static void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2);
+
+/**
+ * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
+ */
+static void opj_jp2_setup_header_writing (opj_jp2_t *jp2);
+
+OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Finds the image execution function related to the given box id.
+ *
+ * @param p_id the id of the handler to fetch.
+ *
+ * @return the given handler or NULL if it could not be found.
+ */
+static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
+
+/**
+ * Finds the execution function related to the given box id.
+ *
+ * @param p_id the id of the handler to fetch.
+ *
+ * @return the given handler or NULL if it could not be found.
+ */
+static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
+
+const opj_jp2_header_handler_t jp2_header [] =
+{
+ {JP2_JP,opj_jp2_read_jp},
+ {JP2_FTYP,opj_jp2_read_ftyp},
+ {JP2_JP2H,opj_jp2_read_jp2h}
+};
+
+const opj_jp2_header_handler_t jp2_img_header [] =
+{
+ {JP2_IHDR,opj_jp2_read_ihdr},
+ {JP2_COLR,opj_jp2_read_colr},
+ {JP2_BPCC,opj_jp2_read_bpcc},
+ {JP2_PCLR,opj_jp2_read_pclr},
+ {JP2_CMAP,opj_jp2_read_cmap},
+ {JP2_CDEF,opj_jp2_read_cdef}
+
+};
+
+/**
+ * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
+ *
+ * @param box the box structure to fill.
+ * @param p_data the character string to read data from.
+ * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
+ * @param p_box_max_size the maximum number of bytes in the box.
+ * @param p_manager FIXME DOC
+ *
+ * @return true if the box is reconized, false otherwise
+*/
+static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_number_bytes_read,
+ OPJ_UINT32 p_box_max_size,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+static void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2);
+
+/**
+ * Sets up the procedures to do on reading header.
+ * Developpers wanting to extend the library can add their own writing procedures.
+ */
+static void opj_jp2_setup_header_reading (opj_jp2_t *jp2);
+
+/* ----------------------------------------------------------------------- */
+ OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
+ OPJ_UINT32 * p_number_bytes_read,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ /* read header from file */
+ OPJ_BYTE l_data_header [8];
+
+ /* preconditions */
+ assert(cio != 00);
+ assert(box != 00);
+ assert(p_number_bytes_read != 00);
+ assert(p_manager != 00);
+
+ *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
+ if (*p_number_bytes_read != 8) {
+ return OPJ_FALSE;
+ }
+
+ /* process read data */
+ opj_read_bytes(l_data_header,&(box->length), 4);
+ opj_read_bytes(l_data_header+4,&(box->type), 4);
+
+ if(box->length == 0)/* last box */
+ {
+ const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
+ box->length = (OPJ_UINT32)bleft;
+ assert( (OPJ_OFF_T)box->length == bleft );
+ return OPJ_TRUE;
+ }
+
+ /* do we have a "special very large box ?" */
+ /* read then the XLBox */
+ if (box->length == 1) {
+ OPJ_UINT32 l_xl_part_size;
+
+ OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
+ if (l_nb_bytes_read != 8) {
+ if (l_nb_bytes_read > 0) {
+ *p_number_bytes_read += l_nb_bytes_read;
+ }
+
+ return OPJ_FALSE;
+ }
+
+ *p_number_bytes_read = 16;
+ opj_read_bytes(l_data_header,&l_xl_part_size, 4);
+ if (l_xl_part_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
+ return OPJ_FALSE;
+ }
+ opj_read_bytes(l_data_header+4,&(box->length), 4);
+ }
+ return OPJ_TRUE;
+}
+
+#if 0
+static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
+ OPJ_UINT32 i;
+ opj_jp2_box_t box;
+
+ box.init_pos = cio_tell(cio);
+ cio_skip(cio, 4);
+ cio_write(cio, JP2_URL, 4); /* DBTL */
+ cio_write(cio, 0, 1); /* VERS */
+ cio_write(cio, 0, 3); /* FLAG */
+
+ if(Idx_file) {
+ for (i = 0; i < strlen(Idx_file); i++) {
+ cio_write(cio, Idx_file[i], 1);
+ }
+ }
+
+ box.length = cio_tell(cio) - box.init_pos;
+ cio_seek(cio, box.init_pos);
+ cio_write(cio, box.length, 4); /* L */
+ cio_seek(cio, box.init_pos + box.length);
+}
+#endif
+
+OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
+ OPJ_BYTE *p_image_header_data,
+ OPJ_UINT32 p_image_header_size,
+ opj_event_mgr_t * p_manager )
+{
+ /* preconditions */
+ assert(p_image_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ if (p_image_header_size != 14) {
+ opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
+ p_image_header_data += 4;
+ opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
+ p_image_header_data += 4;
+ opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
+ p_image_header_data += 2;
+
+ /* allocate memory for components */
+ jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
+ if (jp2->comps == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
+ return OPJ_FALSE;
+ }
+ memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
+
+ opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
+ ++ p_image_header_data;
+
+ opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
+ ++ p_image_header_data;
+
+ /* Should be equal to 7 cf. chapter about image header box of the norm */
+ if (jp2->C != 7){
+ opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
+ }
+
+ opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
+ ++ p_image_header_data;
+ opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
+ ++ p_image_header_data;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written
+ )
+{
+ OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_nb_bytes_written != 00);
+
+ /* default image header is 22 bytes wide */
+ l_ihdr_data = (OPJ_BYTE *) opj_malloc(22);
+ if (l_ihdr_data == 00) {
+ return 00;
+ }
+ memset(l_ihdr_data,0,22);
+
+ l_current_ihdr_ptr = l_ihdr_data;
+
+ opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
+ l_current_ihdr_ptr+=4;
+
+ opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
+ l_current_ihdr_ptr+=4;
+
+ opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
+ l_current_ihdr_ptr+=4;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
+ l_current_ihdr_ptr+=4;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
+ l_current_ihdr_ptr+=2;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
+ ++l_current_ihdr_ptr;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
+ ++l_current_ihdr_ptr;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
+ ++l_current_ihdr_ptr;
+
+ opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
+ ++l_current_ihdr_ptr;
+
+ *p_nb_bytes_written = 22;
+
+ return l_ihdr_data;
+}
+
+OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written
+ )
+{
+ OPJ_UINT32 i;
+ /* room for 8 bytes for box and 1 byte for each component */
+ OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
+ OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_nb_bytes_written != 00);
+
+ l_bpcc_data = (OPJ_BYTE *) opj_malloc(l_bpcc_size);
+ if (l_bpcc_data == 00) {
+ return 00;
+ }
+ memset(l_bpcc_data,0,l_bpcc_size);
+
+ l_current_bpcc_ptr = l_bpcc_data;
+
+ opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
+ l_current_bpcc_ptr += 4;
+
+ opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
+ l_current_bpcc_ptr += 4;
+
+ for (i = 0; i < jp2->numcomps; ++i) {
+ opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
+ ++l_current_bpcc_ptr;
+ }
+
+ *p_nb_bytes_written = l_bpcc_size;
+
+ return l_bpcc_data;
+}
+
+OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
+ OPJ_BYTE * p_bpc_header_data,
+ OPJ_UINT32 p_bpc_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i;
+
+ /* preconditions */
+ assert(p_bpc_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+
+ if (jp2->bpc != 255 ){
+ opj_event_msg(p_manager, EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc);
+ }
+
+ /* and length is relevant */
+ if (p_bpc_header_size != jp2->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
+ return OPJ_FALSE;
+ }
+
+ /* read info for each component */
+ for (i = 0; i < jp2->numcomps; ++i) {
+ opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
+ ++p_bpc_header_data;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
+ OPJ_UINT32 * p_nb_bytes_written
+ )
+{
+ /* room for 8 bytes for box 3 for common data and variable upon profile*/
+ OPJ_UINT32 l_colr_size = 11;
+ OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_nb_bytes_written != 00);
+ assert(jp2->meth == 1 || jp2->meth == 2);
+
+ switch (jp2->meth) {
+ case 1 :
+ l_colr_size += 4; /* EnumCS */
+ break;
+ case 2 :
+ assert(jp2->color.icc_profile_len); /* ICC profile */
+ l_colr_size += jp2->color.icc_profile_len;
+ break;
+ default :
+ return 00;
+ }
+
+ l_colr_data = (OPJ_BYTE *) opj_malloc(l_colr_size);
+ if (l_colr_data == 00) {
+ return 00;
+ }
+ memset(l_colr_data,0,l_colr_size);
+
+ l_current_colr_ptr = l_colr_data;
+
+ opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
+ l_current_colr_ptr += 4;
+
+ opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
+ l_current_colr_ptr += 4;
+
+ opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
+ ++l_current_colr_ptr;
+
+ opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
+ ++l_current_colr_ptr;
+
+ opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
+ ++l_current_colr_ptr;
+
+ if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
+ opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
+ else {
+ if (jp2->meth == 2) { /* ICC profile */
+ OPJ_UINT32 i;
+ for(i = 0; i < jp2->color.icc_profile_len; ++i) {
+ opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
+ ++l_current_colr_ptr;
+ }
+ }
+ }
+
+ *p_nb_bytes_written = l_colr_size;
+
+ return l_colr_data;
+}
+
+void opj_jp2_free_pclr(opj_jp2_color_t *color)
+{
+ opj_free(color->jp2_pclr->channel_sign);
+ opj_free(color->jp2_pclr->channel_size);
+ opj_free(color->jp2_pclr->entries);
+
+ if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
+
+ opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
+}
+
+static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager)
+{
+ OPJ_UINT16 i;
+
+ /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
+ if (color->jp2_cdef) {
+ opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
+ OPJ_UINT16 n = color->jp2_cdef->n;
+
+ for (i = 0; i < n; i++) {
+ if (info[i].cn >= image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, image->numcomps);
+ return OPJ_FALSE;
+ }
+ if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, image->numcomps);
+ return OPJ_FALSE;
+ }
+ }
+ }
+
+ /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
+ 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
+ if (color->jp2_pclr && color->jp2_pclr->cmap) {
+ OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
+ opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
+ OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
+
+ /* verify that all original components match an existing one */
+ for (i = 0; i < nr_channels; i++) {
+ if (cmap[i].cmp >= image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
+ is_sane = OPJ_FALSE;
+ }
+ }
+
+ pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL));
+ if (!pcol_usage) {
+ opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
+ return OPJ_FALSE;
+ }
+ /* verify that no component is targeted more than once */
+ for (i = 0; i < nr_channels; i++) {
+ OPJ_UINT16 pcol = cmap[i].pcol;
+ assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
+ if (pcol >= nr_channels) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
+ is_sane = OPJ_FALSE;
+ }
+ else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
+ opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
+ is_sane = OPJ_FALSE;
+ }
+ else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
+ /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
+ * the value of this field shall be 0. */
+ opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
+ is_sane = OPJ_FALSE;
+ }
+ else
+ pcol_usage[pcol] = OPJ_TRUE;
+ }
+ /* verify that all components are targeted at least once */
+ for (i = 0; i < nr_channels; i++) {
+ if (!pcol_usage[i] && cmap[i].mtyp != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
+ is_sane = OPJ_FALSE;
+ }
+ }
+ opj_free(pcol_usage);
+ if (!is_sane) {
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+/* file9.jp2 */
+void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
+{
+ opj_image_comp_t *old_comps, *new_comps;
+ OPJ_BYTE *channel_size, *channel_sign;
+ OPJ_UINT32 *entries;
+ opj_jp2_cmap_comp_t *cmap;
+ OPJ_INT32 *src, *dst;
+ OPJ_UINT32 j, max;
+ OPJ_UINT16 i, nr_channels, cmp, pcol;
+ OPJ_INT32 k, top_k;
+
+ channel_size = color->jp2_pclr->channel_size;
+ channel_sign = color->jp2_pclr->channel_sign;
+ entries = color->jp2_pclr->entries;
+ cmap = color->jp2_pclr->cmap;
+ nr_channels = color->jp2_pclr->nr_channels;
+
+ old_comps = image->comps;
+ new_comps = (opj_image_comp_t*)
+ opj_malloc(nr_channels * sizeof(opj_image_comp_t));
+
+ for(i = 0; i < nr_channels; ++i) {
+ pcol = cmap[i].pcol; cmp = cmap[i].cmp;
+
+ /* Direct use */
+ if(cmap[i].mtyp == 0){
+ assert( pcol == 0 );
+ new_comps[i] = old_comps[cmp];
+ } else {
+ assert( i == pcol );
+ new_comps[pcol] = old_comps[cmp];
+ }
+
+ /* Palette mapping: */
+ new_comps[i].data = (OPJ_INT32*)
+ opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
+ new_comps[i].prec = channel_size[i];
+ new_comps[i].sgnd = channel_sign[i];
+ }
+
+ top_k = color->jp2_pclr->nr_entries - 1;
+
+ for(i = 0; i < nr_channels; ++i) {
+ /* Palette mapping: */
+ cmp = cmap[i].cmp; pcol = cmap[i].pcol;
+ src = old_comps[cmp].data;
+ assert( src );
+ max = new_comps[pcol].w * new_comps[pcol].h;
+
+ /* Direct use: */
+ if(cmap[i].mtyp == 0) {
+ assert( cmp == 0 );
+ dst = new_comps[i].data;
+ assert( dst );
+ for(j = 0; j < max; ++j) {
+ dst[j] = src[j];
+ }
+ }
+ else {
+ assert( i == pcol );
+ dst = new_comps[pcol].data;
+ assert( dst );
+ for(j = 0; j < max; ++j) {
+ /* The index */
+ if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
+
+ /* The colour */
+ dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
+ }
+ }
+ }
+
+ max = image->numcomps;
+ for(i = 0; i < max; ++i) {
+ if(old_comps[i].data) opj_free(old_comps[i].data);
+ }
+
+ opj_free(old_comps);
+ image->comps = new_comps;
+ image->numcomps = nr_channels;
+
+ opj_jp2_free_pclr(color);
+
+}/* apply_pclr() */
+
+OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
+ OPJ_BYTE * p_pclr_header_data,
+ OPJ_UINT32 p_pclr_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_pclr_t *jp2_pclr;
+ OPJ_BYTE *channel_size, *channel_sign;
+ OPJ_UINT32 *entries;
+ OPJ_UINT16 nr_entries,nr_channels;
+ OPJ_UINT16 i, j;
+ OPJ_UINT32 l_value;
+ OPJ_BYTE *orig_header_data = p_pclr_header_data;
+
+ /* preconditions */
+ assert(p_pclr_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+ (void)p_pclr_header_size;
+
+ if(jp2->color.jp2_pclr)
+ return OPJ_FALSE;
+
+ if (p_pclr_header_size < 3)
+ return OPJ_FALSE;
+
+ opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
+ p_pclr_header_data += 2;
+ nr_entries = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
+ ++p_pclr_header_data;
+ nr_channels = (OPJ_UINT16) l_value;
+
+ if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels || nr_channels == 0 || nr_entries >= (OPJ_UINT32)-1 / nr_channels)
+ return OPJ_FALSE;
+
+ entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32));
+ if (!entries)
+ return OPJ_FALSE;
+ channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
+ if (!channel_size)
+ {
+ opj_free(entries);
+ return OPJ_FALSE;
+ }
+ channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
+ if (!channel_sign)
+ {
+ opj_free(entries);
+ opj_free(channel_size);
+ return OPJ_FALSE;
+ }
+
+ jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
+ if (!jp2_pclr)
+ {
+ opj_free(entries);
+ opj_free(channel_size);
+ opj_free(channel_sign);
+ return OPJ_FALSE;
+ }
+
+ jp2_pclr->channel_sign = channel_sign;
+ jp2_pclr->channel_size = channel_size;
+ jp2_pclr->entries = entries;
+ jp2_pclr->nr_entries = nr_entries;
+ jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
+ jp2_pclr->cmap = NULL;
+
+ jp2->color.jp2_pclr = jp2_pclr;
+
+ for(i = 0; i < nr_channels; ++i) {
+ opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
+ ++p_pclr_header_data;
+
+ channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
+ channel_sign[i] = (l_value & 0x80) ? 1 : 0;
+ }
+
+ for(j = 0; j < nr_entries; ++j) {
+ for(i = 0; i < nr_channels; ++i) {
+ OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i]+7)>>3);
+
+ if (bytes_to_read > sizeof(OPJ_UINT32))
+ bytes_to_read = sizeof(OPJ_UINT32);
+ if ((ptrdiff_t)p_pclr_header_size < p_pclr_header_data - orig_header_data + (ptrdiff_t)bytes_to_read)
+ return OPJ_FALSE;
+
+ opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
+ p_pclr_header_data += bytes_to_read;
+ *entries = (OPJ_UINT32) l_value;
+ entries++;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
+ OPJ_BYTE * p_cmap_header_data,
+ OPJ_UINT32 p_cmap_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_cmap_comp_t *cmap;
+ OPJ_BYTE i, nr_channels;
+ OPJ_UINT32 l_value;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_cmap_header_data != 00);
+ assert(p_manager != 00);
+ (void)p_cmap_header_size;
+
+ /* Need nr_channels: */
+ if(jp2->color.jp2_pclr == NULL) {
+ opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
+ return OPJ_FALSE;
+ }
+
+ /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
+ * inside a JP2 Header box' :
+ */
+ if(jp2->color.jp2_pclr->cmap) {
+ opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
+ return OPJ_FALSE;
+ }
+
+ nr_channels = jp2->color.jp2_pclr->nr_channels;
+ if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
+ opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
+ return OPJ_FALSE;
+ }
+
+ cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
+ if (!cmap)
+ return OPJ_FALSE;
+
+
+ for(i = 0; i < nr_channels; ++i) {
+ opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
+ p_cmap_header_data +=2;
+ cmap[i].cmp = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
+ ++p_cmap_header_data;
+ cmap[i].mtyp = (OPJ_BYTE) l_value;
+
+ opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
+ ++p_cmap_header_data;
+ cmap[i].pcol = (OPJ_BYTE) l_value;
+ }
+
+ jp2->color.jp2_pclr->cmap = cmap;
+
+ return OPJ_TRUE;
+}
+
+void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
+{
+ opj_jp2_cdef_info_t *info;
+ OPJ_UINT16 i, n, cn, asoc, acn;
+
+ info = color->jp2_cdef->info;
+ n = color->jp2_cdef->n;
+
+ for(i = 0; i < n; ++i)
+ {
+ /* WATCH: acn = asoc - 1 ! */
+ asoc = info[i].asoc;
+ if(asoc == 0 || asoc == 65535)
+ {
+ if (i < image->numcomps)
+ image->comps[i].alpha = info[i].typ;
+ continue;
+ }
+
+ cn = info[i].cn;
+ acn = (OPJ_UINT16)(asoc - 1);
+ if( cn >= image->numcomps || acn >= image->numcomps )
+ {
+ fprintf(stderr, "cn=%d, acn=%d, numcomps=%d\n", cn, acn, image->numcomps);
+ continue;
+ }
+
+ if(cn != acn)
+ {
+ opj_image_comp_t saved;
+
+ memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
+ memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
+ memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
+
+ info[i].asoc = (OPJ_UINT16)(cn + 1);
+ info[acn].asoc = (OPJ_UINT16)(info[acn].cn + 1);
+ }
+
+ image->comps[cn].alpha = info[i].typ;
+ }
+
+ if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
+
+ opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
+
+}/* jp2_apply_cdef() */
+
+OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
+ OPJ_BYTE * p_cdef_header_data,
+ OPJ_UINT32 p_cdef_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_cdef_info_t *cdef_info;
+ OPJ_UINT16 i;
+ OPJ_UINT32 l_value;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_cdef_header_data != 00);
+ assert(p_manager != 00);
+ (void)p_cdef_header_size;
+
+ /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
+ * inside a JP2 Header box.'*/
+ if(jp2->color.jp2_cdef) return OPJ_FALSE;
+
+ if (p_cdef_header_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
+ p_cdef_header_data+= 2;
+
+ if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
+ opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
+ opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
+ return OPJ_FALSE;
+ }
+
+ cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
+ if (!cdef_info)
+ return OPJ_FALSE;
+
+ jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
+ if(!jp2->color.jp2_cdef)
+ {
+ opj_free(cdef_info);
+ return OPJ_FALSE;
+ }
+ jp2->color.jp2_cdef->info = cdef_info;
+ jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
+
+ for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].cn = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].typ = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].asoc = (OPJ_UINT16) l_value;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
+ OPJ_BYTE * p_colr_header_data,
+ OPJ_UINT32 p_colr_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_value;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_colr_header_data != 00);
+ assert(p_manager != 00);
+
+ if (p_colr_header_size < 3) {
+ opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
+ return OPJ_FALSE;
+ }
+
+ /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
+ * Specification boxes after the first.'
+ */
+ if(jp2->color.jp2_has_colr) {
+ opj_event_msg(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
+ p_colr_header_data += p_colr_header_size;
+ return OPJ_TRUE;
+ }
+
+ opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
+ ++p_colr_header_data;
+
+ opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
+ ++p_colr_header_data;
+
+ opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
+ ++p_colr_header_data;
+
+ if (jp2->meth == 1) {
+ if (p_colr_header_size > 7) {
+ /* testcase Altona_Technical_v20_x4.pdf */
+ opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
+ }
+
+ opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
+
+ p_colr_header_data += 4;
+
+ if(jp2->enumcs == 14)/* CIELab */
+ {
+ OPJ_UINT32 *cielab;
+ OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
+
+ cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
+ cielab[0] = 14; /* enumcs */
+
+ if(p_colr_header_size == 7)/* default values */
+ {
+ rl = ra = rb = ol = oa = ob = 0;
+ il = 0x00443530; /* D50 */
+ cielab[1] = 0x44454600;/* DEF */
+ }
+ else if(p_colr_header_size == 35)
+ {
+ opj_read_bytes(p_colr_header_data, &rl, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &ol, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &ra, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &oa, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &rb, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &ob, 4);
+ p_colr_header_data += 4;
+ opj_read_bytes(p_colr_header_data, &il, 4);
+ p_colr_header_data += 4;
+ cielab[1] = 0;
+ }
+ cielab[2] = rl; cielab[4] = ra; cielab[6] = rb;
+ cielab[3] = ol; cielab[5] = oa; cielab[7] = ob;
+ cielab[8] = il;
+
+ jp2->color.icc_profile_buf = (unsigned char*)cielab;
+ jp2->color.icc_profile_len = 0;
+ }
+
+ jp2->color.jp2_has_colr = 1;
+ }
+ else if (jp2->meth == 2) {
+ /* ICC profile */
+ OPJ_INT32 it_icc_value = 0;
+ OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
+
+ jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
+ jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_malloc((size_t)icc_len);
+ if (!jp2->color.icc_profile_buf)
+ {
+ jp2->color.icc_profile_len = 0;
+ return OPJ_FALSE;
+ }
+ memset(jp2->color.icc_profile_buf, 0, (size_t)icc_len * sizeof(OPJ_BYTE));
+
+ for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
+ {
+ opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
+ ++p_colr_header_data;
+ jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
+ }
+
+ jp2->color.jp2_has_colr = 1;
+ }
+ else if (jp2->meth > 2)
+ {
+ /* ISO/IEC 15444-1:2004 (E), Table I.9 ?Legal METH values:
+ conforming JP2 reader shall ignore the entire Colour Specification box.*/
+ opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
+ "so we will ignore the entire Colour Specification box. \n", jp2->meth);
+ }
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager)
+{
+ if (!p_image)
+ return OPJ_FALSE;
+
+ /* J2K decoding */
+ if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
+ return OPJ_FALSE;
+ }
+
+ if (!jp2->ignore_pclr_cmap_cdef){
+ if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* Set Image Color Space */
+ if (jp2->enumcs == 16)
+ p_image->color_space = OPJ_CLRSPC_SRGB;
+ else if (jp2->enumcs == 17)
+ p_image->color_space = OPJ_CLRSPC_GRAY;
+ else if (jp2->enumcs == 18)
+ p_image->color_space = OPJ_CLRSPC_SYCC;
+ else if (jp2->enumcs == 24)
+ p_image->color_space = OPJ_CLRSPC_EYCC;
+ else
+ p_image->color_space = OPJ_CLRSPC_UNKNOWN;
+
+ /* Apply the color space if needed */
+ if(jp2->color.jp2_cdef) {
+ opj_jp2_apply_cdef(p_image, &(jp2->color));
+ }
+
+ if(jp2->color.jp2_pclr) {
+ /* Part 1, I.5.3.4: Either both or none : */
+ if( !jp2->color.jp2_pclr->cmap)
+ opj_jp2_free_pclr(&(jp2->color));
+ else
+ if(!p_image->useColorSpace)
+ opj_jp2_apply_pclr(p_image, &(jp2->color));
+ }
+
+ if(jp2->color.icc_profile_buf) {
+ p_image->icc_profile_buf = jp2->color.icc_profile_buf;
+ p_image->icc_profile_len = jp2->color.icc_profile_len;
+ jp2->color.icc_profile_buf = NULL;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_img_header_writer_handler_t l_writers [3];
+ opj_jp2_img_header_writer_handler_t * l_current_writer;
+
+ OPJ_INT32 i, l_nb_pass;
+ /* size of data for super box*/
+ OPJ_UINT32 l_jp2h_size = 8;
+ OPJ_BOOL l_result = OPJ_TRUE;
+
+ /* to store the data of the super box */
+ OPJ_BYTE l_jp2h_data [8];
+
+ /* preconditions */
+ assert(stream != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ memset(l_writers,0,sizeof(l_writers));
+
+ if (jp2->bpc == 255) {
+ l_nb_pass = 3;
+ l_writers[0].handler = opj_jp2_write_ihdr;
+ l_writers[1].handler = opj_jp2_write_bpcc;
+ l_writers[2].handler = opj_jp2_write_colr;
+ }
+ else {
+ l_nb_pass = 2;
+ l_writers[0].handler = opj_jp2_write_ihdr;
+ l_writers[1].handler = opj_jp2_write_colr;
+ }
+
+ /* write box header */
+ /* write JP2H type */
+ opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
+
+ l_current_writer = l_writers;
+ for (i=0;i<l_nb_pass;++i) {
+ l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
+ if (l_current_writer->m_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+
+ l_jp2h_size += l_current_writer->m_size;
+ ++l_current_writer;
+ }
+
+ if (! l_result) {
+ l_current_writer = l_writers;
+ for (i=0;i<l_nb_pass;++i) {
+ if (l_current_writer->m_data != 00) {
+ opj_free(l_current_writer->m_data );
+ }
+ ++l_current_writer;
+ }
+
+ return OPJ_FALSE;
+ }
+
+ /* write super box size */
+ opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
+
+ /* write super box data on stream */
+ if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
+ l_result = OPJ_FALSE;
+ }
+
+ if (l_result) {
+ l_current_writer = l_writers;
+ for (i=0;i<l_nb_pass;++i) {
+ if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
+ l_result = OPJ_FALSE;
+ break;
+ }
+ ++l_current_writer;
+ }
+ }
+
+ l_current_writer = l_writers;
+
+ /* cleanup */
+ for (i=0;i<l_nb_pass;++i) {
+ if (l_current_writer->m_data != 00) {
+ opj_free(l_current_writer->m_data );
+ }
+ ++l_current_writer;
+ }
+
+ return l_result;
+}
+
+OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
+ OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
+ OPJ_BOOL l_result;
+
+ /* preconditions */
+ assert(cio != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ l_ftyp_data = (OPJ_BYTE *) opj_malloc(l_ftyp_size);
+
+ if (l_ftyp_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
+ return OPJ_FALSE;
+ }
+
+ memset(l_ftyp_data,0,l_ftyp_size);
+
+ l_current_data_ptr = l_ftyp_data;
+
+ opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
+ l_current_data_ptr += 4;
+
+ opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
+ l_current_data_ptr += 4;
+
+ opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
+ l_current_data_ptr += 4;
+
+ opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
+ l_current_data_ptr += 4;
+
+ for (i = 0; i < jp2->numcl; i++) {
+ opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
+ }
+
+ l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
+ if (! l_result)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
+ }
+
+ opj_free(l_ftyp_data);
+
+ return l_result;
+}
+
+OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_OFF_T j2k_codestream_exit;
+ OPJ_BYTE l_data_header [8];
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+ assert(opj_stream_has_seek(cio));
+
+ j2k_codestream_exit = opj_stream_tell(cio);
+ opj_write_bytes(l_data_header,
+ (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
+ 4); /* size of codestream */
+ opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
+
+ if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ /* 12 bytes will be read */
+ OPJ_BYTE l_signature_data [12];
+
+ /* preconditions */
+ assert(cio != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ /* write box length */
+ opj_write_bytes(l_signature_data,12,4);
+ /* writes box type */
+ opj_write_bytes(l_signature_data+4,JP2_JP,4);
+ /* writes magic number*/
+ opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
+
+ if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+/* JP2 decoder interface */
+/* ----------------------------------------------------------------------- */
+
+void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
+{
+ /* setup the J2K codec */
+ opj_j2k_setup_decoder(jp2->j2k, parameters);
+
+ /* further JP2 initializations go here */
+ jp2->color.jp2_has_colr = 0;
+ jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
+}
+
+/* ----------------------------------------------------------------------- */
+/* JP2 encoder interface */
+/* ----------------------------------------------------------------------- */
+
+void opj_jp2_setup_encoder( opj_jp2_t *jp2,
+ opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 depth_0;
+ OPJ_UINT32 sign;
+
+ if(!jp2 || !parameters || !image)
+ return;
+
+ /* setup the J2K codec */
+ /* ------------------- */
+
+ /* Check if number of components respects standard */
+ if (image->numcomps < 1 || image->numcomps > 16384) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
+ return;
+ }
+
+ opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager );
+
+ /* setup the JP2 codec */
+ /* ------------------- */
+
+ /* Profile box */
+
+ jp2->brand = JP2_JP2; /* BR */
+ jp2->minversion = 0; /* MinV */
+ jp2->numcl = 1;
+ jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
+ if (!jp2->cl){
+ jp2->cl = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
+ return;
+ }
+ jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
+
+ /* Image Header box */
+
+ jp2->numcomps = image->numcomps; /* NC */
+ jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
+ if (!jp2->comps) {
+ jp2->comps = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
+ return;
+ }
+
+ jp2->h = image->y1 - image->y0; /* HEIGHT */
+ jp2->w = image->x1 - image->x0; /* WIDTH */
+ /* BPC */
+ depth_0 = image->comps[0].prec - 1;
+ sign = image->comps[0].sgnd;
+ jp2->bpc = depth_0 + (sign << 7);
+ for (i = 1; i < image->numcomps; i++) {
+ OPJ_UINT32 depth = image->comps[i].prec - 1;
+ sign = image->comps[i].sgnd;
+ if (depth_0 != depth)
+ jp2->bpc = 255;
+ }
+ jp2->C = 7; /* C : Always 7 */
+ jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
+ jp2->IPR = 0; /* IPR, no intellectual property */
+
+ /* BitsPerComponent box */
+ for (i = 0; i < image->numcomps; i++) {
+ jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
+ }
+
+ /* Colour Specification box */
+ if(image->icc_profile_len) {
+ jp2->meth = 2;
+ jp2->enumcs = 0;
+ }
+ else {
+ jp2->meth = 1;
+ if (image->color_space == 1)
+ jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
+ else if (image->color_space == 2)
+ jp2->enumcs = 17; /* greyscale */
+ else if (image->color_space == 3)
+ jp2->enumcs = 18; /* YUV */
+ }
+
+
+ jp2->precedence = 0; /* PRECEDENCE */
+ jp2->approx = 0; /* APPROX */
+
+ jp2->jpip_on = parameters->jpip_on;
+}
+
+OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager)
+{
+ return opj_j2k_encode(jp2->j2k, stream, p_manager);
+}
+
+OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* customization of the end encoding */
+ opj_jp2_setup_end_header_reading(jp2);
+
+ /* write header */
+ if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
+}
+
+OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* customization of the end encoding */
+ opj_jp2_setup_end_header_writing(jp2);
+
+ if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* write header */
+ return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
+}
+
+void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+
+#ifdef USE_JPIP
+ if( jp2->jpip_on )
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr );
+#endif
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
+ /* DEVELOPER CORNER, add your custom procedures */
+#ifdef USE_JPIP
+ if( jp2->jpip_on )
+ {
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx );
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx );
+ }
+#endif
+}
+
+void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
+ /* DEVELOPER CORNER, add your custom procedures */
+}
+
+OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_BOOL l_is_valid = OPJ_TRUE;
+ OPJ_UINT32 i;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* JPEG2000 codec validation */
+
+ /* STATE checking */
+ /* make sure the state is at 0 */
+ l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
+
+ /* make sure not reading a jp2h ???? WEIRD */
+ l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
+
+ /* POINTER validation */
+ /* make sure a j2k codec is present */
+ l_is_valid &= (jp2->j2k != 00);
+
+ /* make sure a procedure list is present */
+ l_is_valid &= (jp2->m_procedure_list != 00);
+
+ /* make sure a validation list is present */
+ l_is_valid &= (jp2->m_validation_list != 00);
+
+ /* PARAMETER VALIDATION */
+ /* number of components */
+ l_is_valid &= (jp2->numcl > 0);
+ /* width */
+ l_is_valid &= (jp2->h > 0);
+ /* height */
+ l_is_valid &= (jp2->w > 0);
+ /* precision */
+ for (i = 0; i < jp2->numcomps; ++i) {
+ l_is_valid &= (jp2->comps[i].bpcc > 0);
+ }
+
+ /* METH */
+ l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
+
+ /* stream validation */
+ /* back and forth is needed */
+ l_is_valid &= opj_stream_has_seek(cio);
+
+ return l_is_valid;
+}
+
+OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_box_t box;
+ OPJ_UINT32 l_nb_bytes_read;
+ const opj_jp2_header_handler_t * l_current_handler;
+ OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
+ OPJ_UINT32 l_current_data_size;
+ OPJ_BYTE * l_current_data = 00;
+
+ /* preconditions */
+ assert(stream != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ l_current_data = (OPJ_BYTE*)opj_malloc(l_last_data_size);
+
+ if (l_current_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
+ return OPJ_FALSE;
+ }
+ memset(l_current_data, 0 , l_last_data_size);
+
+ while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
+ /* is it the codestream box ? */
+ if (box.type == JP2_JP2C) {
+ if (jp2->jp2_state & JP2_STATE_HEADER) {
+ jp2->jp2_state |= JP2_STATE_CODESTREAM;
+ opj_free(l_current_data);
+ return OPJ_TRUE;
+ }
+ else {
+ opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ else if (box.length == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ /* testcase 1851.pdf.SIGSEGV.ce9.948 */
+ /* testcase K-5787457125613568 */
+ else if (box.length < l_nb_bytes_read || box.length > opj_stream_get_number_byte_left(stream)) {
+ opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ l_current_handler = opj_jp2_find_handler(box.type);
+ l_current_data_size = box.length - l_nb_bytes_read;
+
+ if (l_current_handler != 00) {
+ if (l_current_data_size > l_last_data_size) {
+ OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
+ if (!new_current_data) {
+ opj_free(l_current_data);
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
+ return OPJ_FALSE;
+ }
+ l_current_data = new_current_data;
+ l_last_data_size = l_current_data_size;
+ }
+
+ l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
+ if (l_nb_bytes_read != l_current_data_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ jp2->jp2_state |= JP2_STATE_UNKNOWN;
+ if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ }
+
+ opj_free(l_current_data);
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Excutes the given procedures on the given codec.
+ *
+ * @param p_procedure_list the list of procedures to execute
+ * @param jp2 the jpeg2000 file codec to execute the procedures on.
+ * @param stream the stream to execute the procedures on.
+ * @param p_manager the user manager.
+ *
+ * @return true if all the procedures were successfully executed.
+ */
+static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
+ opj_procedure_list_t * p_procedure_list,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager
+ )
+
+{
+ OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
+ OPJ_BOOL l_result = OPJ_TRUE;
+ OPJ_UINT32 l_nb_proc, i;
+
+ /* preconditions */
+ assert(p_procedure_list != 00);
+ assert(jp2 != 00);
+ assert(stream != 00);
+ assert(p_manager != 00);
+
+ l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
+ l_procedure = (OPJ_BOOL (**) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
+
+ for (i=0;i<l_nb_proc;++i) {
+ l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
+ ++l_procedure;
+ }
+
+ /* and clear the procedure list at the end. */
+ opj_procedure_list_clear(p_procedure_list);
+ return l_result;
+}
+
+OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(stream != 00);
+ assert(p_manager != 00);
+
+ /* customization of the validation */
+ opj_jp2_setup_encoding_validation (jp2);
+
+ /* validation of the parameters codec */
+ if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* customization of the encoding */
+ opj_jp2_setup_header_writing(jp2);
+
+ /* write header */
+ if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
+}
+
+const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
+{
+ OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
+
+ for (i=0;i<l_handler_size;++i) {
+ if (jp2_header[i].id == p_id) {
+ return &jp2_header[i];
+ }
+ }
+ return NULL;
+}
+
+/**
+ * Finds the image execution function related to the given box id.
+ *
+ * @param p_id the id of the handler to fetch.
+ *
+ * @return the given handler or 00 if it could not be found.
+ */
+static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
+{
+ OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
+ for (i=0;i<l_handler_size;++i)
+ {
+ if (jp2_img_header[i].id == p_id) {
+ return &jp2_img_header[i];
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * Reads a jpeg2000 file signature box.
+ *
+ * @param p_header_data the data contained in the signature box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the signature box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the file signature box is valid.
+ */
+static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+
+{
+ OPJ_UINT32 l_magic_number;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ if (jp2->jp2_state != JP2_STATE_NONE) {
+ opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ /* assure length of data is correct (4 -> magic number) */
+ if (p_header_size != 4) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ /* rearrange data */
+ opj_read_bytes(p_header_data,&l_magic_number,4);
+ if (l_magic_number != 0x0d0a870a ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
+ return OPJ_FALSE;
+ }
+
+ jp2->jp2_state |= JP2_STATE_SIGNATURE;
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a a FTYP box - File type box
+ *
+ * @param p_header_data the data contained in the FTYP box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the FTYP box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the FTYP box is valid.
+ */
+static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i, l_remaining_bytes;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
+ opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ /* assure length of data is correct */
+ if (p_header_size < 8) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
+ p_header_data += 4;
+
+ opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
+ p_header_data += 4;
+
+ l_remaining_bytes = p_header_size - 8;
+
+ /* the number of remaining bytes should be a multiple of 4 */
+ if ((l_remaining_bytes & 0x3) != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ /* div by 4 */
+ jp2->numcl = l_remaining_bytes >> 2;
+ if (jp2->numcl) {
+ jp2->cl = (OPJ_UINT32 *) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
+ if (jp2->cl == 00) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
+ return OPJ_FALSE;
+ }
+ memset(jp2->cl,0,jp2->numcl * sizeof(OPJ_UINT32));
+ }
+
+ for (i = 0; i < jp2->numcl; ++i)
+ {
+ opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
+ p_header_data += 4;
+ }
+
+ jp2->jp2_state |= JP2_STATE_FILE_TYPE;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(stream != 00);
+ assert(p_manager != 00);
+
+ jp2->j2k_codestream_offset = opj_stream_tell(stream);
+
+ if (opj_stream_skip(stream,8,p_manager) != 8) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(stream != 00);
+ assert(p_manager != 00);
+
+ jp2->jpip_iptr_offset = opj_stream_tell(stream);
+
+ if (opj_stream_skip(stream,24,p_manager) != 24) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
+ *
+ * @param p_header_data the data contained in the file header box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the file header box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the JP2 Header box was successfully reconized.
+*/
+static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
+ OPJ_BYTE *p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
+ opj_jp2_box_t box;
+ const opj_jp2_header_handler_t * l_current_handler;
+
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ /* make sure the box is well placed */
+ if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
+ opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ jp2->jp2_img_state = JP2_IMG_STATE_NONE;
+
+ /* iterate while remaining data */
+ while (p_header_size > 0) {
+
+ if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
+ return OPJ_FALSE;
+ }
+
+ if (box.length > p_header_size) {
+ opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
+ return OPJ_FALSE;
+ }
+
+ l_current_handler = opj_jp2_img_find_handler(box.type);
+ //BUGID:0055999
+ //test file: fuzz-signal_sigsegv_6b88de_1123_2509.pdf
+ if (box.length < l_box_size) return OPJ_FALSE;
+ l_current_data_size = box.length - l_box_size;
+ p_header_data += l_box_size;
+
+ if (l_current_handler != 00) {
+ if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
+ }
+
+ p_header_data += l_current_data_size;
+ p_header_size -= box.length;
+ }
+
+ jp2->jp2_state |= JP2_STATE_HEADER;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_number_bytes_read,
+ OPJ_UINT32 p_box_max_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_value;
+
+ /* preconditions */
+ assert(p_data != 00);
+ assert(box != 00);
+ assert(p_number_bytes_read != 00);
+ assert(p_manager != 00);
+
+ if (p_box_max_size < 8) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
+ return OPJ_FALSE;
+ }
+
+ /* process read data */
+ opj_read_bytes(p_data, &l_value, 4);
+ p_data += 4;
+ box->length = (OPJ_UINT32)(l_value);
+
+ opj_read_bytes(p_data, &l_value, 4);
+ p_data += 4;
+ box->type = (OPJ_UINT32)(l_value);
+
+ *p_number_bytes_read = 8;
+
+ /* do we have a "special very large box ?" */
+ /* read then the XLBox */
+ if (box->length == 1) {
+ OPJ_UINT32 l_xl_part_size;
+
+ if (p_box_max_size < 16) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_data,&l_xl_part_size, 4);
+ p_data += 4;
+ *p_number_bytes_read += 4;
+
+ if (l_xl_part_size != 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_data, &l_value, 4);
+ *p_number_bytes_read += 4;
+ box->length = (OPJ_UINT32)(l_value);
+
+ if (box->length == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ return OPJ_FALSE;
+ }
+ }
+ else if (box->length == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
+ opj_jp2_t *jp2,
+ opj_image_t ** p_image,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ /* customization of the validation */
+ opj_jp2_setup_decoding_validation (jp2);
+
+ /* customization of the encoding */
+ opj_jp2_setup_header_reading(jp2);
+
+ /* validation of the parameters codec */
+ if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* read header */
+ if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return opj_j2k_read_header( p_stream,
+ jp2->j2k,
+ p_image,
+ p_manager);
+}
+
+void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+
+ opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
+ /* DEVELOPER CORNER, add your custom validation procedure */
+}
+
+void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ /* DEVELOPER CORNER, add your custom validation procedure */
+}
+
+void opj_jp2_setup_header_writing (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
+ if( jp2->jpip_on )
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr );
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
+
+ /* DEVELOPER CORNER, insert your custom procedures */
+
+}
+
+void opj_jp2_setup_header_reading (opj_jp2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
+ /* DEVELOPER CORNER, add your custom procedures */
+}
+
+OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0,
+ OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1,
+ OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_go_on,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ return opj_j2k_read_tile_header(p_jp2->j2k,
+ p_tile_index,
+ p_data_size,
+ p_tile_x0, p_tile_y0,
+ p_tile_x1, p_tile_y1,
+ p_nb_comps,
+ p_go_on,
+ p_stream,
+ p_manager);
+}
+
+OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+
+{
+ return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
+}
+
+OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
+}
+
+void opj_jp2_destroy(opj_jp2_t *jp2)
+{
+ if (jp2) {
+ /* destroy the J2K codec */
+ opj_j2k_destroy(jp2->j2k);
+ jp2->j2k = 00;
+
+ if (jp2->comps) {
+ opj_free(jp2->comps);
+ jp2->comps = 00;
+ }
+
+ if (jp2->cl) {
+ opj_free(jp2->cl);
+ jp2->cl = 00;
+ }
+
+ if (jp2->color.icc_profile_buf) {
+ opj_free(jp2->color.icc_profile_buf);
+ jp2->color.icc_profile_buf = 00;
+ }
+
+ if (jp2->color.jp2_cdef) {
+ if (jp2->color.jp2_cdef->info) {
+ opj_free(jp2->color.jp2_cdef->info);
+ jp2->color.jp2_cdef->info = NULL;
+ }
+
+ opj_free(jp2->color.jp2_cdef);
+ jp2->color.jp2_cdef = 00;
+ }
+
+ if (jp2->color.jp2_pclr) {
+ if (jp2->color.jp2_pclr->cmap) {
+ opj_free(jp2->color.jp2_pclr->cmap);
+ jp2->color.jp2_pclr->cmap = NULL;
+ }
+ if (jp2->color.jp2_pclr->channel_sign) {
+ opj_free(jp2->color.jp2_pclr->channel_sign);
+ jp2->color.jp2_pclr->channel_sign = NULL;
+ }
+ if (jp2->color.jp2_pclr->channel_size) {
+ opj_free(jp2->color.jp2_pclr->channel_size);
+ jp2->color.jp2_pclr->channel_size = NULL;
+ }
+ if (jp2->color.jp2_pclr->entries) {
+ opj_free(jp2->color.jp2_pclr->entries);
+ jp2->color.jp2_pclr->entries = NULL;
+ }
+
+ opj_free(jp2->color.jp2_pclr);
+ jp2->color.jp2_pclr = 00;
+ }
+
+ if (jp2->m_validation_list) {
+ opj_procedure_list_destroy(jp2->m_validation_list);
+ jp2->m_validation_list = 00;
+ }
+
+ if (jp2->m_procedure_list) {
+ opj_procedure_list_destroy(jp2->m_procedure_list);
+ jp2->m_procedure_list = 00;
+ }
+
+ opj_free(jp2);
+ }
+}
+
+OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
+ opj_event_mgr_t * p_manager
+ )
+{
+ return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
+}
+
+OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager,
+ OPJ_UINT32 tile_index
+ )
+{
+ if (!p_image)
+ return OPJ_FALSE;
+
+ opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
+
+ if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
+ return OPJ_FALSE;
+ }
+
+ if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* Set Image Color Space */
+ if (p_jp2->enumcs == 16)
+ p_image->color_space = OPJ_CLRSPC_SRGB;
+ else if (p_jp2->enumcs == 17)
+ p_image->color_space = OPJ_CLRSPC_GRAY;
+ else if (p_jp2->enumcs == 18)
+ p_image->color_space = OPJ_CLRSPC_SYCC;
+ else
+ p_image->color_space = OPJ_CLRSPC_UNKNOWN;
+
+ /* Apply the color space if needed */
+ if(p_jp2->color.jp2_cdef) {
+ opj_jp2_apply_cdef(p_image, &(p_jp2->color));
+ }
+
+ if(p_jp2->color.jp2_pclr) {
+ /* Part 1, I.5.3.4: Either both or none : */
+ if( !p_jp2->color.jp2_pclr->cmap)
+ opj_jp2_free_pclr(&(p_jp2->color));
+ else
+ opj_jp2_apply_pclr(p_image, &(p_jp2->color));
+ }
+
+ if(p_jp2->color.icc_profile_buf) {
+ p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
+ p_image->icc_profile_len = p_jp2->color.icc_profile_len;
+ p_jp2->color.icc_profile_buf = NULL;
+ }
+
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+/* JP2 encoder interface */
+/* ----------------------------------------------------------------------- */
+
+opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
+{
+ opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
+ if (jp2) {
+ memset(jp2,0,sizeof(opj_jp2_t));
+
+ /* create the J2K codec */
+ if (! p_is_decoder) {
+ jp2->j2k = opj_j2k_create_compress();
+ }
+ else {
+ jp2->j2k = opj_j2k_create_decompress();
+ }
+
+ if (jp2->j2k == 00) {
+ opj_jp2_destroy(jp2);
+ return 00;
+ }
+
+ /* Color structure */
+ jp2->color.icc_profile_buf = NULL;
+ jp2->color.icc_profile_len = 0;
+ jp2->color.jp2_cdef = NULL;
+ jp2->color.jp2_pclr = NULL;
+ jp2->color.jp2_has_colr = 0;
+
+ /* validation list creation */
+ jp2->m_validation_list = opj_procedure_list_create();
+ if (! jp2->m_validation_list) {
+ opj_jp2_destroy(jp2);
+ return 00;
+ }
+
+ /* execution list creation */
+ jp2->m_procedure_list = opj_procedure_list_create();
+ if (! jp2->m_procedure_list) {
+ opj_jp2_destroy(jp2);
+ return 00;
+ }
+ }
+
+ return jp2;
+}
+
+void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
+{
+ /* preconditions */
+ assert(p_jp2 != 00);
+
+ j2k_dump(p_jp2->j2k,
+ flag,
+ out_stream);
+}
+
+opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
+{
+ return j2k_get_cstr_index(p_jp2->j2k);
+}
+
+opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
+{
+ return j2k_get_cstr_info(p_jp2->j2k);
+}
+
+OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager)
+{
+ return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
+}
+
+/* JPIP specific */
+
+#ifdef USE_JPIP
+static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_OFF_T j2k_codestream_exit;
+ OPJ_BYTE l_data_header [24];
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+ assert(opj_stream_has_seek(cio));
+
+ j2k_codestream_exit = opj_stream_tell(cio);
+ opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
+ opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
+#if 0
+ opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
+ opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
+#else
+ opj_write_double(l_data_header + 4 + 4, 0); /* offset */
+ opj_write_double(l_data_header + 8 + 8, 0); /* length */
+#endif
+
+ if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_OFF_T j2k_codestream_exit;
+ OPJ_BYTE l_data_header [24];
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+ assert(opj_stream_has_seek(cio));
+
+ opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
+ opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
+ opj_write_double(l_data_header + 4 + 4, 0); /* offset */
+ opj_write_double(l_data_header + 8 + 8, 0); /* length */
+
+ if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ j2k_codestream_exit = opj_stream_tell(cio);
+ if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_OFF_T j2k_codestream_exit;
+ OPJ_BYTE l_data_header [24];
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+ assert(opj_stream_has_seek(cio));
+
+ j2k_codestream_exit = opj_stream_tell(cio);
+ opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
+ opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
+#if 0
+ opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
+ opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
+#else
+ opj_write_double(l_data_header + 4 + 4, 0); /* offset */
+ opj_write_double(l_data_header + 8 + 8, 0); /* length */
+#endif
+
+ if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ j2k_codestream_exit = opj_stream_tell(cio);
+ if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+#if 0
+static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_BYTE l_data_header [8];
+ OPJ_OFF_T len, lenp;
+
+ lenp = opj_stream_tell(cio);
+ opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
+ opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
+ opj_stream_write_data(cio,l_data_header,4,p_manager);
+
+ opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
+ opj_stream_write_data(cio,l_data_header,8,p_manager);
+ opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
+ opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
+ opj_stream_write_data(cio,l_data_header,8,p_manager);
+
+ opj_write_bytes( l_data_header, 1, 1);/* NI */
+ opj_stream_write_data(cio,l_data_header,1,p_manager);
+
+ opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
+ opj_stream_write_data(cio,l_data_header,8,p_manager);
+ opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
+ opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
+ opj_stream_write_data(cio,l_data_header,8,p_manager);
+
+ len = opj_stream_tell(cio)-lenp;
+ opj_stream_skip(cio, lenp, p_manager);
+ opj_write_bytes(l_data_header,len,4);/* L */
+ opj_stream_write_data(cio,l_data_header,4,p_manager);
+ opj_stream_seek(cio, lenp+len,p_manager);
+}
+#endif
+
+
+#if 0
+static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ OPJ_BYTE l_data_header [4];
+ OPJ_OFF_T len, lenp;
+
+ lenp = opj_stream_tell(cio);
+ opj_stream_skip(cio, 4, p_manager);
+ opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
+ opj_stream_write_data(cio,l_data_header,4,p_manager);
+
+ write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
+
+ len = opj_stream_tell(cio)-lenp;
+ opj_stream_skip(cio, lenp, p_manager);
+ opj_write_bytes(l_data_header,len,4);/* L */
+ opj_stream_write_data(cio,l_data_header,4,p_manager);
+ opj_stream_seek(cio, lenp+len,p_manager);
+
+ return len;
+}
+#endif
+#endif /* USE_JPIP */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.h
new file mode 100644
index 0000000000..c11d2f3135
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.h
@@ -0,0 +1,495 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __JP2_H
+#define __JP2_H
+/**
+@file jp2.h
+@brief The JPEG-2000 file format Reader/Writer (JP2)
+
+*/
+
+/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
+/*@{*/
+
+/*#define JPIP_JPIP 0x6a706970*/
+
+#define JP2_JP 0x6a502020 /**< JPEG 2000 signature box */
+#define JP2_FTYP 0x66747970 /**< File type box */
+#define JP2_JP2H 0x6a703268 /**< JP2 header box (super-box) */
+#define JP2_IHDR 0x69686472 /**< Image header box */
+#define JP2_COLR 0x636f6c72 /**< Colour specification box */
+#define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */
+#define JP2_URL 0x75726c20 /**< Data entry URL box */
+#define JP2_PCLR 0x70636c72 /**< Palette box */
+#define JP2_CMAP 0x636d6170 /**< Component Mapping box */
+#define JP2_CDEF 0x63646566 /**< Channel Definition box */
+#define JP2_DTBL 0x6474626c /**< Data Reference box */
+#define JP2_BPCC 0x62706363 /**< Bits per component box */
+#define JP2_JP2 0x6a703220 /**< File type fields */
+
+/* For the future */
+/* #define JP2_RES 0x72657320 */ /**< Resolution box (super-box) */
+/* #define JP2_JP2I 0x6a703269 */ /**< Intellectual property box */
+/* #define JP2_XML 0x786d6c20 */ /**< XML box */
+/* #define JP2_UUID 0x75756994 */ /**< UUID box */
+/* #define JP2_UINF 0x75696e66 */ /**< UUID info box (super-box) */
+/* #define JP2_ULST 0x756c7374 */ /**< UUID list box */
+
+/* ----------------------------------------------------------------------- */
+
+typedef enum
+{
+ JP2_STATE_NONE = 0x0,
+ JP2_STATE_SIGNATURE = 0x1,
+ JP2_STATE_FILE_TYPE = 0x2,
+ JP2_STATE_HEADER = 0x4,
+ JP2_STATE_CODESTREAM = 0x8,
+ JP2_STATE_END_CODESTREAM = 0x10,
+ JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
+}
+JP2_STATE;
+
+typedef enum
+{
+ JP2_IMG_STATE_NONE = 0x0,
+ JP2_IMG_STATE_UNKNOWN = 0x7fffffff
+}
+JP2_IMG_STATE;
+
+/**
+Channel description: channel index, type, assocation
+*/
+typedef struct opj_jp2_cdef_info
+{
+ OPJ_UINT16 cn, typ, asoc;
+} opj_jp2_cdef_info_t;
+
+/**
+Channel descriptions and number of descriptions
+*/
+typedef struct opj_jp2_cdef
+{
+ opj_jp2_cdef_info_t *info;
+ OPJ_UINT16 n;
+} opj_jp2_cdef_t;
+
+/**
+Component mappings: channel index, mapping type, palette index
+*/
+typedef struct opj_jp2_cmap_comp
+{
+ OPJ_UINT16 cmp;
+ OPJ_BYTE mtyp, pcol;
+} opj_jp2_cmap_comp_t;
+
+/**
+Palette data: table entries, palette columns
+*/
+typedef struct opj_jp2_pclr
+{
+ OPJ_UINT32 *entries;
+ OPJ_BYTE *channel_sign;
+ OPJ_BYTE *channel_size;
+ opj_jp2_cmap_comp_t *cmap;
+ OPJ_UINT16 nr_entries;
+ OPJ_BYTE nr_channels;
+} opj_jp2_pclr_t;
+
+/**
+Collector for ICC profile, palette, component mapping, channel description
+*/
+typedef struct opj_jp2_color
+{
+ OPJ_BYTE *icc_profile_buf;
+ OPJ_UINT32 icc_profile_len;
+
+ opj_jp2_cdef_t *jp2_cdef;
+ opj_jp2_pclr_t *jp2_pclr;
+ OPJ_BYTE jp2_has_colr;
+} opj_jp2_color_t;
+
+/**
+JP2 component
+*/
+typedef struct opj_jp2_comps {
+ OPJ_UINT32 depth;
+ OPJ_UINT32 sgnd;
+ OPJ_UINT32 bpcc;
+} opj_jp2_comps_t;
+
+/**
+JPEG-2000 file format reader/writer
+*/
+typedef struct opj_jp2
+{
+ /** handle to the J2K codec */
+ opj_j2k_t *j2k;
+ /** list of validation procedures */
+ struct opj_procedure_list * m_validation_list;
+ /** list of execution procedures */
+ struct opj_procedure_list * m_procedure_list;
+
+ /* width of image */
+ OPJ_UINT32 w;
+ /* height of image */
+ OPJ_UINT32 h;
+ /* number of components in the image */
+ OPJ_UINT32 numcomps;
+ OPJ_UINT32 bpc;
+ OPJ_UINT32 C;
+ OPJ_UINT32 UnkC;
+ OPJ_UINT32 IPR;
+ OPJ_UINT32 meth;
+ OPJ_UINT32 approx;
+ OPJ_UINT32 enumcs;
+ OPJ_UINT32 precedence;
+ OPJ_UINT32 brand;
+ OPJ_UINT32 minversion;
+ OPJ_UINT32 numcl;
+ OPJ_UINT32 *cl;
+ opj_jp2_comps_t *comps;
+ /* FIXME: The following two variables are used to save offset
+ as we write out a JP2 file to disk. This mecanism is not flexible
+ as codec writers will need to extand those fields as new part
+ of the standard are implemented.
+ */
+ OPJ_OFF_T j2k_codestream_offset;
+ OPJ_OFF_T jpip_iptr_offset;
+ OPJ_BOOL jpip_on;
+ OPJ_UINT32 jp2_state;
+ OPJ_UINT32 jp2_img_state;
+
+ opj_jp2_color_t color;
+
+ OPJ_BOOL ignore_pclr_cmap_cdef;
+}
+opj_jp2_t;
+
+/**
+JP2 Box
+*/
+typedef struct opj_jp2_box {
+ OPJ_UINT32 length;
+ OPJ_UINT32 type;
+ OPJ_INT32 init_pos;
+} opj_jp2_box_t;
+
+typedef struct opj_jp2_header_handler
+{
+ /* marker value */
+ OPJ_UINT32 id;
+ /* action linked to the marker */
+ OPJ_BOOL (*handler) ( opj_jp2_t *jp2,
+ OPJ_BYTE *p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+}
+opj_jp2_header_handler_t;
+
+
+typedef struct opj_jp2_img_header_writer_handler
+{
+ /* action to perform */
+ OPJ_BYTE* (*handler) (opj_jp2_t *jp2, OPJ_UINT32 * p_data_size);
+ /* result of the action : data */
+ OPJ_BYTE* m_data;
+ /* size of data */
+ OPJ_UINT32 m_size;
+}
+opj_jp2_img_header_writer_handler_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
+ *
+ * @param jp2 the jpeg2000 file codec.
+ * @param stream the stream to write data to.
+ * @param p_manager user event manager.
+ *
+ * @return true if writing was successful.
+*/
+OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+Setup the decoder decoding parameters using user parameters.
+Decoding parameters are returned in jp2->j2k->cp.
+@param jp2 JP2 decompressor handle
+@param parameters decompression parameters
+*/
+void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters);
+
+/**
+ * Decode an image from a JPEG-2000 file stream
+ * @param jp2 JP2 decompressor handle
+ * @param p_stream FIXME DOC
+ * @param p_image FIXME DOC
+ * @param p_manager FIXME DOC
+ *
+ * @return Returns a decoded image if successful, returns NULL otherwise
+*/
+OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Setup the encoder parameters using the current image and using user parameters.
+ * Coding parameters are returned in jp2->j2k->cp.
+ *
+ * @param jp2 JP2 compressor handle
+ * @param parameters compression parameters
+ * @param image input filled image
+ * @param p_manager FIXME DOC
+*/
+void opj_jp2_setup_encoder( opj_jp2_t *jp2,
+ opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t * p_manager);
+
+/**
+Encode an image into a JPEG-2000 file stream
+@param jp2 JP2 compressor handle
+@param stream Output buffer stream
+@param p_manager event manager
+@return Returns true if successful, returns false otherwise
+*/
+OPJ_BOOL opj_jp2_encode( opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_event_mgr_t * p_manager);
+
+
+/**
+ * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
+ *
+ * @param jp2 the jpeg2000 file codec.
+ * @param stream the stream object.
+ * @param p_image FIXME DOC
+ * @param p_manager FIXME DOC
+ *
+ * @return true if the codec is valid.
+ */
+OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
+ opj_stream_private_t *stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager);
+
+
+/**
+ * Ends the compression procedures and possibiliy add data to be read after the
+ * codestream.
+ */
+OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager);
+
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Ends the decompression procedures and possibiliy add data to be read after the
+ * codestream.
+ */
+OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Reads a jpeg2000 file header structure.
+ *
+ * @param p_stream the stream to read data from.
+ * @param jp2 the jpeg2000 file header structure.
+ * @param p_image FIXME DOC
+ * @param p_manager the user event manager.
+ *
+ * @return true if the box is valid.
+ */
+OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
+ opj_jp2_t *jp2,
+ opj_image_t ** p_image,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Reads a tile header.
+ * @param p_jp2 the jpeg2000 codec.
+ * @param p_tile_index FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_tile_x0 FIXME DOC
+ * @param p_tile_y0 FIXME DOC
+ * @param p_tile_x1 FIXME DOC
+ * @param p_tile_y1 FIXME DOC
+ * @param p_nb_comps FIXME DOC
+ * @param p_go_on FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0,
+ OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1,
+ OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_go_on,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Writes a tile.
+ *
+ * @param p_jp2 the jpeg2000 codec.
+ * @param p_tile_index FIXME DOC
+ * @param p_data FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Decode tile data.
+ * @param p_jp2 the jpeg2000 codec.
+ * @param p_tile_index FIXME DOC
+ * @param p_data FIXME DOC
+ * @param p_data_size FIXME DOC
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ *
+ * @return FIXME DOC
+ */
+OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager );
+
+/**
+ * Creates a jpeg2000 file decompressor.
+ *
+ * @return an empty jpeg2000 file codec.
+ */
+opj_jp2_t* opj_jp2_create (OPJ_BOOL p_is_decoder);
+
+/**
+Destroy a JP2 decompressor handle
+@param jp2 JP2 decompressor handle to destroy
+*/
+void opj_jp2_destroy(opj_jp2_t *jp2);
+
+
+/**
+ * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
+ *
+ * @param p_jp2 the jpeg2000 codec.
+ * @param p_image FIXME DOC
+ * @param p_start_x the left position of the rectangle to decode (in image coordinates).
+ * @param p_start_y the up position of the rectangle to decode (in image coordinates).
+ * @param p_end_x the right position of the rectangle to decode (in image coordinates).
+ * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
+ * @param p_manager the user event manager
+ *
+ * @return true if the area could be set.
+ */
+OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
+ opj_event_mgr_t * p_manager );
+
+ /**
+ *
+ */
+OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
+ opj_stream_private_t *p_stream,
+ opj_image_t* p_image,
+ opj_event_mgr_t * p_manager,
+ OPJ_UINT32 tile_index );
+
+
+/**
+ *
+ */
+OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager);
+
+
+/* TODO MSD: clean these 3 functions */
+/**
+ * Dump some elements from the JP2 decompression structure .
+ *
+ *@param p_jp2 the jp2 codec.
+ *@param flag flag to describe what elments are dump.
+ *@param out_stream output stream where dump the elements.
+ *
+*/
+void jp2_dump (opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
+
+/**
+ * Get the codestream info from a JPEG2000 codec.
+ *
+ *@param p_jp2 jp2 codec.
+ *
+ *@return the codestream information extract from the jpg2000 codec
+ */
+opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2);
+
+/**
+ * Get the codestream index from a JPEG2000 codec.
+ *
+ *@param p_jp2 jp2 codec.
+ *
+ *@return the codestream index extract from the jpg2000 codec
+ */
+opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2);
+
+
+/*@}*/
+
+/*@}*/
+
+#endif /* __JP2_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.c
new file mode 100644
index 0000000000..0a64938b5c
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.c
@@ -0,0 +1,342 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef __SSE__
+#include <xmmintrin.h>
+#endif
+
+#include "opj_includes.h"
+
+/* <summary> */
+/* This table contains the norms of the basis function of the reversible MCT. */
+/* </summary> */
+static const OPJ_FLOAT64 opj_mct_norms[3] = { 1.732, .8292, .8292 };
+
+/* <summary> */
+/* This table contains the norms of the basis function of the irreversible MCT. */
+/* </summary> */
+static const OPJ_FLOAT64 opj_mct_norms_real[3] = { 1.732, 1.805, 1.573 };
+
+const OPJ_FLOAT64 * opj_mct_get_mct_norms ()
+{
+ return opj_mct_norms;
+}
+
+const OPJ_FLOAT64 * opj_mct_get_mct_norms_real ()
+{
+ return opj_mct_norms_real;
+}
+
+/* <summary> */
+/* Foward reversible MCT. */
+/* </summary> */
+void opj_mct_encode(
+ OPJ_INT32* restrict c0,
+ OPJ_INT32* restrict c1,
+ OPJ_INT32* restrict c2,
+ OPJ_UINT32 n)
+{
+ OPJ_UINT32 i;
+ for(i = 0; i < n; ++i) {
+ OPJ_INT32 r = c0[i];
+ OPJ_INT32 g = c1[i];
+ OPJ_INT32 b = c2[i];
+ OPJ_INT32 y = (r + (g * 2) + b) >> 2;
+ OPJ_INT32 u = b - g;
+ OPJ_INT32 v = r - g;
+ c0[i] = y;
+ c1[i] = u;
+ c2[i] = v;
+ }
+}
+
+/* <summary> */
+/* Inverse reversible MCT. */
+/* </summary> */
+void opj_mct_decode(
+ OPJ_INT32* restrict c0,
+ OPJ_INT32* restrict c1,
+ OPJ_INT32* restrict c2,
+ OPJ_UINT32 n)
+{
+ OPJ_UINT32 i;
+ for (i = 0; i < n; ++i) {
+ OPJ_INT32 y = c0[i];
+ OPJ_INT32 u = c1[i];
+ OPJ_INT32 v = c2[i];
+ OPJ_INT32 g = y - ((u + v) >> 2);
+ OPJ_INT32 r = v + g;
+ OPJ_INT32 b = u + g;
+ c0[i] = r;
+ c1[i] = g;
+ c2[i] = b;
+ }
+}
+
+/* <summary> */
+/* Get norm of basis function of reversible MCT. */
+/* </summary> */
+OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno) {
+ return opj_mct_norms[compno];
+}
+
+/* <summary> */
+/* Foward irreversible MCT. */
+/* </summary> */
+void opj_mct_encode_real(
+ OPJ_INT32* restrict c0,
+ OPJ_INT32* restrict c1,
+ OPJ_INT32* restrict c2,
+ OPJ_UINT32 n)
+{
+ OPJ_UINT32 i;
+ for(i = 0; i < n; ++i) {
+ OPJ_INT32 r = c0[i];
+ OPJ_INT32 g = c1[i];
+ OPJ_INT32 b = c2[i];
+ OPJ_INT32 y = opj_int_fix_mul(r, 2449) + opj_int_fix_mul(g, 4809) + opj_int_fix_mul(b, 934);
+ OPJ_INT32 u = -opj_int_fix_mul(r, 1382) - opj_int_fix_mul(g, 2714) + opj_int_fix_mul(b, 4096);
+ OPJ_INT32 v = opj_int_fix_mul(r, 4096) - opj_int_fix_mul(g, 3430) - opj_int_fix_mul(b, 666);
+ c0[i] = y;
+ c1[i] = u;
+ c2[i] = v;
+ }
+}
+
+/* <summary> */
+/* Inverse irreversible MCT. */
+/* </summary> */
+void opj_mct_decode_real(
+ OPJ_FLOAT32* restrict c0,
+ OPJ_FLOAT32* restrict c1,
+ OPJ_FLOAT32* restrict c2,
+ OPJ_UINT32 n)
+{
+ OPJ_UINT32 i;
+#ifdef __SSE__
+ // Mantis BUGID: 0056291. The address must be 16-byte aligned.
+ // TestFile: fuzz-signal_sigsegv_6e9e7f_5076_5265.pdf
+ if ((OPJ_UINT32)c0 % 16 == 0 && (OPJ_UINT32)c1 % 16 == 0 && (OPJ_UINT32)c2 % 16 == 0){
+ __m128 vrv, vgu, vgv, vbu;
+ vrv = _mm_set1_ps(1.402f);
+ vgu = _mm_set1_ps(0.34413f);
+ vgv = _mm_set1_ps(0.71414f);
+ vbu = _mm_set1_ps(1.772f);
+ for (i = 0; i < (n >> 3); ++i) {
+ __m128 vy, vu, vv;
+ __m128 vr, vg, vb;
+
+ vy = _mm_load_ps(c0);
+ vu = _mm_load_ps(c1);
+ vv = _mm_load_ps(c2);
+ vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
+ vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
+ vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
+ _mm_store_ps(c0, vr);
+ _mm_store_ps(c1, vg);
+ _mm_store_ps(c2, vb);
+ c0 += 4;
+ c1 += 4;
+ c2 += 4;
+
+ vy = _mm_load_ps(c0);
+ vu = _mm_load_ps(c1);
+ vv = _mm_load_ps(c2);
+ vr = _mm_add_ps(vy, _mm_mul_ps(vv, vrv));
+ vg = _mm_sub_ps(_mm_sub_ps(vy, _mm_mul_ps(vu, vgu)), _mm_mul_ps(vv, vgv));
+ vb = _mm_add_ps(vy, _mm_mul_ps(vu, vbu));
+ _mm_store_ps(c0, vr);
+ _mm_store_ps(c1, vg);
+ _mm_store_ps(c2, vb);
+ c0 += 4;
+ c1 += 4;
+ c2 += 4;
+ }
+ n &= 7;
+ } else {
+ for(i = 0; i < n; ++i) {
+ OPJ_FLOAT32 y = c0[i];
+ OPJ_FLOAT32 u = c1[i];
+ OPJ_FLOAT32 v = c2[i];
+ OPJ_FLOAT32 r = y + (v * 1.402f);
+ OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
+ OPJ_FLOAT32 b = y + (u * 1.772f);
+ c0[i] = r;
+ c1[i] = g;
+ c2[i] = b;
+ }
+ }
+
+#endif
+ for(i = 0; i < n; ++i) {
+ OPJ_FLOAT32 y = c0[i];
+ OPJ_FLOAT32 u = c1[i];
+ OPJ_FLOAT32 v = c2[i];
+ OPJ_FLOAT32 r = y + (v * 1.402f);
+ OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f));
+ OPJ_FLOAT32 b = y + (u * 1.772f);
+ c0[i] = r;
+ c1[i] = g;
+ c2[i] = b;
+ }
+}
+
+/* <summary> */
+/* Get norm of basis function of irreversible MCT. */
+/* </summary> */
+OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno) {
+ return opj_mct_norms_real[compno];
+}
+
+
+OPJ_BOOL opj_mct_encode_custom(
+ OPJ_BYTE * pCodingdata,
+ OPJ_UINT32 n,
+ OPJ_BYTE ** pData,
+ OPJ_UINT32 pNbComp,
+ OPJ_UINT32 isSigned)
+{
+ OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata;
+ OPJ_UINT32 i;
+ OPJ_UINT32 j;
+ OPJ_UINT32 k;
+ OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp;
+ OPJ_INT32 * lCurrentData = 00;
+ OPJ_INT32 * lCurrentMatrix = 00;
+ OPJ_INT32 ** lData = (OPJ_INT32 **) pData;
+ OPJ_UINT32 lMultiplicator = 1 << 13;
+ OPJ_INT32 * lMctPtr;
+
+ OPJ_ARG_NOT_USED(isSigned);
+
+ lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32));
+ if (! lCurrentData) {
+ return OPJ_FALSE;
+ }
+
+ lCurrentMatrix = lCurrentData + pNbComp;
+
+ for (i =0;i<lNbMatCoeff;++i) {
+ lCurrentMatrix[i] = (OPJ_INT32) (*(lMct++) * (OPJ_FLOAT32)lMultiplicator);
+ }
+
+ for (i = 0; i < n; ++i) {
+ lMctPtr = lCurrentMatrix;
+ for (j=0;j<pNbComp;++j) {
+ lCurrentData[j] = (*(lData[j]));
+ }
+
+ for (j=0;j<pNbComp;++j) {
+ *(lData[j]) = 0;
+ for (k=0;k<pNbComp;++k) {
+ *(lData[j]) += opj_int_fix_mul(*lMctPtr, lCurrentData[k]);
+ ++lMctPtr;
+ }
+
+ ++lData[j];
+ }
+ }
+
+ opj_free(lCurrentData);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_mct_decode_custom(
+ OPJ_BYTE * pDecodingData,
+ OPJ_UINT32 n,
+ OPJ_BYTE ** pData,
+ OPJ_UINT32 pNbComp,
+ OPJ_UINT32 isSigned)
+{
+ OPJ_FLOAT32 * lMct;
+ OPJ_UINT32 i;
+ OPJ_UINT32 j;
+ OPJ_UINT32 k;
+
+ OPJ_FLOAT32 * lCurrentData = 00;
+ OPJ_FLOAT32 * lCurrentResult = 00;
+ OPJ_FLOAT32 ** lData = (OPJ_FLOAT32 **) pData;
+
+ OPJ_ARG_NOT_USED(isSigned);
+
+ lCurrentData = (OPJ_FLOAT32 *) opj_malloc (2 * pNbComp * sizeof(OPJ_FLOAT32));
+ if (! lCurrentData) {
+ return OPJ_FALSE;
+ }
+ lCurrentResult = lCurrentData + pNbComp;
+
+ for (i = 0; i < n; ++i) {
+ lMct = (OPJ_FLOAT32 *) pDecodingData;
+ for (j=0;j<pNbComp;++j) {
+ lCurrentData[j] = (OPJ_FLOAT32) (*(lData[j]));
+ }
+ for (j=0;j<pNbComp;++j) {
+ lCurrentResult[j] = 0;
+ for (k=0;k<pNbComp;++k) {
+ lCurrentResult[j] += *(lMct++) * lCurrentData[k];
+ }
+ *(lData[j]++) = (OPJ_FLOAT32) (lCurrentResult[j]);
+ }
+ }
+ opj_free(lCurrentData);
+ return OPJ_TRUE;
+}
+
+void opj_calculate_norms( OPJ_FLOAT64 * pNorms,
+ OPJ_UINT32 pNbComps,
+ OPJ_FLOAT32 * pMatrix)
+{
+ OPJ_UINT32 i,j,lIndex;
+ OPJ_FLOAT32 lCurrentValue;
+ OPJ_FLOAT64 * lNorms = (OPJ_FLOAT64 *) pNorms;
+ OPJ_FLOAT32 * lMatrix = (OPJ_FLOAT32 *) pMatrix;
+
+ for (i=0;i<pNbComps;++i) {
+ lNorms[i] = 0;
+ lIndex = i;
+
+ for (j=0;j<pNbComps;++j) {
+ lCurrentValue = lMatrix[lIndex];
+ lIndex += pNbComps;
+ lNorms[i] += lCurrentValue * lCurrentValue;
+ }
+ lNorms[i] = sqrt(lNorms[i]);
+ }
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.h
new file mode 100644
index 0000000000..1c1f4d0c4d
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mct.h
@@ -0,0 +1,155 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MCT_H
+#define __MCT_H
+/**
+@file mct.h
+@brief Implementation of a multi-component transforms (MCT)
+
+The functions in MCT.C have for goal to realize reversible and irreversible multicomponent
+transform. The functions in MCT.C are used by some function in TCD.C.
+*/
+
+/** @defgroup MCT MCT - Implementation of a multi-component transform */
+/*@{*/
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Apply a reversible multi-component transform to an image
+@param c0 Samples for red component
+@param c1 Samples for green component
+@param c2 Samples blue component
+@param n Number of samples for each component
+*/
+void opj_mct_encode(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n);
+/**
+Apply a reversible multi-component inverse transform to an image
+@param c0 Samples for luminance component
+@param c1 Samples for red chrominance component
+@param c2 Samples for blue chrominance component
+@param n Number of samples for each component
+*/
+void opj_mct_decode(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n);
+/**
+Get norm of the basis function used for the reversible multi-component transform
+@param compno Number of the component (0->Y, 1->U, 2->V)
+@return
+*/
+OPJ_FLOAT64 opj_mct_getnorm(OPJ_UINT32 compno);
+
+/**
+Apply an irreversible multi-component transform to an image
+@param c0 Samples for red component
+@param c1 Samples for green component
+@param c2 Samples blue component
+@param n Number of samples for each component
+*/
+void opj_mct_encode_real(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n);
+/**
+Apply an irreversible multi-component inverse transform to an image
+@param c0 Samples for luminance component
+@param c1 Samples for red chrominance component
+@param c2 Samples for blue chrominance component
+@param n Number of samples for each component
+*/
+void opj_mct_decode_real(OPJ_FLOAT32* c0, OPJ_FLOAT32* c1, OPJ_FLOAT32* c2, OPJ_UINT32 n);
+/**
+Get norm of the basis function used for the irreversible multi-component transform
+@param compno Number of the component (0->Y, 1->U, 2->V)
+@return
+*/
+OPJ_FLOAT64 opj_mct_getnorm_real(OPJ_UINT32 compno);
+
+/**
+FIXME DOC
+@param p_coding_data MCT data
+@param n size of components
+@param p_data components
+@param p_nb_comp nb of components (i.e. size of p_data)
+@param is_signed tells if the data is signed
+@return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise
+*/
+OPJ_BOOL opj_mct_encode_custom(
+ OPJ_BYTE * p_coding_data,
+ OPJ_UINT32 n,
+ OPJ_BYTE ** p_data,
+ OPJ_UINT32 p_nb_comp,
+ OPJ_UINT32 is_signed);
+/**
+FIXME DOC
+@param pDecodingData MCT data
+@param n size of components
+@param pData components
+@param pNbComp nb of components (i.e. size of p_data)
+@param isSigned tells if the data is signed
+@return OPJ_FALSE if function encounter a problem, OPJ_TRUE otherwise
+*/
+OPJ_BOOL opj_mct_decode_custom(
+ OPJ_BYTE * pDecodingData,
+ OPJ_UINT32 n,
+ OPJ_BYTE ** pData,
+ OPJ_UINT32 pNbComp,
+ OPJ_UINT32 isSigned);
+/**
+FIXME DOC
+@param pNorms MCT data
+@param p_nb_comps size of components
+@param pMatrix components
+@return
+*/
+void opj_calculate_norms( OPJ_FLOAT64 * pNorms,
+ OPJ_UINT32 p_nb_comps,
+ OPJ_FLOAT32 * pMatrix);
+/**
+FIXME DOC
+*/
+const OPJ_FLOAT64 * opj_mct_get_mct_norms (void);
+/**
+FIXME DOC
+*/
+const OPJ_FLOAT64 * opj_mct_get_mct_norms_real (void);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __MCT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.c
new file mode 100644
index 0000000000..18fcc47605
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.c
@@ -0,0 +1,610 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup MQC MQC - Implementation of an MQ-Coder */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Output a byte, doing bit-stuffing if necessary.
+After a 0xff byte, the next byte must be smaller than 0x90.
+@param mqc MQC handle
+*/
+static void opj_mqc_byteout(opj_mqc_t *mqc);
+/**
+Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
+@param mqc MQC handle
+*/
+static void opj_mqc_renorme(opj_mqc_t *mqc);
+/**
+Encode the most probable symbol
+@param mqc MQC handle
+*/
+static void opj_mqc_codemps(opj_mqc_t *mqc);
+/**
+Encode the most least symbol
+@param mqc MQC handle
+*/
+static void opj_mqc_codelps(opj_mqc_t *mqc);
+/**
+Fill mqc->c with 1's for flushing
+@param mqc MQC handle
+*/
+static void opj_mqc_setbits(opj_mqc_t *mqc);
+/**
+FIXME DOC
+@param mqc MQC handle
+@return
+*/
+static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc);
+/**
+FIXME DOC
+@param mqc MQC handle
+@return
+*/
+static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc);
+/**
+Input a byte
+@param mqc MQC handle
+*/
+static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc);
+/**
+Renormalize mqc->a and mqc->c while decoding
+@param mqc MQC handle
+*/
+static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc);
+/*@}*/
+
+/*@}*/
+
+/* <summary> */
+/* This array defines all the possible states for a context. */
+/* </summary> */
+static opj_mqc_state_t mqc_states[47 * 2] = {
+ {0x5601, 0, &mqc_states[2], &mqc_states[3]},
+ {0x5601, 1, &mqc_states[3], &mqc_states[2]},
+ {0x3401, 0, &mqc_states[4], &mqc_states[12]},
+ {0x3401, 1, &mqc_states[5], &mqc_states[13]},
+ {0x1801, 0, &mqc_states[6], &mqc_states[18]},
+ {0x1801, 1, &mqc_states[7], &mqc_states[19]},
+ {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
+ {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
+ {0x0521, 0, &mqc_states[10], &mqc_states[58]},
+ {0x0521, 1, &mqc_states[11], &mqc_states[59]},
+ {0x0221, 0, &mqc_states[76], &mqc_states[66]},
+ {0x0221, 1, &mqc_states[77], &mqc_states[67]},
+ {0x5601, 0, &mqc_states[14], &mqc_states[13]},
+ {0x5601, 1, &mqc_states[15], &mqc_states[12]},
+ {0x5401, 0, &mqc_states[16], &mqc_states[28]},
+ {0x5401, 1, &mqc_states[17], &mqc_states[29]},
+ {0x4801, 0, &mqc_states[18], &mqc_states[28]},
+ {0x4801, 1, &mqc_states[19], &mqc_states[29]},
+ {0x3801, 0, &mqc_states[20], &mqc_states[28]},
+ {0x3801, 1, &mqc_states[21], &mqc_states[29]},
+ {0x3001, 0, &mqc_states[22], &mqc_states[34]},
+ {0x3001, 1, &mqc_states[23], &mqc_states[35]},
+ {0x2401, 0, &mqc_states[24], &mqc_states[36]},
+ {0x2401, 1, &mqc_states[25], &mqc_states[37]},
+ {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
+ {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
+ {0x1601, 0, &mqc_states[58], &mqc_states[42]},
+ {0x1601, 1, &mqc_states[59], &mqc_states[43]},
+ {0x5601, 0, &mqc_states[30], &mqc_states[29]},
+ {0x5601, 1, &mqc_states[31], &mqc_states[28]},
+ {0x5401, 0, &mqc_states[32], &mqc_states[28]},
+ {0x5401, 1, &mqc_states[33], &mqc_states[29]},
+ {0x5101, 0, &mqc_states[34], &mqc_states[30]},
+ {0x5101, 1, &mqc_states[35], &mqc_states[31]},
+ {0x4801, 0, &mqc_states[36], &mqc_states[32]},
+ {0x4801, 1, &mqc_states[37], &mqc_states[33]},
+ {0x3801, 0, &mqc_states[38], &mqc_states[34]},
+ {0x3801, 1, &mqc_states[39], &mqc_states[35]},
+ {0x3401, 0, &mqc_states[40], &mqc_states[36]},
+ {0x3401, 1, &mqc_states[41], &mqc_states[37]},
+ {0x3001, 0, &mqc_states[42], &mqc_states[38]},
+ {0x3001, 1, &mqc_states[43], &mqc_states[39]},
+ {0x2801, 0, &mqc_states[44], &mqc_states[38]},
+ {0x2801, 1, &mqc_states[45], &mqc_states[39]},
+ {0x2401, 0, &mqc_states[46], &mqc_states[40]},
+ {0x2401, 1, &mqc_states[47], &mqc_states[41]},
+ {0x2201, 0, &mqc_states[48], &mqc_states[42]},
+ {0x2201, 1, &mqc_states[49], &mqc_states[43]},
+ {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
+ {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
+ {0x1801, 0, &mqc_states[52], &mqc_states[46]},
+ {0x1801, 1, &mqc_states[53], &mqc_states[47]},
+ {0x1601, 0, &mqc_states[54], &mqc_states[48]},
+ {0x1601, 1, &mqc_states[55], &mqc_states[49]},
+ {0x1401, 0, &mqc_states[56], &mqc_states[50]},
+ {0x1401, 1, &mqc_states[57], &mqc_states[51]},
+ {0x1201, 0, &mqc_states[58], &mqc_states[52]},
+ {0x1201, 1, &mqc_states[59], &mqc_states[53]},
+ {0x1101, 0, &mqc_states[60], &mqc_states[54]},
+ {0x1101, 1, &mqc_states[61], &mqc_states[55]},
+ {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
+ {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
+ {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
+ {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
+ {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
+ {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
+ {0x0521, 0, &mqc_states[68], &mqc_states[62]},
+ {0x0521, 1, &mqc_states[69], &mqc_states[63]},
+ {0x0441, 0, &mqc_states[70], &mqc_states[64]},
+ {0x0441, 1, &mqc_states[71], &mqc_states[65]},
+ {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
+ {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
+ {0x0221, 0, &mqc_states[74], &mqc_states[68]},
+ {0x0221, 1, &mqc_states[75], &mqc_states[69]},
+ {0x0141, 0, &mqc_states[76], &mqc_states[70]},
+ {0x0141, 1, &mqc_states[77], &mqc_states[71]},
+ {0x0111, 0, &mqc_states[78], &mqc_states[72]},
+ {0x0111, 1, &mqc_states[79], &mqc_states[73]},
+ {0x0085, 0, &mqc_states[80], &mqc_states[74]},
+ {0x0085, 1, &mqc_states[81], &mqc_states[75]},
+ {0x0049, 0, &mqc_states[82], &mqc_states[76]},
+ {0x0049, 1, &mqc_states[83], &mqc_states[77]},
+ {0x0025, 0, &mqc_states[84], &mqc_states[78]},
+ {0x0025, 1, &mqc_states[85], &mqc_states[79]},
+ {0x0015, 0, &mqc_states[86], &mqc_states[80]},
+ {0x0015, 1, &mqc_states[87], &mqc_states[81]},
+ {0x0009, 0, &mqc_states[88], &mqc_states[82]},
+ {0x0009, 1, &mqc_states[89], &mqc_states[83]},
+ {0x0005, 0, &mqc_states[90], &mqc_states[84]},
+ {0x0005, 1, &mqc_states[91], &mqc_states[85]},
+ {0x0001, 0, &mqc_states[90], &mqc_states[86]},
+ {0x0001, 1, &mqc_states[91], &mqc_states[87]},
+ {0x5601, 0, &mqc_states[92], &mqc_states[92]},
+ {0x5601, 1, &mqc_states[93], &mqc_states[93]},
+};
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+void opj_mqc_byteout(opj_mqc_t *mqc) {
+ if (*mqc->bp == 0xff) {
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
+ mqc->c &= 0xfffff;
+ mqc->ct = 7;
+ } else {
+ if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
+ mqc->c &= 0x7ffff;
+ mqc->ct = 8;
+ } else {
+ (*mqc->bp)++;
+ if (*mqc->bp == 0xff) {
+ mqc->c &= 0x7ffffff;
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
+ mqc->c &= 0xfffff;
+ mqc->ct = 7;
+ } else {
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
+ mqc->c &= 0x7ffff;
+ mqc->ct = 8;
+ }
+ }
+ }
+}
+
+void opj_mqc_renorme(opj_mqc_t *mqc) {
+ do {
+ mqc->a <<= 1;
+ mqc->c <<= 1;
+ mqc->ct--;
+ if (mqc->ct == 0) {
+ opj_mqc_byteout(mqc);
+ }
+ } while ((mqc->a & 0x8000) == 0);
+}
+
+void opj_mqc_codemps(opj_mqc_t *mqc) {
+ mqc->a -= (*mqc->curctx)->qeval;
+ if ((mqc->a & 0x8000) == 0) {
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->a = (*mqc->curctx)->qeval;
+ } else {
+ mqc->c += (*mqc->curctx)->qeval;
+ }
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ opj_mqc_renorme(mqc);
+ } else {
+ mqc->c += (*mqc->curctx)->qeval;
+ }
+}
+
+void opj_mqc_codelps(opj_mqc_t *mqc) {
+ mqc->a -= (*mqc->curctx)->qeval;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->c += (*mqc->curctx)->qeval;
+ } else {
+ mqc->a = (*mqc->curctx)->qeval;
+ }
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ opj_mqc_renorme(mqc);
+}
+
+void opj_mqc_setbits(opj_mqc_t *mqc) {
+ OPJ_UINT32 tempc = mqc->c + mqc->a;
+ mqc->c |= 0xffff;
+ if (mqc->c >= tempc) {
+ mqc->c -= 0x8000;
+ }
+}
+
+static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc) {
+ OPJ_INT32 d;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ } else {
+ d = (OPJ_INT32)(*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ }
+
+ return d;
+}
+
+static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc) {
+ OPJ_INT32 d;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->a = (*mqc->curctx)->qeval;
+ d = (OPJ_INT32)(*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ } else {
+ mqc->a = (*mqc->curctx)->qeval;
+ d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ }
+
+ return d;
+}
+
+#ifdef MQC_PERF_OPT
+static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) {
+ unsigned int i = *((unsigned int *) mqc->bp);
+ mqc->c += i & 0xffff00;
+ mqc->ct = i & 0x0f;
+ mqc->bp += (i >> 2) & 0x04;
+}
+#else
+static void opj_mqc_bytein(opj_mqc_t *const mqc) {
+ if (mqc->bp != mqc->end) {
+ OPJ_UINT32 c;
+ if (mqc->bp + 1 != mqc->end) {
+ c = *(mqc->bp + 1);
+ } else {
+ c = 0xff;
+ }
+ if (*mqc->bp == 0xff) {
+ if (c > 0x8f) {
+ mqc->c += 0xff00;
+ mqc->ct = 8;
+ } else {
+ mqc->bp++;
+ mqc->c += c << 9;
+ mqc->ct = 7;
+ }
+ } else {
+ mqc->bp++;
+ mqc->c += c << 8;
+ mqc->ct = 8;
+ }
+ } else {
+ mqc->c += 0xff00;
+ mqc->ct = 8;
+ }
+}
+#endif
+
+static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) {
+ do {
+ if (mqc->ct == 0) {
+ opj_mqc_bytein(mqc);
+ }
+ mqc->a <<= 1;
+ mqc->c <<= 1;
+ mqc->ct--;
+ } while (mqc->a < 0x8000);
+}
+
+/*
+==========================================================
+ MQ-Coder interface
+==========================================================
+*/
+
+opj_mqc_t* opj_mqc_create(void) {
+ opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
+#ifdef MQC_PERF_OPT
+ mqc->buffer = NULL;
+#endif
+ return mqc;
+}
+
+void opj_mqc_destroy(opj_mqc_t *mqc) {
+ if(mqc) {
+#ifdef MQC_PERF_OPT
+ opj_free(mqc->buffer);
+#endif
+ opj_free(mqc);
+ }
+}
+
+OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc) {
+ const ptrdiff_t diff = mqc->bp - mqc->start;
+#if 0
+ assert( diff <= 0xffffffff && diff >= 0 ); /* UINT32_MAX */
+#endif
+ return (OPJ_UINT32)diff;
+}
+
+void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) {
+ /* TODO MSD: need to take a look to the v2 version */
+ opj_mqc_setcurctx(mqc, 0);
+ mqc->a = 0x8000;
+ mqc->c = 0;
+ mqc->bp = bp - 1;
+ mqc->ct = 12;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 13;
+ }
+ mqc->start = bp;
+}
+
+void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) {
+ if ((*mqc->curctx)->mps == d) {
+ opj_mqc_codemps(mqc);
+ } else {
+ opj_mqc_codelps(mqc);
+ }
+}
+
+void opj_mqc_flush(opj_mqc_t *mqc) {
+ opj_mqc_setbits(mqc);
+ mqc->c <<= mqc->ct;
+ opj_mqc_byteout(mqc);
+ mqc->c <<= mqc->ct;
+ opj_mqc_byteout(mqc);
+
+ if (*mqc->bp != 0xff) {
+ mqc->bp++;
+ }
+}
+
+void opj_mqc_bypass_init_enc(opj_mqc_t *mqc) {
+ mqc->c = 0;
+ mqc->ct = 8;
+ /*if (*mqc->bp == 0xff) {
+ mqc->ct = 7;
+ } */
+}
+
+void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) {
+ mqc->ct--;
+ mqc->c = mqc->c + (d << mqc->ct);
+ if (mqc->ct == 0) {
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)mqc->c;
+ mqc->ct = 8;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 7;
+ }
+ mqc->c = 0;
+ }
+}
+
+OPJ_UINT32 opj_mqc_bypass_flush_enc(opj_mqc_t *mqc) {
+ OPJ_BYTE bit_padding;
+
+ bit_padding = 0;
+
+ if (mqc->ct != 0) {
+ while (mqc->ct > 0) {
+ mqc->ct--;
+ mqc->c += (OPJ_UINT32)(bit_padding << mqc->ct);
+ bit_padding = (bit_padding + 1) & 0x01;
+ }
+ mqc->bp++;
+ *mqc->bp = (OPJ_BYTE)mqc->c;
+ mqc->ct = 8;
+ mqc->c = 0;
+ }
+
+ return 1;
+}
+
+void opj_mqc_reset_enc(opj_mqc_t *mqc) {
+ opj_mqc_resetstates(mqc);
+ opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
+ opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
+ opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
+}
+
+OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) {
+ OPJ_UINT32 correction = 1;
+
+ /* <flush part> */
+ OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
+ mqc->c <<= mqc->ct;
+ while (n > 0) {
+ opj_mqc_byteout(mqc);
+ n -= (OPJ_INT32)mqc->ct;
+ mqc->c <<= mqc->ct;
+ }
+ opj_mqc_byteout(mqc);
+
+ return correction;
+}
+
+void opj_mqc_restart_init_enc(opj_mqc_t *mqc) {
+ /* <Re-init part> */
+ opj_mqc_setcurctx(mqc, 0);
+ mqc->a = 0x8000;
+ mqc->c = 0;
+ mqc->ct = 12;
+ mqc->bp--;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 13;
+ }
+}
+
+void opj_mqc_erterm_enc(opj_mqc_t *mqc) {
+ OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
+
+ while (k > 0) {
+ mqc->c <<= mqc->ct;
+ mqc->ct = 0;
+ opj_mqc_byteout(mqc);
+ k -= (OPJ_INT32)mqc->ct;
+ }
+
+ if (*mqc->bp != 0xff) {
+ opj_mqc_byteout(mqc);
+ }
+}
+
+void opj_mqc_segmark_enc(opj_mqc_t *mqc) {
+ OPJ_UINT32 i;
+ opj_mqc_setcurctx(mqc, 18);
+
+ for (i = 1; i < 5; i++) {
+ opj_mqc_encode(mqc, i % 2);
+ }
+}
+
+OPJ_BOOL opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) {
+ opj_mqc_setcurctx(mqc, 0);
+ mqc->start = bp;
+ mqc->end = bp + len;
+ mqc->bp = bp;
+ if (len==0) mqc->c = 0xff << 16;
+ else mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
+
+#ifdef MQC_PERF_OPT /* TODO_MSD: check this option and put in experimental */
+ {
+ OPJ_UINT32 c;
+ OPJ_UINT32 *ip;
+ OPJ_BYTE *end = mqc->end - 1;
+ void* new_buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(OPJ_UINT32));
+ if (! new_buffer) {
+ opj_free(mqc->buffer);
+ mqc->buffer = NULL;
+ return OPJ_FALSE;
+ }
+ mqc->buffer = new_buffer;
+
+ ip = (OPJ_UINT32 *) mqc->buffer;
+
+ while (bp < end) {
+ c = *(bp + 1);
+ if (*bp == 0xff) {
+ if (c > 0x8f) {
+ break;
+ } else {
+ *ip = 0x00000017 | (c << 9);
+ }
+ } else {
+ *ip = 0x00000018 | (c << 8);
+ }
+ bp++;
+ ip++;
+ }
+
+ /* Handle last byte of data */
+ c = 0xff;
+ if (*bp == 0xff) {
+ *ip = 0x0000ff18;
+ } else {
+ bp++;
+ *ip = 0x00000018 | (c << 8);
+ }
+ ip++;
+
+ *ip = 0x0000ff08;
+ mqc->bp = mqc->buffer;
+ }
+#endif
+ opj_mqc_bytein(mqc);
+ mqc->c <<= 7;
+ mqc->ct -= 7;
+ mqc->a = 0x8000;
+ return OPJ_TRUE;
+}
+
+OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc) {
+ OPJ_INT32 d;
+ mqc->a -= (*mqc->curctx)->qeval;
+ if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
+ d = opj_mqc_lpsexchange(mqc);
+ opj_mqc_renormd(mqc);
+ } else {
+ mqc->c -= (*mqc->curctx)->qeval << 16;
+ if ((mqc->a & 0x8000) == 0) {
+ d = opj_mqc_mpsexchange(mqc);
+ opj_mqc_renormd(mqc);
+ } else {
+ d = (OPJ_INT32)(*mqc->curctx)->mps;
+ }
+ }
+
+ return d;
+}
+
+void opj_mqc_resetstates(opj_mqc_t *mqc) {
+ OPJ_UINT32 i;
+ for (i = 0; i < MQC_NUMCTXS; i++) {
+ mqc->ctxs[i] = mqc_states;
+ }
+}
+
+void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) {
+ mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
+}
+
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.h
new file mode 100644
index 0000000000..69a2d46029
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/mqc.h
@@ -0,0 +1,207 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MQC_H
+#define __MQC_H
+/**
+@file mqc.h
+@brief Implementation of an MQ-Coder (MQC)
+
+The functions in MQC.C have for goal to realize the MQ-coder operations. The functions
+in MQC.C are used by some function in T1.C.
+*/
+
+/** @defgroup MQC MQC - Implementation of an MQ-Coder */
+/*@{*/
+
+/**
+This struct defines the state of a context.
+*/
+typedef struct opj_mqc_state {
+ /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
+ OPJ_UINT32 qeval;
+ /** the Most Probable Symbol (0 or 1) */
+ OPJ_UINT32 mps;
+ /** next state if the next encoded symbol is the MPS */
+ struct opj_mqc_state *nmps;
+ /** next state if the next encoded symbol is the LPS */
+ struct opj_mqc_state *nlps;
+} opj_mqc_state_t;
+
+#define MQC_NUMCTXS 19
+
+/**
+MQ coder
+*/
+typedef struct opj_mqc {
+ OPJ_UINT32 c;
+ OPJ_UINT32 a;
+ OPJ_UINT32 ct;
+ OPJ_BYTE *bp;
+ OPJ_BYTE *start;
+ OPJ_BYTE *end;
+ opj_mqc_state_t *ctxs[MQC_NUMCTXS];
+ opj_mqc_state_t **curctx;
+#ifdef MQC_PERF_OPT
+ unsigned char *buffer;
+#endif
+} opj_mqc_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new MQC handle
+@return Returns a new MQC handle if successful, returns NULL otherwise
+*/
+opj_mqc_t* opj_mqc_create(void);
+/**
+Destroy a previously created MQC handle
+@param mqc MQC handle to destroy
+*/
+void opj_mqc_destroy(opj_mqc_t *mqc);
+/**
+Return the number of bytes written/read since initialisation
+@param mqc MQC handle
+@return Returns the number of bytes already encoded
+*/
+OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc);
+/**
+Reset the states of all the context of the coder/decoder
+(each context is set to a state where 0 and 1 are more or less equiprobable)
+@param mqc MQC handle
+*/
+void opj_mqc_resetstates(opj_mqc_t *mqc);
+/**
+Set the state of a particular context
+@param mqc MQC handle
+@param ctxno Number that identifies the context
+@param msb The MSB of the new state of the context
+@param prob Number that identifies the probability of the symbols for the new state of the context
+*/
+void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob);
+/**
+Initialize the encoder
+@param mqc MQC handle
+@param bp Pointer to the start of the buffer where the bytes will be written
+*/
+void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp);
+/**
+Set the current context used for coding/decoding
+@param mqc MQC handle
+@param ctxno Number that identifies the context
+*/
+#define opj_mqc_setcurctx(mqc, ctxno) (mqc)->curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
+/**
+Encode a symbol using the MQ-coder
+@param mqc MQC handle
+@param d The symbol to be encoded (0 or 1)
+*/
+void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d);
+/**
+Flush the encoder, so that all remaining data is written
+@param mqc MQC handle
+*/
+void opj_mqc_flush(opj_mqc_t *mqc);
+/**
+BYPASS mode switch, initialization operation.
+JPEG 2000 p 505.
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+*/
+void opj_mqc_bypass_init_enc(opj_mqc_t *mqc);
+/**
+BYPASS mode switch, coding operation.
+JPEG 2000 p 505.
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+@param d The symbol to be encoded (0 or 1)
+*/
+void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d);
+/**
+BYPASS mode switch, flush operation
+<h2>Not fully implemented and tested !!</h2>
+@param mqc MQC handle
+@return Returns 1 (always)
+*/
+OPJ_UINT32 opj_mqc_bypass_flush_enc(opj_mqc_t *mqc);
+/**
+RESET mode switch
+@param mqc MQC handle
+*/
+void opj_mqc_reset_enc(opj_mqc_t *mqc);
+/**
+RESTART mode switch (TERMALL)
+@param mqc MQC handle
+@return Returns 1 (always)
+*/
+OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc);
+/**
+RESTART mode switch (TERMALL) reinitialisation
+@param mqc MQC handle
+*/
+void opj_mqc_restart_init_enc(opj_mqc_t *mqc);
+/**
+ERTERM mode switch (PTERM)
+@param mqc MQC handle
+*/
+void opj_mqc_erterm_enc(opj_mqc_t *mqc);
+/**
+SEGMARK mode switch (SEGSYM)
+@param mqc MQC handle
+*/
+void opj_mqc_segmark_enc(opj_mqc_t *mqc);
+/**
+Initialize the decoder
+@param mqc MQC handle
+@param bp Pointer to the start of the buffer from which the bytes will be read
+@param len Length of the input buffer
+*/
+OPJ_BOOL opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len);
+/**
+Decode a symbol
+@param mqc MQC handle
+@return Returns the decoded symbol (0 or 1)
+*/
+OPJ_INT32 opj_mqc_decode(opj_mqc_t * const mqc);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __MQC_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.c
new file mode 100644
index 0000000000..89bb75a552
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.c
@@ -0,0 +1,960 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef _WIN32
+#include <windows.h>
+#endif /* _WIN32 */
+
+#include "opj_includes.h"
+
+
+/* ---------------------------------------------------------------------- */
+/* Functions to set the message handlers */
+
+OPJ_BOOL OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data)
+{
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ if(! l_codec){
+ return OPJ_FALSE;
+ }
+
+ l_codec->m_event_mgr.info_handler = p_callback;
+ l_codec->m_event_mgr.m_info_data = p_user_data;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler( opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data)
+{
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ if (! l_codec) {
+ return OPJ_FALSE;
+ }
+
+ l_codec->m_event_mgr.warning_handler = p_callback;
+ l_codec->m_event_mgr.m_warning_data = p_user_data;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data)
+{
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ if (! l_codec) {
+ return OPJ_FALSE;
+ }
+
+ l_codec->m_event_mgr.error_handler = p_callback;
+ l_codec->m_event_mgr.m_error_data = p_user_data;
+
+ return OPJ_TRUE;
+}
+
+/* ---------------------------------------------------------------------- */
+
+static OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
+{
+ OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
+ return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
+}
+
+static OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
+{
+ OPJ_OFF_T file_length = 0;
+
+ OPJ_FSEEK(p_file, 0, SEEK_END);
+ file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
+ OPJ_FSEEK(p_file, 0, SEEK_SET);
+
+ return (OPJ_UINT64)file_length;
+}
+
+static OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
+{
+ return fwrite(p_buffer,1,p_nb_bytes,p_file);
+}
+
+static OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
+{
+ if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
+ return -1;
+ }
+
+ return p_nb_bytes;
+}
+
+static OPJ_BOOL opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
+{
+ if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/* ---------------------------------------------------------------------- */
+#ifdef _WIN32
+#ifndef OPJ_STATIC
+BOOL APIENTRY
+DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
+
+ OPJ_ARG_NOT_USED(lpReserved);
+ OPJ_ARG_NOT_USED(hModule);
+
+ switch (ul_reason_for_call) {
+ case DLL_PROCESS_ATTACH :
+ break;
+ case DLL_PROCESS_DETACH :
+ break;
+ case DLL_THREAD_ATTACH :
+ case DLL_THREAD_DETACH :
+ break;
+ }
+
+ return TRUE;
+}
+#endif /* OPJ_STATIC */
+#endif /* _WIN32 */
+
+/* ---------------------------------------------------------------------- */
+
+const char* OPJ_CALLCONV opj_version(void) {
+ return OPJ_PACKAGE_VERSION;
+}
+
+/* ---------------------------------------------------------------------- */
+/* DECOMPRESSION FUNCTIONS*/
+
+opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
+{
+ opj_codec_private_t *l_codec = 00;
+
+ l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
+ if (!l_codec){
+ return 00;
+ }
+ memset(l_codec, 0, sizeof(opj_codec_private_t));
+
+ l_codec->is_decompressor = 1;
+
+ switch (p_format) {
+ case OPJ_CODEC_J2K:
+ l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
+
+ l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
+
+ l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
+
+ l_codec->m_codec_data.m_decompression.opj_decode =
+ (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
+
+ l_codec->m_codec_data.m_decompression.opj_end_decompress =
+ (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_j2k_end_decompress;
+
+ l_codec->m_codec_data.m_decompression.opj_read_header =
+ (OPJ_BOOL (*) ( struct opj_stream_private *,
+ void *,
+ opj_image_t **,
+ struct opj_event_mgr * )) opj_j2k_read_header;
+
+ l_codec->m_codec_data.m_decompression.opj_destroy =
+ (void (*) (void *))opj_j2k_destroy;
+
+ l_codec->m_codec_data.m_decompression.opj_setup_decoder =
+ (void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
+
+ l_codec->m_codec_data.m_decompression.opj_read_tile_header =
+ (OPJ_BOOL (*) ( void *,
+ OPJ_UINT32*,
+ OPJ_UINT32*,
+ OPJ_INT32*, OPJ_INT32*,
+ OPJ_INT32*, OPJ_INT32*,
+ OPJ_UINT32*,
+ OPJ_BOOL*,
+ struct opj_stream_private *,
+ struct opj_event_mgr * )) opj_j2k_read_tile_header;
+
+ l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
+ (OPJ_BOOL (*) ( void *,
+ OPJ_UINT32,
+ OPJ_BYTE*,
+ OPJ_UINT32,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_j2k_decode_tile;
+
+ l_codec->m_codec_data.m_decompression.opj_set_decode_area =
+ (OPJ_BOOL (*) ( void *,
+ opj_image_t*,
+ OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
+ struct opj_event_mgr *)) opj_j2k_set_decode_area;
+
+ l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
+ (OPJ_BOOL (*) ( void *p_codec,
+ opj_stream_private_t *p_cio,
+ opj_image_t *p_image,
+ struct opj_event_mgr * p_manager,
+ OPJ_UINT32 tile_index)) opj_j2k_get_tile;
+
+ l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
+ (OPJ_BOOL (*) ( void * p_codec,
+ OPJ_UINT32 res_factor,
+ struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
+
+ l_codec->m_codec = opj_j2k_create_decompress();
+
+ if (! l_codec->m_codec) {
+ opj_free(l_codec);
+ return NULL;
+ }
+
+ break;
+
+ case OPJ_CODEC_JP2:
+ /* get a JP2 decoder handle */
+ l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
+
+ l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
+
+ l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
+
+ l_codec->m_codec_data.m_decompression.opj_decode =
+ (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ opj_image_t*,
+ struct opj_event_mgr * )) opj_jp2_decode;
+
+ l_codec->m_codec_data.m_decompression.opj_end_decompress =
+ (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_jp2_end_decompress;
+
+ l_codec->m_codec_data.m_decompression.opj_read_header =
+ (OPJ_BOOL (*) ( struct opj_stream_private *,
+ void *,
+ opj_image_t **,
+ struct opj_event_mgr * )) opj_jp2_read_header;
+
+ l_codec->m_codec_data.m_decompression.opj_read_tile_header =
+ (OPJ_BOOL (*) ( void *,
+ OPJ_UINT32*,
+ OPJ_UINT32*,
+ OPJ_INT32*,
+ OPJ_INT32*,
+ OPJ_INT32 * ,
+ OPJ_INT32 * ,
+ OPJ_UINT32 * ,
+ OPJ_BOOL *,
+ struct opj_stream_private *,
+ struct opj_event_mgr * )) opj_jp2_read_tile_header;
+
+ l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
+ (OPJ_BOOL (*) ( void *,
+ OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
+ struct opj_stream_private *,
+ struct opj_event_mgr * )) opj_jp2_decode_tile;
+
+ l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
+
+ l_codec->m_codec_data.m_decompression.opj_setup_decoder =
+ (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
+
+ l_codec->m_codec_data.m_decompression.opj_set_decode_area =
+ (OPJ_BOOL (*) ( void *,
+ opj_image_t*,
+ OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
+ struct opj_event_mgr * )) opj_jp2_set_decode_area;
+
+ l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
+ (OPJ_BOOL (*) ( void *p_codec,
+ opj_stream_private_t *p_cio,
+ opj_image_t *p_image,
+ struct opj_event_mgr * p_manager,
+ OPJ_UINT32 tile_index)) opj_jp2_get_tile;
+
+ l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
+ (OPJ_BOOL (*) ( void * p_codec,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
+
+ l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
+
+ if (! l_codec->m_codec) {
+ opj_free(l_codec);
+ return 00;
+ }
+
+ break;
+ case OPJ_CODEC_UNKNOWN:
+ case OPJ_CODEC_JPT:
+ default:
+ opj_free(l_codec);
+ return 00;
+ }
+
+ opj_set_default_event_handler(&(l_codec->m_event_mgr));
+ return (opj_codec_t*) l_codec;
+}
+
+void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
+ if(parameters) {
+ memset(parameters, 0, sizeof(opj_dparameters_t));
+ /* default decoding parameters */
+ parameters->cp_layer = 0;
+ parameters->cp_reduce = 0;
+
+ parameters->decod_format = -1;
+ parameters->cod_format = -1;
+ parameters->flags = 0;
+/* UniPG>> */
+#ifdef USE_JPWL
+ parameters->jpwl_correct = OPJ_FALSE;
+ parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
+ parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
+#endif /* USE_JPWL */
+/* <<UniPG */
+ }
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
+ opj_dparameters_t *parameters
+ )
+{
+ if (p_codec && parameters) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+
+ if (! l_codec->is_decompressor) {
+ opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
+ "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
+ return OPJ_FALSE;
+ }
+
+ l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
+ parameters);
+ return OPJ_TRUE;
+ }
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
+ opj_codec_t *p_codec,
+ opj_image_t **p_image )
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
+ opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+
+ if(! l_codec->is_decompressor) {
+ opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
+ "Codec provided to the opj_read_header function is not a decompressor handler.\n");
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
+ l_codec->m_codec,
+ p_image,
+ &(l_codec->m_event_mgr) );
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_decode( opj_codec_t *p_codec,
+ opj_stream_t *p_stream,
+ opj_image_t* p_image)
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
+ l_stream,
+ p_image,
+ &(l_codec->m_event_mgr) );
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
+ )
+{
+ if (p_codec) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_set_decode_area( l_codec->m_codec,
+ p_image,
+ p_start_x, p_start_y,
+ p_end_x, p_end_y,
+ &(l_codec->m_event_mgr) );
+ }
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
+ opj_stream_t * p_stream,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_should_go_on)
+{
+ if (p_codec && p_stream && p_data_size && p_tile_index) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_read_tile_header( l_codec->m_codec,
+ p_tile_index,
+ p_data_size,
+ p_tile_x0, p_tile_y0,
+ p_tile_x1, p_tile_y1,
+ p_nb_comps,
+ p_should_go_on,
+ l_stream,
+ &(l_codec->m_event_mgr));
+ }
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_t *p_stream
+ )
+{
+ if (p_codec && p_data && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_decode_tile_data( l_codec->m_codec,
+ p_tile_index,
+ p_data,
+ p_data_size,
+ l_stream,
+ &(l_codec->m_event_mgr) );
+ }
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
+ opj_stream_t *p_stream,
+ opj_image_t *p_image,
+ OPJ_UINT32 tile_index)
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile( l_codec->m_codec,
+ l_stream,
+ p_image,
+ &(l_codec->m_event_mgr),
+ tile_index);
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
+ OPJ_UINT32 res_factor )
+{
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+
+ if ( !l_codec ){
+ fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
+ return OPJ_FALSE;
+ }
+
+ l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec,
+ res_factor,
+ &(l_codec->m_event_mgr) );
+ return OPJ_TRUE;
+}
+
+/* ---------------------------------------------------------------------- */
+/* COMPRESSION FUNCTIONS*/
+
+opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
+{
+ opj_codec_private_t *l_codec = 00;
+
+ l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
+ if (!l_codec) {
+ return 00;
+ }
+ memset(l_codec, 0, sizeof(opj_codec_private_t));
+
+ l_codec->is_decompressor = 0;
+
+ switch(p_format) {
+ case OPJ_CODEC_J2K:
+ l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr * )) opj_j2k_encode;
+
+ l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_j2k_end_compress;
+
+ l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
+ struct opj_stream_private *,
+ struct opj_image * ,
+ struct opj_event_mgr *)) opj_j2k_start_compress;
+
+ l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
+ OPJ_UINT32,
+ OPJ_BYTE*,
+ OPJ_UINT32,
+ struct opj_stream_private *,
+ struct opj_event_mgr *) ) opj_j2k_write_tile;
+
+ l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
+
+ l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
+ opj_cparameters_t *,
+ struct opj_image *,
+ struct opj_event_mgr * )) opj_j2k_setup_encoder;
+
+ l_codec->m_codec = opj_j2k_create_compress();
+ if (! l_codec->m_codec) {
+ opj_free(l_codec);
+ return 00;
+ }
+
+ break;
+
+ case OPJ_CODEC_JP2:
+ /* get a JP2 decoder handle */
+ l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr * )) opj_jp2_encode;
+
+ l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) ( void *,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_jp2_end_compress;
+
+ l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
+ struct opj_stream_private *,
+ struct opj_image * ,
+ struct opj_event_mgr *)) opj_jp2_start_compress;
+
+ l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
+ OPJ_UINT32,
+ OPJ_BYTE*,
+ OPJ_UINT32,
+ struct opj_stream_private *,
+ struct opj_event_mgr *)) opj_jp2_write_tile;
+
+ l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
+
+ l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
+ opj_cparameters_t *,
+ struct opj_image *,
+ struct opj_event_mgr * )) opj_jp2_setup_encoder;
+
+ l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
+ if (! l_codec->m_codec) {
+ opj_free(l_codec);
+ return 00;
+ }
+
+ break;
+
+ case OPJ_CODEC_UNKNOWN:
+ case OPJ_CODEC_JPT:
+ default:
+ opj_free(l_codec);
+ return 00;
+ }
+
+ opj_set_default_event_handler(&(l_codec->m_event_mgr));
+ return (opj_codec_t*) l_codec;
+}
+
+void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
+ if(parameters) {
+ memset(parameters, 0, sizeof(opj_cparameters_t));
+ /* default coding parameters */
+ parameters->cp_cinema = OPJ_OFF;
+ parameters->max_comp_size = 0;
+ parameters->numresolution = 6;
+ parameters->cp_rsiz = OPJ_STD_RSIZ;
+ parameters->cblockw_init = 64;
+ parameters->cblockh_init = 64;
+ parameters->prog_order = OPJ_LRCP;
+ parameters->roi_compno = -1; /* no ROI */
+ parameters->subsampling_dx = 1;
+ parameters->subsampling_dy = 1;
+ parameters->tp_on = 0;
+ parameters->decod_format = -1;
+ parameters->cod_format = -1;
+ parameters->tcp_rates[0] = 0;
+ parameters->tcp_numlayers = 0;
+ parameters->cp_disto_alloc = 0;
+ parameters->cp_fixed_alloc = 0;
+ parameters->cp_fixed_quality = 0;
+ parameters->jpip_on = OPJ_FALSE;
+/* UniPG>> */
+#ifdef USE_JPWL
+ parameters->jpwl_epc_on = OPJ_FALSE;
+ parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
+ {
+ int i;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
+ parameters->jpwl_hprot_TPH[i] = 0; /* absent */
+ }
+ };
+ {
+ int i;
+ for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
+ parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
+ parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
+ parameters->jpwl_pprot[i] = 0; /* absent */
+ }
+ };
+ parameters->jpwl_sens_size = 0; /* 0 means no ESD */
+ parameters->jpwl_sens_addr = 0; /* 0 means auto */
+ parameters->jpwl_sens_range = 0; /* 0 means packet */
+ parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
+ {
+ int i;
+ for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
+ parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
+ parameters->jpwl_sens_TPH[i] = -1; /* absent */
+ }
+ };
+#endif /* USE_JPWL */
+/* <<UniPG */
+ }
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
+ opj_cparameters_t *parameters,
+ opj_image_t *p_image)
+{
+ if (p_codec && parameters && p_image) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+
+ if (! l_codec->is_decompressor) {
+ l_codec->m_codec_data.m_compression.opj_setup_encoder( l_codec->m_codec,
+ parameters,
+ p_image,
+ &(l_codec->m_event_mgr) );
+ return OPJ_TRUE;
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
+ opj_image_t * p_image,
+ opj_stream_t *p_stream)
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return l_codec->m_codec_data.m_compression.opj_start_compress( l_codec->m_codec,
+ l_stream,
+ p_image,
+ &(l_codec->m_event_mgr));
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
+{
+ if (p_info && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
+ l_stream,
+ &(l_codec->m_event_mgr));
+ }
+ }
+
+ return OPJ_FALSE;
+
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
+ opj_stream_t *p_stream)
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
+ l_stream,
+ &(l_codec->m_event_mgr));
+ }
+ }
+ return OPJ_FALSE;
+
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
+ opj_stream_t *p_stream)
+{
+ if (p_codec && p_stream) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (! l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
+ l_stream,
+ &(l_codec->m_event_mgr) );
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
+ OPJ_FLOAT32 * pEncodingMatrix,
+ OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
+{
+ OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
+ OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
+ OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
+
+ /* add MCT capability */
+ OPJ_INT32 rsiz = (OPJ_INT32)parameters->cp_rsiz | (OPJ_INT32)OPJ_MCT;
+ parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
+ parameters->irreversible = 1;
+
+ /* use array based MCT */
+ parameters->tcp_mct = 2;
+ parameters->mct_data = opj_malloc(l_mct_total_size);
+ if (! parameters->mct_data) {
+ return OPJ_FALSE;
+ }
+
+ memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
+ memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_t *p_stream )
+{
+ if (p_codec && p_stream && p_data) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+ opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
+
+ if (l_codec->is_decompressor) {
+ return OPJ_FALSE;
+ }
+
+ return l_codec->m_codec_data.m_compression.opj_write_tile( l_codec->m_codec,
+ p_tile_index,
+ p_data,
+ p_data_size,
+ l_stream,
+ &(l_codec->m_event_mgr) );
+ }
+
+ return OPJ_FALSE;
+}
+
+/* ---------------------------------------------------------------------- */
+
+void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
+{
+ if (p_codec) {
+ opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
+
+ if (l_codec->is_decompressor) {
+ l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
+ }
+ else {
+ l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
+ }
+
+ l_codec->m_codec = 00;
+ opj_free(l_codec);
+ }
+}
+
+/* ---------------------------------------------------------------------- */
+
+void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
+ OPJ_INT32 info_flag,
+ FILE* output_stream)
+{
+ if (p_codec) {
+ opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
+
+ l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
+ return;
+ }
+
+ fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
+ return;
+}
+
+opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
+{
+ if (p_codec) {
+ opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
+
+ return l_codec->opj_get_codec_info(l_codec->m_codec);
+ }
+
+ return NULL;
+}
+
+void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
+ if (cstr_info) {
+
+ if ((*cstr_info)->m_default_tile_info.tccp_info){
+ opj_free((*cstr_info)->m_default_tile_info.tccp_info);
+ }
+
+ if ((*cstr_info)->tile_info){
+ /* FIXME not used for the moment*/
+ }
+
+ opj_free((*cstr_info));
+ (*cstr_info) = NULL;
+ }
+}
+
+opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
+{
+ if (p_codec) {
+ opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
+
+ return l_codec->opj_get_codec_index(l_codec->m_codec);
+ }
+
+ return NULL;
+}
+
+void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
+{
+ if (*p_cstr_index){
+ j2k_destroy_cstr_index(*p_cstr_index);
+ (*p_cstr_index) = NULL;
+ }
+}
+
+opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream_v3 (const char *fname, OPJ_BOOL p_is_read_stream)
+{
+ return opj_stream_create_file_stream_v3(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
+}
+
+opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream_v3 (
+ const char *fname,
+ OPJ_SIZE_T p_size,
+ OPJ_BOOL p_is_read_stream)
+{
+ opj_stream_t* l_stream = 00;
+ FILE *p_file;
+ const char *mode;
+
+ if (! fname) {
+ return NULL;
+ }
+
+ if(p_is_read_stream) mode = "rb"; else mode = "wb";
+
+ p_file = fopen(fname, mode);
+
+ if (! p_file) {
+ return NULL;
+ }
+
+ l_stream = opj_stream_create(p_size,p_is_read_stream);
+ if (! l_stream) {
+ fclose(p_file);
+ return NULL;
+ }
+
+ opj_stream_set_user_data_v3(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
+ opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
+ opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
+ opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
+ opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
+ opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
+
+ return l_stream;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.h
new file mode 100644
index 0000000000..29a9a9084f
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/openjpeg.h
@@ -0,0 +1,1501 @@
+ /*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006-2007, Parvatha Elangovan
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * Copyright (c) 2010-2011, Kaori Hagihara
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPENJPEG_H
+#define OPENJPEG_H
+
+#ifndef OPJ_STATIC
+#define OPJ_STATIC
+#endif
+
+
+
+/*
+==========================================================
+ Compiler directives
+==========================================================
+*/
+
+/*
+The inline keyword is supported by C99 but not by C90.
+Most compilers implement their own version of this keyword ...
+*/
+#ifndef INLINE
+ #if defined(_MSC_VER)
+ #define INLINE __forceinline
+ #elif defined(__GNUC__)
+ #define INLINE __inline__
+ #elif defined(__MWERKS__)
+ #define INLINE inline
+ #else
+ /* add other compilers here ... */
+ #define INLINE
+ #endif /* defined(<Compiler>) */
+#endif /* INLINE */
+
+/* deprecated attribute */
+#ifdef __GNUC__
+ #define OPJ_DEPRECATED(func) func __attribute__ ((deprecated))
+#elif defined(_MSC_VER)
+ #define OPJ_DEPRECATED(func) __declspec(deprecated) func
+#else
+ #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
+ #define OPJ_DEPRECATED(func) func
+#endif
+
+#if defined(OPJ_STATIC) || !defined(_WIN32)
+/* http://gcc.gnu.org/wiki/Visibility */
+#if __GNUC__ >= 4
+#define OPJ_API __attribute__ ((visibility ("default")))
+#define OPJ_LOCAL __attribute__ ((visibility ("hidden")))
+#else
+#define OPJ_API
+#define OPJ_LOCAL
+#endif
+#define OPJ_CALLCONV
+#else
+#define OPJ_CALLCONV __stdcall
+/*
+The following ifdef block is the standard way of creating macros which make exporting
+from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS
+symbol defined on the command line. this symbol should not be defined on any project
+that uses this DLL. This way any other project whose source files include this file see
+OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols
+defined with this macro as being exported.
+*/
+#if defined(OPJ_EXPORTS) || defined(DLL_EXPORT)
+#define OPJ_API __declspec(dllexport)
+#else
+#define OPJ_API __declspec(dllimport)
+#endif /* OPJ_EXPORTS */
+#endif /* !OPJ_STATIC || !_WIN32 */
+
+typedef int OPJ_BOOL;
+#define OPJ_TRUE 1
+#define OPJ_FALSE 0
+
+typedef char OPJ_CHAR;
+typedef float OPJ_FLOAT32;
+typedef double OPJ_FLOAT64;
+typedef unsigned char OPJ_BYTE;
+#include "opj_stdint.h"
+
+typedef int8_t OPJ_INT8;
+typedef uint8_t OPJ_UINT8;
+typedef int16_t OPJ_INT16;
+typedef uint16_t OPJ_UINT16;
+typedef int32_t OPJ_INT32;
+typedef uint32_t OPJ_UINT32;
+typedef int64_t OPJ_INT64;
+typedef uint64_t OPJ_UINT64;
+
+typedef int64_t OPJ_OFF_T; /* 64-bit file offset type */
+
+#include <stdio.h>
+typedef size_t OPJ_SIZE_T;
+
+/* Avoid compile-time warning because parameter is not used */
+#define OPJ_ARG_NOT_USED(x) (void)(x)
+
+/*
+==========================================================
+ Useful constant definitions
+==========================================================
+*/
+
+#define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */
+
+#define OPJ_J2K_MAXRLVLS 33 /**< Number of maximum resolution level authorized */
+#define OPJ_J2K_MAXBANDS (3*OPJ_J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */
+
+#define OPJ_J2K_DEFAULT_NB_SEGS 10
+#define OPJ_J2K_STREAM_CHUNK_SIZE 0x100000 /** 1 mega by default */
+#define OPJ_J2K_DEFAULT_HEADER_SIZE 1000
+#define OPJ_J2K_MCC_DEFAULT_NB_RECORDS 10
+#define OPJ_J2K_MCT_DEFAULT_NB_RECORDS 10
+
+/* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */
+#define JPWL_MAX_NO_TILESPECS 16 /**< Maximum number of tile parts expected by JPWL: increase at your will */
+#define JPWL_MAX_NO_PACKSPECS 16 /**< Maximum number of packet parts expected by JPWL: increase at your will */
+#define JPWL_MAX_NO_MARKERS 512 /**< Maximum number of JPWL markers: increase at your will */
+#define JPWL_PRIVATEINDEX_NAME "jpwl_index_privatefilename" /**< index file name used when JPWL is on */
+#define JPWL_EXPECTED_COMPONENTS 3 /**< Expect this number of components, so you'll find better the first EPB */
+#define JPWL_MAXIMUM_TILES 8192 /**< Expect this maximum number of tiles, to avoid some crashes */
+#define JPWL_MAXIMUM_HAMMING 2 /**< Expect this maximum number of bit errors in marker id's */
+#define JPWL_MAXIMUM_EPB_ROOM 65450 /**< Expect this maximum number of bytes for composition of EPBs */
+/* <<UniPG */
+
+/**
+ * EXPERIMENTAL FOR THE MOMENT
+ * Supported options about file information used only in j2k_dump
+*/
+#define OPJ_IMG_INFO 1 /**< Basic image information provided to the user */
+#define OPJ_J2K_MH_INFO 2 /**< Codestream information based only on the main header */
+#define OPJ_J2K_TH_INFO 4 /**< Tile information based on the current tile header */
+#define OPJ_J2K_TCH_INFO 8 /**< Tile/Component information of all tiles */
+#define OPJ_J2K_MH_IND 16 /**< Codestream index based only on the main header */
+#define OPJ_J2K_TH_IND 32 /**< Tile index based on the current tile */
+/*FIXME #define OPJ_J2K_CSTR_IND 48*/ /**< */
+#define OPJ_JP2_INFO 128 /**< JP2 file information */
+#define OPJ_JP2_IND 256 /**< JP2 file index */
+
+
+/*
+==========================================================
+ enum definitions
+==========================================================
+*/
+/**
+ * Rsiz Capabilities
+ * */
+typedef enum RSIZ_CAPABILITIES {
+ OPJ_STD_RSIZ = 0, /** Standard JPEG2000 profile*/
+ OPJ_CINEMA2K = 3, /** Profile name for a 2K image*/
+ OPJ_CINEMA4K = 4, /** Profile name for a 4K image*/
+ OPJ_MCT = 0x8100
+} OPJ_RSIZ_CAPABILITIES;
+
+/**
+ * Digital cinema operation mode
+ * */
+typedef enum CINEMA_MODE {
+ OPJ_OFF = 0, /** Not Digital Cinema*/
+ OPJ_CINEMA2K_24 = 1, /** 2K Digital Cinema at 24 fps*/
+ OPJ_CINEMA2K_48 = 2, /** 2K Digital Cinema at 48 fps*/
+ OPJ_CINEMA4K_24 = 3 /** 4K Digital Cinema at 24 fps*/
+}OPJ_CINEMA_MODE;
+
+/**
+ * Progression order
+ * */
+typedef enum PROG_ORDER {
+ OPJ_PROG_UNKNOWN = -1, /**< place-holder */
+ OPJ_LRCP = 0, /**< layer-resolution-component-precinct order */
+ OPJ_RLCP = 1, /**< resolution-layer-component-precinct order */
+ OPJ_RPCL = 2, /**< resolution-precinct-component-layer order */
+ OPJ_PCRL = 3, /**< precinct-component-resolution-layer order */
+ OPJ_CPRL = 4 /**< component-precinct-resolution-layer order */
+} OPJ_PROG_ORDER;
+
+/**
+ * Supported image color spaces
+*/
+typedef enum COLOR_SPACE {
+ OPJ_CLRSPC_UNKNOWN = -1, /**< not supported by the library */
+ OPJ_CLRSPC_UNSPECIFIED = 0, /**< not specified in the codestream */
+ OPJ_CLRSPC_SRGB = 1, /**< sRGB */
+ OPJ_CLRSPC_GRAY = 2, /**< grayscale */
+ OPJ_CLRSPC_SYCC = 3, /**< YUV */
+ OPJ_CLRSPC_EYCC = 4 /**< e-YCC */
+} OPJ_COLOR_SPACE;
+
+/**
+ * Supported codec
+*/
+typedef enum CODEC_FORMAT {
+ OPJ_CODEC_UNKNOWN = -1, /**< place-holder */
+ OPJ_CODEC_J2K = 0, /**< JPEG-2000 codestream : read/write */
+ OPJ_CODEC_JPT = 1, /**< JPT-stream (JPEG 2000, JPIP) : read only */
+ OPJ_CODEC_JP2 = 2 /**< JPEG-2000 file format : read/write */
+} OPJ_CODEC_FORMAT;
+
+
+/*
+==========================================================
+ event manager typedef definitions
+==========================================================
+*/
+
+/**
+ * Callback function prototype for events
+ * @param msg Event message
+ * @param client_data Client object where will be return the event message
+ * */
+typedef void (*opj_msg_callback) (const char *msg, void *client_data);
+
+/*
+==========================================================
+ codec typedef definitions
+==========================================================
+*/
+
+/**
+ * Progression order changes
+ *
+ */
+typedef struct opj_poc {
+ /** Resolution num start, Component num start, given by POC */
+ OPJ_UINT32 resno0, compno0;
+ /** Layer num end,Resolution num end, Component num end, given by POC */
+ OPJ_UINT32 layno1, resno1, compno1;
+ /** Layer num start,Precinct num start, Precinct num end */
+ OPJ_UINT32 layno0, precno0, precno1;
+ /** Progression order enum*/
+ OPJ_PROG_ORDER prg1,prg;
+ /** Progression order string*/
+ OPJ_CHAR progorder[5];
+ /** Tile number */
+ OPJ_UINT32 tile;
+ /** Start and end values for Tile width and height*/
+ OPJ_INT32 tx0,tx1,ty0,ty1;
+ /** Start value, initialised in pi_initialise_encode*/
+ OPJ_UINT32 layS, resS, compS, prcS;
+ /** End value, initialised in pi_initialise_encode */
+ OPJ_UINT32 layE, resE, compE, prcE;
+ /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/
+ OPJ_UINT32 txS,txE,tyS,tyE,dx,dy;
+ /** Temporary values for Tile parts, initialised in pi_create_encode */
+ OPJ_UINT32 lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t;
+} opj_poc_t;
+
+/**
+ * Compression parameters
+ * */
+typedef struct opj_cparameters {
+ /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */
+ OPJ_BOOL tile_size_on;
+ /** XTOsiz */
+ int cp_tx0;
+ /** YTOsiz */
+ int cp_ty0;
+ /** XTsiz */
+ int cp_tdx;
+ /** YTsiz */
+ int cp_tdy;
+ /** allocation by rate/distortion */
+ int cp_disto_alloc;
+ /** allocation by fixed layer */
+ int cp_fixed_alloc;
+ /** add fixed_quality */
+ int cp_fixed_quality;
+ /** fixed layer */
+ int *cp_matrice;
+ /** comment for coding */
+ char *cp_comment;
+ /** csty : coding style */
+ int csty;
+ /** progression order (default OPJ_LRCP) */
+ OPJ_PROG_ORDER prog_order;
+ /** progression order changes */
+ opj_poc_t POC[32];
+ /** number of progression order changes (POC), default to 0 */
+ OPJ_UINT32 numpocs;
+ /** number of layers */
+ int tcp_numlayers;
+ /** rates of layers */
+ float tcp_rates[100];
+ /** different psnr for successive layers */
+ float tcp_distoratio[100];
+ /** number of resolutions */
+ int numresolution;
+ /** initial code block width, default to 64 */
+ int cblockw_init;
+ /** initial code block height, default to 64 */
+ int cblockh_init;
+ /** mode switch (cblk_style) */
+ int mode;
+ /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */
+ int irreversible;
+ /** region of interest: affected component in [0..3], -1 means no ROI */
+ int roi_compno;
+ /** region of interest: upshift value */
+ int roi_shift;
+ /* number of precinct size specifications */
+ int res_spec;
+ /** initial precinct width */
+ int prcw_init[OPJ_J2K_MAXRLVLS];
+ /** initial precinct height */
+ int prch_init[OPJ_J2K_MAXRLVLS];
+
+ /**@name command line encoder parameters (not used inside the library) */
+ /*@{*/
+ /** input file name */
+ char infile[OPJ_PATH_LEN];
+ /** output file name */
+ char outfile[OPJ_PATH_LEN];
+ /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
+ int index_on;
+ /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
+ char index[OPJ_PATH_LEN];
+ /** subimage encoding: origin image offset in x direction */
+ int image_offset_x0;
+ /** subimage encoding: origin image offset in y direction */
+ int image_offset_y0;
+ /** subsampling value for dx */
+ int subsampling_dx;
+ /** subsampling value for dy */
+ int subsampling_dy;
+ /** input file format 0: PGX, 1: PxM, 2: BMP 3:TIF*/
+ int decod_format;
+ /** output file format 0: J2K, 1: JP2, 2: JPT */
+ int cod_format;
+ /*@}*/
+
+/* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */
+ /**@name JPWL encoding parameters */
+ /*@{*/
+ /** enables writing of EPC in MH, thus activating JPWL */
+ OPJ_BOOL jpwl_epc_on;
+ /** error protection method for MH (0,1,16,32,37-128) */
+ int jpwl_hprot_MH;
+ /** tile number of header protection specification (>=0) */
+ int jpwl_hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
+ /** error protection methods for TPHs (0,1,16,32,37-128) */
+ int jpwl_hprot_TPH[JPWL_MAX_NO_TILESPECS];
+ /** tile number of packet protection specification (>=0) */
+ int jpwl_pprot_tileno[JPWL_MAX_NO_PACKSPECS];
+ /** packet number of packet protection specification (>=0) */
+ int jpwl_pprot_packno[JPWL_MAX_NO_PACKSPECS];
+ /** error protection methods for packets (0,1,16,32,37-128) */
+ int jpwl_pprot[JPWL_MAX_NO_PACKSPECS];
+ /** enables writing of ESD, (0=no/1/2 bytes) */
+ int jpwl_sens_size;
+ /** sensitivity addressing size (0=auto/2/4 bytes) */
+ int jpwl_sens_addr;
+ /** sensitivity range (0-3) */
+ int jpwl_sens_range;
+ /** sensitivity method for MH (-1=no,0-7) */
+ int jpwl_sens_MH;
+ /** tile number of sensitivity specification (>=0) */
+ int jpwl_sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
+ /** sensitivity methods for TPHs (-1=no,0-7) */
+ int jpwl_sens_TPH[JPWL_MAX_NO_TILESPECS];
+ /*@}*/
+/* <<UniPG */
+
+ /** Digital Cinema compliance 0-not compliant, 1-compliant*/
+ OPJ_CINEMA_MODE cp_cinema;
+ /** Maximum rate for each component. If == 0, component size limitation is not considered */
+ int max_comp_size;
+ /** Profile name*/
+ OPJ_RSIZ_CAPABILITIES cp_rsiz;
+ /** Tile part generation*/
+ char tp_on;
+ /** Flag for Tile part generation*/
+ char tp_flag;
+ /** MCT (multiple component transform) */
+ char tcp_mct;
+ /** Enable JPIP indexing*/
+ OPJ_BOOL jpip_on;
+ /** Naive implementation of MCT restricted to a single reversible array based
+ encoding without offset concerning all the components. */
+ void * mct_data;
+} opj_cparameters_t;
+
+#define OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG 0x0001
+
+/**
+ * Decompression parameters
+ * */
+typedef struct opj_dparameters {
+ /**
+ Set the number of highest resolution levels to be discarded.
+ The image resolution is effectively divided by 2 to the power of the number of discarded levels.
+ The reduce factor is limited by the smallest total number of decomposition levels among tiles.
+ if != 0, then original dimension divided by 2^(reduce);
+ if == 0 or not used, image is decoded to the full resolution
+ */
+ OPJ_UINT32 cp_reduce;
+ /**
+ Set the maximum number of quality layers to decode.
+ If there are less quality layers than the specified number, all the quality layers are decoded.
+ if != 0, then only the first "layer" layers are decoded;
+ if == 0 or not used, all the quality layers are decoded
+ */
+ OPJ_UINT32 cp_layer;
+
+ /**@name command line decoder parameters (not used inside the library) */
+ /*@{*/
+ /** input file name */
+ char infile[OPJ_PATH_LEN];
+ /** output file name */
+ char outfile[OPJ_PATH_LEN];
+ /** input file format 0: J2K, 1: JP2, 2: JPT */
+ int decod_format;
+ /** output file format 0: PGX, 1: PxM, 2: BMP */
+ int cod_format;
+
+ /** Decoding area left boundary */
+ OPJ_UINT32 DA_x0;
+ /** Decoding area right boundary */
+ OPJ_UINT32 DA_x1;
+ /** Decoding area up boundary */
+ OPJ_UINT32 DA_y0;
+ /** Decoding area bottom boundary */
+ OPJ_UINT32 DA_y1;
+ /** Verbose mode */
+ OPJ_BOOL m_verbose;
+
+ /** tile number ot the decoded tile*/
+ OPJ_UINT32 tile_index;
+ /** Nb of tile to decode */
+ OPJ_UINT32 nb_tile_to_decode;
+
+ /*@}*/
+
+/* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */
+ /**@name JPWL decoding parameters */
+ /*@{*/
+ /** activates the JPWL correction capabilities */
+ OPJ_BOOL jpwl_correct;
+ /** expected number of components */
+ int jpwl_exp_comps;
+ /** maximum number of tiles */
+ int jpwl_max_tiles;
+ /*@}*/
+/* <<UniPG */
+
+ unsigned int flags;
+
+} opj_dparameters_t;
+
+
+/**
+ * JPEG2000 codec V2.
+ * */
+typedef void * opj_codec_t;
+
+/*
+==========================================================
+ I/O stream typedef definitions
+==========================================================
+*/
+
+/**
+ * Stream open flags.
+ * */
+/** The stream was opened for reading. */
+#define OPJ_STREAM_READ OPJ_TRUE
+/** The stream was opened for writing. */
+#define OPJ_STREAM_WRITE OPJ_FALSE
+
+/*
+ * Callback function prototype for read function
+ */
+typedef OPJ_SIZE_T (* opj_stream_read_fn) (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
+
+/*
+ * Callback function prototype for write function
+ */
+typedef OPJ_SIZE_T (* opj_stream_write_fn) (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
+
+/*
+ * Callback function prototype for skip function
+ */
+typedef OPJ_OFF_T (* opj_stream_skip_fn) (OPJ_OFF_T p_nb_bytes, void * p_user_data) ;
+
+/*
+ * Callback function prototype for seek function
+ */
+typedef OPJ_BOOL (* opj_stream_seek_fn) (OPJ_OFF_T p_nb_bytes, void * p_user_data) ;
+
+/*
+ * Callback function prototype for free user data function
+ */
+typedef void (* opj_stream_free_user_data_fn) (void * p_user_data) ;
+
+/*
+ * JPEG2000 Stream.
+ */
+typedef void * opj_stream_t;
+
+/*
+==========================================================
+ image typedef definitions
+==========================================================
+*/
+
+/**
+ * Defines a single image component
+ * */
+typedef struct opj_image_comp {
+ /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
+ OPJ_UINT32 dx;
+ /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
+ OPJ_UINT32 dy;
+ /** data width */
+ OPJ_UINT32 w;
+ /** data height */
+ OPJ_UINT32 h;
+ /** x component offset compared to the whole image */
+ OPJ_UINT32 x0;
+ /** y component offset compared to the whole image */
+ OPJ_UINT32 y0;
+ /** precision */
+ OPJ_UINT32 prec;
+ /** image depth in bits */
+ OPJ_UINT32 bpp;
+ /** signed (1) / unsigned (0) */
+ OPJ_UINT32 sgnd;
+ /** number of decoded resolution */
+ OPJ_UINT32 resno_decoded;
+ /** number of division by 2 of the out image compared to the original size of image */
+ OPJ_UINT32 factor;
+ /** image component data */
+ OPJ_INT32 *data;
+ /** alpha channel */
+ OPJ_UINT16 alpha;
+} opj_image_comp_t;
+
+/**
+ * Defines image data and characteristics
+ * */
+typedef struct opj_image {
+ /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area */
+ OPJ_UINT32 x0;
+ /** YOsiz: vertical offset from the origin of the reference grid to the top side of the image area */
+ OPJ_UINT32 y0;
+ /** Xsiz: width of the reference grid */
+ OPJ_UINT32 x1;
+ /** Ysiz: height of the reference grid */
+ OPJ_UINT32 y1;
+ /** number of components in the image */
+ OPJ_UINT32 numcomps;
+ /** color space: sRGB, Greyscale or YUV */
+ OPJ_COLOR_SPACE color_space;
+ /** image components */
+ opj_image_comp_t *comps;
+ /** 'restricted' ICC profile */
+ OPJ_BYTE *icc_profile_buf;
+ /** size of ICC profile */
+ OPJ_UINT32 icc_profile_len;
+
+ OPJ_INT8 useColorSpace; //liang
+} opj_image_t;
+
+
+/**
+ * Component parameters structure used by the opj_image_create function
+ * */
+typedef struct opj_image_comptparm {
+ /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
+ OPJ_UINT32 dx;
+ /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
+ OPJ_UINT32 dy;
+ /** data width */
+ OPJ_UINT32 w;
+ /** data height */
+ OPJ_UINT32 h;
+ /** x component offset compared to the whole image */
+ OPJ_UINT32 x0;
+ /** y component offset compared to the whole image */
+ OPJ_UINT32 y0;
+ /** precision */
+ OPJ_UINT32 prec;
+ /** image depth in bits */
+ OPJ_UINT32 bpp;
+ /** signed (1) / unsigned (0) */
+ OPJ_UINT32 sgnd;
+} opj_image_cmptparm_t;
+
+
+/*
+==========================================================
+ Information on the JPEG 2000 codestream
+==========================================================
+*/
+/* QUITE EXPERIMENTAL FOR THE MOMENT */
+
+/**
+ * Index structure : Information concerning a packet inside tile
+ * */
+typedef struct opj_packet_info {
+ /** packet start position (including SOP marker if it exists) */
+ OPJ_OFF_T start_pos;
+ /** end of packet header position (including EPH marker if it exists)*/
+ OPJ_OFF_T end_ph_pos;
+ /** packet end position */
+ OPJ_OFF_T end_pos;
+ /** packet distorsion */
+ double disto;
+} opj_packet_info_t;
+
+
+/* UniPG>> */
+/**
+ * Marker structure
+ * */
+typedef struct opj_marker_info {
+ /** marker type */
+ unsigned short int type;
+ /** position in codestream */
+ OPJ_OFF_T pos;
+ /** length, marker val included */
+ int len;
+} opj_marker_info_t;
+/* <<UniPG */
+
+/**
+ * Index structure : Information concerning tile-parts
+*/
+typedef struct opj_tp_info {
+ /** start position of tile part */
+ int tp_start_pos;
+ /** end position of tile part header */
+ int tp_end_header;
+ /** end position of tile part */
+ int tp_end_pos;
+ /** start packet of tile part */
+ int tp_start_pack;
+ /** number of packets of tile part */
+ int tp_numpacks;
+} opj_tp_info_t;
+
+/**
+ * Index structure : information regarding tiles
+*/
+typedef struct opj_tile_info {
+ /** value of thresh for each layer by tile cfr. Marcela */
+ double *thresh;
+ /** number of tile */
+ int tileno;
+ /** start position */
+ int start_pos;
+ /** end position of the header */
+ int end_header;
+ /** end position */
+ int end_pos;
+ /** precinct number for each resolution level (width) */
+ int pw[33];
+ /** precinct number for each resolution level (height) */
+ int ph[33];
+ /** precinct size (in power of 2), in X for each resolution level */
+ int pdx[33];
+ /** precinct size (in power of 2), in Y for each resolution level */
+ int pdy[33];
+ /** information concerning packets inside tile */
+ opj_packet_info_t *packet;
+ /** add fixed_quality */
+ int numpix;
+ /** add fixed_quality */
+ double distotile;
+ /** number of markers */
+ int marknum;
+ /** list of markers */
+ opj_marker_info_t *marker;
+ /** actual size of markers array */
+ int maxmarknum;
+ /** number of tile parts */
+ int num_tps;
+ /** information concerning tile parts */
+ opj_tp_info_t *tp;
+} opj_tile_info_t;
+
+/**
+ * Index structure of the codestream
+*/
+typedef struct opj_codestream_info {
+ /** maximum distortion reduction on the whole image (add for Marcela) */
+ double D_max;
+ /** packet number */
+ int packno;
+ /** writing the packet in the index with t2_encode_packets */
+ int index_write;
+ /** image width */
+ int image_w;
+ /** image height */
+ int image_h;
+ /** progression order */
+ OPJ_PROG_ORDER prog;
+ /** tile size in x */
+ int tile_x;
+ /** tile size in y */
+ int tile_y;
+ /** */
+ int tile_Ox;
+ /** */
+ int tile_Oy;
+ /** number of tiles in X */
+ int tw;
+ /** number of tiles in Y */
+ int th;
+ /** component numbers */
+ int numcomps;
+ /** number of layer */
+ int numlayers;
+ /** number of decomposition for each component */
+ int *numdecompos;
+/* UniPG>> */
+ /** number of markers */
+ int marknum;
+ /** list of markers */
+ opj_marker_info_t *marker;
+ /** actual size of markers array */
+ int maxmarknum;
+/* <<UniPG */
+ /** main header position */
+ int main_head_start;
+ /** main header position */
+ int main_head_end;
+ /** codestream's size */
+ int codestream_size;
+ /** information regarding tiles inside image */
+ opj_tile_info_t *tile;
+} opj_codestream_info_t;
+
+/* <----------------------------------------------------------- */
+/* new output managment of the codestream information and index */
+
+/**
+ * Tile-component coding parameters information
+ */
+typedef struct opj_tccp_info
+{
+ /** component index */
+ OPJ_UINT32 compno;
+ /** coding style */
+ OPJ_UINT32 csty;
+ /** number of resolutions */
+ OPJ_UINT32 numresolutions;
+ /** code-blocks width */
+ OPJ_UINT32 cblkw;
+ /** code-blocks height */
+ OPJ_UINT32 cblkh;
+ /** code-block coding style */
+ OPJ_UINT32 cblksty;
+ /** discrete wavelet transform identifier */
+ OPJ_UINT32 qmfbid;
+ /** quantisation style */
+ OPJ_UINT32 qntsty;
+ /** stepsizes used for quantization */
+ OPJ_UINT32 stepsizes_mant[OPJ_J2K_MAXBANDS];
+ /** stepsizes used for quantization */
+ OPJ_UINT32 stepsizes_expn[OPJ_J2K_MAXBANDS];
+ /** number of guard bits */
+ OPJ_UINT32 numgbits;
+ /** Region Of Interest shift */
+ OPJ_INT32 roishift;
+ /** precinct width */
+ OPJ_UINT32 prcw[OPJ_J2K_MAXRLVLS];
+ /** precinct height */
+ OPJ_UINT32 prch[OPJ_J2K_MAXRLVLS];
+}
+opj_tccp_info_t;
+
+/**
+ * Tile coding parameters information
+ */
+typedef struct opj_tile_v2_info {
+
+ /** number (index) of tile */
+ int tileno;
+ /** coding style */
+ OPJ_UINT32 csty;
+ /** progression order */
+ OPJ_PROG_ORDER prg;
+ /** number of layers */
+ OPJ_UINT32 numlayers;
+ /** multi-component transform identifier */
+ OPJ_UINT32 mct;
+
+ /** information concerning tile component parameters*/
+ opj_tccp_info_t *tccp_info;
+
+} opj_tile_info_v2_t;
+
+/**
+ * Information structure about the codestream (FIXME should be expand and enhance)
+ */
+typedef struct opj_codestream_info_v2 {
+ /* Tile info */
+ /** tile origin in x = XTOsiz */
+ OPJ_UINT32 tx0;
+ /** tile origin in y = YTOsiz */
+ OPJ_UINT32 ty0;
+ /** tile size in x = XTsiz */
+ OPJ_UINT32 tdx;
+ /** tile size in y = YTsiz */
+ OPJ_UINT32 tdy;
+ /** number of tiles in X */
+ OPJ_UINT32 tw;
+ /** number of tiles in Y */
+ OPJ_UINT32 th;
+
+ /** number of components*/
+ OPJ_UINT32 nbcomps;
+
+ /** Default information regarding tiles inside image */
+ opj_tile_info_v2_t m_default_tile_info;
+
+ /** information regarding tiles inside image */
+ opj_tile_info_v2_t *tile_info; /* FIXME not used for the moment */
+
+} opj_codestream_info_v2_t;
+
+
+/**
+ * Index structure about a tile part
+ */
+typedef struct opj_tp_index {
+ /** start position */
+ OPJ_OFF_T start_pos;
+ /** end position of the header */
+ OPJ_OFF_T end_header;
+ /** end position */
+ OPJ_OFF_T end_pos;
+
+} opj_tp_index_t;
+
+/**
+ * Index structure about a tile
+ */
+typedef struct opj_tile_index {
+ /** tile index */
+ OPJ_UINT32 tileno;
+
+ /** number of tile parts */
+ OPJ_UINT32 nb_tps;
+ /** current nb of tile part (allocated)*/
+ OPJ_UINT32 current_nb_tps;
+ /** current tile-part index */
+ OPJ_UINT32 current_tpsno;
+ /** information concerning tile parts */
+ opj_tp_index_t *tp_index;
+
+ /* UniPG>> */ /* NOT USED FOR THE MOMENT IN THE V2 VERSION */
+ /** number of markers */
+ OPJ_UINT32 marknum;
+ /** list of markers */
+ opj_marker_info_t *marker;
+ /** actual size of markers array */
+ OPJ_UINT32 maxmarknum;
+ /* <<UniPG */
+
+ /** packet number */
+ OPJ_UINT32 nb_packet;
+ /** information concerning packets inside tile */
+ opj_packet_info_t *packet_index;
+
+} opj_tile_index_t;
+
+/**
+ * Index structure of the codestream (FIXME should be expand and enhance)
+ */
+typedef struct opj_codestream_index {
+ /** main header start position (SOC position) */
+ OPJ_OFF_T main_head_start;
+ /** main header end position (first SOT position) */
+ OPJ_OFF_T main_head_end;
+
+ /** codestream's size */
+ OPJ_UINT64 codestream_size;
+
+/* UniPG>> */ /* NOT USED FOR THE MOMENT IN THE V2 VERSION */
+ /** number of markers */
+ OPJ_UINT32 marknum;
+ /** list of markers */
+ opj_marker_info_t *marker;
+ /** actual size of markers array */
+ OPJ_UINT32 maxmarknum;
+/* <<UniPG */
+
+ /** */
+ OPJ_UINT32 nb_of_tiles;
+ /** */
+ opj_tile_index_t *tile_index; /* FIXME not used for the moment */
+
+}opj_codestream_index_t;
+/* -----------------------------------------------------------> */
+
+/*
+==========================================================
+ Metadata from the JP2file
+==========================================================
+*/
+
+/**
+ * Info structure of the JP2 file
+ * EXPERIMENTAL FOR THE MOMENT
+ */
+typedef struct opj_jp2_metadata {
+ /** */
+ OPJ_INT32 not_used;
+
+} opj_jp2_metadata_t;
+
+/**
+ * Index structure of the JP2 file
+ * EXPERIMENTAL FOR THE MOMENT
+ */
+typedef struct opj_jp2_index {
+ /** */
+ OPJ_INT32 not_used;
+
+} opj_jp2_index_t;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/*
+==========================================================
+ openjpeg version
+==========================================================
+*/
+
+/* Get the version of the openjpeg library*/
+OPJ_API const char * OPJ_CALLCONV opj_version(void);
+
+/*
+==========================================================
+ image functions definitions
+==========================================================
+*/
+
+/**
+ * Create an image
+ *
+ * @param numcmpts number of components
+ * @param cmptparms components parameters
+ * @param clrspc image color space
+ * @return returns a new image structure if successful, returns NULL otherwise
+ * */
+OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
+
+/**
+ * Deallocate any resources associated with an image
+ *
+ * @param image image to be destroyed
+ */
+OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image);
+
+/**
+ * Creates an image without allocating memory for the image (used in the new version of the library).
+ *
+ * @param numcmpts the number of components
+ * @param cmptparms the components parameters
+ * @param clrspc the image color space
+ *
+ * @return a new image structure if successful, NULL otherwise.
+*/
+OPJ_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
+
+/*
+==========================================================
+ stream functions definitions
+==========================================================
+*/
+
+/**
+ * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
+ *
+ * @param p_is_input if set to true then the stream will be an input stream, an output stream else.
+ *
+ * @return a stream object.
+*/
+OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL p_is_input);
+
+/**
+ * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
+ *
+ * @param p_buffer_size FIXME DOC
+ * @param p_is_input if set to true then the stream will be an input stream, an output stream else.
+ *
+ * @return a stream object.
+*/
+OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size, OPJ_BOOL p_is_input);
+
+/**
+ * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
+ * close its own implementation of the stream.
+ *
+ * @param p_stream the stream to destroy.
+ */
+OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream);
+
+/**
+ * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream.
+ * If needed the user must close its own implementation of the stream.
+ *
+ * @param p_stream the stream to destroy.
+ */
+OPJ_API void OPJ_CALLCONV opj_stream_destroy_v3(opj_stream_t* p_stream);
+
+/**
+ * Sets the given function to be used as a read function.
+ * @param p_stream the stream to modify
+ * @param p_function the function to use a read function.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function);
+
+/**
+ * Sets the given function to be used as a write function.
+ * @param p_stream the stream to modify
+ * @param p_function the function to use a write function.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function);
+
+/**
+ * Sets the given function to be used as a skip function.
+ * @param p_stream the stream to modify
+ * @param p_function the function to use a skip function.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function);
+
+/**
+ * Sets the given function to be used as a seek function, the stream is then seekable.
+ * @param p_stream the stream to modify
+ * @param p_function the function to use a skip function.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function);
+
+/**
+ * Sets the given data to be used as a user data for the stream.
+ * @param p_stream the stream to modify
+ * @param p_data the data to set.
+ * @warning depending on your source object p_stream this function may leak, use opj_stream_set_user_data_v3
+*/
+OPJ_DEPRECATED(OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data));
+
+/**
+ * Sets the given data to be used as a user data for the stream.
+ * @param p_stream the stream to modify
+ * @param p_data the data to set.
+ * @param p_function the function to free p_data when opj_stream_destroy() is called.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_v3 (opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function);
+
+/**
+ * Sets the length of the user data for the stream.
+ *
+ * @param p_stream the stream to modify
+ * @param data_length length of the user_data.
+*/
+OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length);
+
+/**
+ * Create a stream from a file identified with its filename with default parameters (helper function)
+ * @param fname the filename of the file to stream
+ * @param p_is_read_stream whether the stream is a read stream (true) or not (false)
+*/
+OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream_v3 (const char *fname, OPJ_BOOL p_is_read_stream);
+
+/** Create a stream from a file identified with its filename with a specific buffer size
+ * @param fname the filename of the file to stream
+ * @param p_buffer_size size of the chunk used to stream
+ * @param p_is_read_stream whether the stream is a read stream (true) or not (false)
+*/
+OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream_v3 (const char *fname,
+ OPJ_SIZE_T p_buffer_size,
+ OPJ_BOOL p_is_read_stream);
+
+/*
+==========================================================
+ event manager functions definitions
+==========================================================
+*/
+/**
+ * Set the info handler use by openjpeg.
+ * @param p_codec the codec previously initialise
+ * @param p_callback the callback function which will be used
+ * @param p_user_data client object where will be returned the message
+*/
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data);
+/**
+ * Set the warning handler use by openjpeg.
+ * @param p_codec the codec previously initialise
+ * @param p_callback the callback function which will be used
+ * @param p_user_data client object where will be returned the message
+*/
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data);
+/**
+ * Set the error handler use by openjpeg.
+ * @param p_codec the codec previously initialise
+ * @param p_callback the callback function which will be used
+ * @param p_user_data client object where will be returned the message
+*/
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
+ opj_msg_callback p_callback,
+ void * p_user_data);
+
+/*
+==========================================================
+ codec functions definitions
+==========================================================
+*/
+
+/**
+ * Creates a J2K/JP2 decompression structure
+ * @param format Decoder to select
+ *
+ * @return Returns a handle to a decompressor if successful, returns NULL otherwise
+ * */
+OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
+
+/**
+ * Destroy a decompressor handle
+ *
+ * @param p_codec decompressor handle to destroy
+ */
+OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec);
+
+/**
+ * Read after the codestream if necessary
+ * @param p_codec the JPEG2000 codec to read.
+ * @param p_stream the JPEG2000 stream.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_decompress ( opj_codec_t *p_codec,
+ opj_stream_t *p_stream);
+
+
+/**
+ * Set decoding parameters to default values
+ * @param parameters Decompression parameters
+ */
+OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
+
+/**
+ * Setup the decoder with decompression parameters provided by the user and with the message handler
+ * provided by the user.
+ *
+ * @param p_codec decompressor handler
+ * @param parameters decompression parameters
+ *
+ * @return true if the decoder is correctly set
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
+ opj_dparameters_t *parameters );
+
+/**
+ * Decodes an image header.
+ *
+ * @param p_stream the jpeg2000 stream.
+ * @param p_codec the jpeg2000 codec to read.
+ * @param p_image the image structure initialized with the characteristics of encoded image.
+ *
+ * @return true if the main header of the codestream and the JP2 header is correctly read.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
+ opj_codec_t *p_codec,
+ opj_image_t **p_image);
+
+/**
+ * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param p_image the decoded image previously setted by opj_read_header
+ * @param p_start_x the left position of the rectangle to decode (in image coordinates).
+ * @param p_end_x the right position of the rectangle to decode (in image coordinates).
+ * @param p_start_y the up position of the rectangle to decode (in image coordinates).
+ * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
+ *
+ * @return true if the area could be set.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_decode_area( opj_codec_t *p_codec,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y );
+
+/**
+ * Decode an image from a JPEG-2000 codestream
+ *
+ * @param p_decompressor decompressor handle
+ * @param p_stream Input buffer stream
+ * @param p_image the decoded image
+ * @return true if success, otherwise false
+ * */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decode( opj_codec_t *p_decompressor,
+ opj_stream_t *p_stream,
+ opj_image_t *p_image);
+
+/**
+ * Get the decoded tile from the codec
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param p_stream input streamm
+ * @param p_image output image
+ * @param tile_index index of the tile which will be decode
+ *
+ * @return true if success, otherwise false
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
+ opj_stream_t *p_stream,
+ opj_image_t *p_image,
+ OPJ_UINT32 tile_index);
+
+/**
+ * Set the resolution factor of the decoded image
+ * @param p_codec the jpeg2000 codec.
+ * @param res_factor resolution factor to set
+ *
+ * @return true if success, otherwise false
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, OPJ_UINT32 res_factor);
+
+/**
+ * Writes a tile with the given data.
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param p_tile_index the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
+ * @param p_data pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
+ * @param p_data_size this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of
+ * tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes, depending on the precision of the given component.
+ * @param p_stream the stream to write data to.
+ *
+ * @return true if the data could be written.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_write_tile ( opj_codec_t *p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_t *p_stream );
+
+/**
+ * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
+ * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success.
+ * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
+ * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
+ * as depicted in opj_write_tile.
+ * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image).
+ * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image).
+ * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image).
+ * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image).
+ * @param p_nb_comps pointer to a value that will hold the number of components in the tile.
+ * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the
+ * codestream is over at the time of the call, the value will be set to false. The user should then stop
+ * the decoding.
+ * @param p_stream the stream to decode.
+ * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true.
+ * returning false may be the result of a shortage of memory or an internal error.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
+ opj_stream_t * p_stream,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_should_go_on );
+
+/**
+ * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
+ * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header.
+ * @param p_data pointer to a memory block that will hold the decoded data.
+ * @param p_data_size size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
+ * @param p_stream the stream to decode.
+ *
+ * @return true if the data could be decoded.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_t *p_stream );
+
+/* COMPRESSION FUNCTIONS*/
+
+/**
+ * Creates a J2K/JP2 compression structure
+ * @param format Coder to select
+ * @return Returns a handle to a compressor if successful, returns NULL otherwise
+ */
+OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
+
+/**
+Set encoding parameters to default values, that means :
+<ul>
+<li>Lossless
+<li>1 tile
+<li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
+<li>Size of code-block : 64 x 64
+<li>Number of resolutions: 6
+<li>No SOP marker in the codestream
+<li>No EPH marker in the codestream
+<li>No sub-sampling in x or y direction
+<li>No mode switch activated
+<li>Progression order: LRCP
+<li>No index file
+<li>No ROI upshifted
+<li>No offset of the origin of the image
+<li>No offset of the origin of the tiles
+<li>Reversible DWT 5-3
+</ul>
+@param parameters Compression parameters
+*/
+OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
+
+/**
+ * Setup the encoder parameters using the current image and using user parameters.
+ * @param p_codec Compressor handle
+ * @param parameters Compression parameters
+ * @param image Input filled image
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
+ opj_cparameters_t *parameters,
+ opj_image_t *image);
+
+/**
+ * Start to compress the current image.
+ * @param p_codec Compressor handle
+ * @param image Input filled image
+ * @param p_stream Input stgream
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_start_compress ( opj_codec_t *p_codec,
+ opj_image_t * p_image,
+ opj_stream_t *p_stream);
+
+/**
+ * End to compress the current image.
+ * @param p_codec Compressor handle
+ * @param p_stream Input stgream
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
+ opj_stream_t *p_stream);
+
+/**
+ * Encode an image into a JPEG-2000 codestream
+ * @param p_codec compressor handle
+ * @param p_stream Output buffer stream
+ *
+ * @return Returns true if successful, returns false otherwise
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_codec,
+ opj_stream_t *p_stream);
+/*
+==========================================================
+ codec output functions definitions
+==========================================================
+*/
+/* EXPERIMENTAL FUNCTIONS FOR NOW, USED ONLY IN J2K_DUMP*/
+
+/**
+Destroy Codestream information after compression or decompression
+@param cstr_info Codestream information structure
+*/
+OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info);
+
+
+/**
+ * Dump the codec information into the output stream
+ *
+ * @param p_codec the jpeg2000 codec.
+ * @param info_flag type of information dump.
+ * @param output_stream output stream where dump the informations get from the codec.
+ *
+ */
+OPJ_API void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
+ OPJ_INT32 info_flag,
+ FILE* output_stream);
+
+/**
+ * Get the codestream information from the codec
+ *
+ * @param p_codec the jpeg2000 codec.
+ *
+ * @return a pointer to a codestream information structure.
+ *
+ */
+OPJ_API opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec);
+
+/**
+ * Get the codestream index from the codec
+ *
+ * @param p_codec the jpeg2000 codec.
+ *
+ * @return a pointer to a codestream index structure.
+ *
+ */
+OPJ_API opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec);
+
+OPJ_API void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index);
+
+
+/**
+ * Get the JP2 file information from the codec FIXME
+ *
+ * @param p_codec the jpeg2000 codec.
+ *
+ * @return a pointer to a JP2 metadata structure.
+ *
+ */
+OPJ_API opj_jp2_metadata_t* OPJ_CALLCONV opj_get_jp2_metadata(opj_codec_t *p_codec);
+
+/**
+ * Get the JP2 file index from the codec FIXME
+ *
+ * @param p_codec the jpeg2000 codec.
+ *
+ * @return a pointer to a JP2 index structure.
+ *
+ */
+OPJ_API opj_jp2_index_t* OPJ_CALLCONV opj_get_jp2_index(opj_codec_t *p_codec);
+
+
+/*
+==========================================================
+ MCT functions
+==========================================================
+*/
+
+/**
+ * Sets the MCT matrix to use.
+ *
+ * @param parameters the parameters to change.
+ * @param pEncodingMatrix the encoding matrix.
+ * @param p_dc_shift the dc shift coefficients to use.
+ * @param pNbComp the number of components of the image.
+ *
+ * @return true if the parameters could be set.
+ */
+OPJ_API OPJ_BOOL OPJ_CALLCONV opj_set_MCT( opj_cparameters_t *parameters,
+ OPJ_FLOAT32 * pEncodingMatrix,
+ OPJ_INT32 * p_dc_shift,
+ OPJ_UINT32 pNbComp);
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* OPENJPEG_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.c
new file mode 100644
index 0000000000..0df99ef04d
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.c
@@ -0,0 +1,64 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/times.h>
+#endif /* _WIN32 */
+#include "opj_includes.h"
+
+OPJ_FLOAT64 opj_clock(void) {
+#ifdef _WIN32
+ /* _WIN32: use QueryPerformance (very accurate) */
+ LARGE_INTEGER freq , t ;
+ /* freq is the clock speed of the CPU */
+ QueryPerformanceFrequency(&freq) ;
+ /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
+ /* t is the high resolution performance counter (see MSDN) */
+ QueryPerformanceCounter ( & t ) ;
+ return ( t.QuadPart /(OPJ_FLOAT64) freq.QuadPart ) ;
+#else
+ /* Unix or Linux: use resource usage */
+ struct rusage t;
+ OPJ_FLOAT64 procTime;
+ /* (1) Get the rusage data structure at this moment (man getrusage) */
+ getrusage(0,&t);
+ /* (2) What is the elapsed time ? - CPU time = User time + System time */
+ /* (2a) Get the seconds */
+ procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
+ /* (2b) More precisely! Get the microseconds part ! */
+ return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
+#endif
+}
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.h
new file mode 100644
index 0000000000..6f5168b572
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_clock.h
@@ -0,0 +1,59 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __OPJ_CLOCK_H
+#define __OPJ_CLOCK_H
+/**
+@file opj_clock.h
+@brief Internal function for timing
+
+The functions in OPJ_CLOCK.C are internal utilities mainly used for timing.
+*/
+
+/** @defgroup MISC MISC - Miscellaneous internal functions */
+/*@{*/
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Difference in successive opj_clock() calls tells you the elapsed time
+@return Returns time in seconds
+*/
+OPJ_FLOAT64 opj_clock(void);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __OPJ_CLOCK_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_codec.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_codec.h
new file mode 100644
index 0000000000..4fdfa7d8b9
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_codec.h
@@ -0,0 +1,165 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __OPJ_CODEC_H
+#define __OPJ_CODEC_H
+/**
+@file opj_codec.h
+*/
+
+
+/**
+ * Main codec handler used for compression or decompression.
+ */
+typedef struct opj_codec_private
+{
+ /** FIXME DOC */
+ union
+ {
+ /**
+ * Decompression handler.
+ */
+ struct opj_decompression
+ {
+ /** Main header reading function handler */
+ OPJ_BOOL (*opj_read_header) ( struct opj_stream_private * cio,
+ void * p_codec,
+ opj_image_t **p_image,
+ struct opj_event_mgr * p_manager);
+
+ /** Decoding function */
+ OPJ_BOOL (*opj_decode) ( void * p_codec,
+ struct opj_stream_private * p_cio,
+ opj_image_t * p_image,
+ struct opj_event_mgr * p_manager);
+
+ /** FIXME DOC */
+ OPJ_BOOL (*opj_read_tile_header)( void * p_codec,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0,
+ OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1,
+ OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ OPJ_BOOL * p_should_go_on,
+ struct opj_stream_private * p_cio,
+ struct opj_event_mgr * p_manager);
+
+ /** FIXME DOC */
+ OPJ_BOOL (*opj_decode_tile_data)( void * p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ struct opj_stream_private * p_cio,
+ struct opj_event_mgr * p_manager);
+
+ /** Reading function used after codestream if necessary */
+ OPJ_BOOL (* opj_end_decompress) ( void *p_codec,
+ struct opj_stream_private * cio,
+ struct opj_event_mgr * p_manager);
+
+ /** Codec destroy function handler */
+ void (*opj_destroy) (void * p_codec);
+
+ /** Setup decoder function handler */
+ void (*opj_setup_decoder) ( void * p_codec, opj_dparameters_t * p_param);
+
+ /** Set decode area function handler */
+ OPJ_BOOL (*opj_set_decode_area) ( void * p_codec,
+ opj_image_t * p_image,
+ OPJ_INT32 p_start_x,
+ OPJ_INT32 p_end_x,
+ OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_y,
+ struct opj_event_mgr * p_manager);
+
+ /** Get tile function */
+ OPJ_BOOL (*opj_get_decoded_tile) ( void *p_codec,
+ opj_stream_private_t * p_cio,
+ opj_image_t *p_image,
+ struct opj_event_mgr * p_manager,
+ OPJ_UINT32 tile_index);
+
+ /** Set the decoded resolution factor */
+ OPJ_BOOL (*opj_set_decoded_resolution_factor) ( void * p_codec,
+ OPJ_UINT32 res_factor,
+ opj_event_mgr_t * p_manager);
+ } m_decompression;
+
+ /**
+ * Compression handler. FIXME DOC
+ */
+ struct opj_compression
+ {
+ OPJ_BOOL (* opj_start_compress) ( void *p_codec,
+ struct opj_stream_private * cio,
+ struct opj_image * p_image,
+ struct opj_event_mgr * p_manager);
+
+ OPJ_BOOL (* opj_encode) ( void * p_codec,
+ struct opj_stream_private *p_cio,
+ struct opj_event_mgr * p_manager);
+
+ OPJ_BOOL (* opj_write_tile) ( void * p_codec,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ struct opj_stream_private * p_cio,
+ struct opj_event_mgr * p_manager);
+
+ OPJ_BOOL (* opj_end_compress) ( void * p_codec,
+ struct opj_stream_private * p_cio,
+ struct opj_event_mgr * p_manager);
+
+ void (* opj_destroy) (void * p_codec);
+
+ void (* opj_setup_encoder) ( void * p_codec,
+ opj_cparameters_t * p_param,
+ struct opj_image * p_image,
+ struct opj_event_mgr * p_manager);
+ } m_compression;
+ } m_codec_data;
+ /** FIXME DOC*/
+ void * m_codec;
+ /** Event handler */
+ opj_event_mgr_t m_event_mgr;
+ /** Flag to indicate if the codec is used to decode or encode*/
+ OPJ_BOOL is_decompressor;
+ void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
+ opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
+ opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
+}
+opj_codec_private_t;
+
+
+#endif /* __OPJ_CODEC_H */
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h
new file mode 100644
index 0000000000..2f91e850a1
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h
@@ -0,0 +1,2 @@
+/* create opj_config.h for CMake */
+#define OPJ_HAVE_STDINT_H 1
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h.cmake.in b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h.cmake.in
new file mode 100644
index 0000000000..2f5311b87c
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config.h.cmake.in
@@ -0,0 +1,2 @@
+/* create opj_config.h for CMake */
+#cmakedefine OPJ_HAVE_STDINT_H @OPJ_HAVE_STDINT_H@
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config_private.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config_private.h
new file mode 100644
index 0000000000..37077fc961
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_config_private.h
@@ -0,0 +1,43 @@
+/* create opj_config_private.h for CMake */
+/* #undef OPJ_HAVE_INTTYPES_H */
+
+#ifndef _WIN32
+#ifndef OPJ_HAVE_INTTYPES_H
+#define OPJ_HAVE_INTTYPES_H
+#endif
+#endif
+
+#define OPJ_PACKAGE_VERSION "2.1.0"
+
+/* Not used by openjp2*/
+/*#define HAVE_MEMORY_H 1*/
+/*#define HAVE_STDLIB_H 1*/
+/* #undef HAVE_STRINGS_H */
+/*#define HAVE_STRING_H 1*/
+/*#define HAVE_SYS_STAT_H 1*/
+/*#define HAVE_SYS_TYPES_H 1 */
+/* #undef HAVE_UNISTD_H */
+
+/* #undef _LARGEFILE_SOURCE */
+/* #undef _LARGE_FILES */
+/* #undef _FILE_OFFSET_BITS */
+/* #undef OPJ_HAVE_FSEEKO */
+
+#ifndef _WIN32
+#ifndef OPJ_HAVE_FSEEKO
+#define OPJ_HAVE_FSEEKO
+#endif
+#endif
+
+/* Byte order. */
+/* All compilers that support Mac OS X define either __BIG_ENDIAN__ or
+__LITTLE_ENDIAN__ to match the endianness of the architecture being
+compiled for. This is not necessarily the same as the architecture of the
+machine doing the building. In order to support Universal Binaries on
+Mac OS X, we prefer those defines to decide the endianness.
+On other platforms we use the result of the TRY_RUN. */
+#if !defined(__APPLE__)
+/* #undef OPJ_BIG_ENDIAN */
+#elif defined(__BIG_ENDIAN__)
+# define OPJ_BIG_ENDIAN
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_includes.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_includes.h
new file mode 100644
index 0000000000..d9238b1bfd
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_includes.h
@@ -0,0 +1,180 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPJ_INCLUDES_H
+#define OPJ_INCLUDES_H
+
+/*
+ * This must be included before any system headers,
+ * since they can react to macro defined there
+ */
+#include "opj_config_private.h"
+
+/*
+ ==========================================================
+ Standard includes used by the library
+ ==========================================================
+*/
+#include <memory.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <float.h>
+#include <time.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <assert.h>
+
+/*
+ Use fseeko() and ftello() if they are available since they use
+ 'off_t' rather than 'long'. It is wrong to use fseeko() and
+ ftello() only on systems with special LFS support since some systems
+ (e.g. FreeBSD) support a 64-bit off_t by default.
+*/
+#if defined(OPJ_HAVE_FSEEKO) && !defined(fseek)
+# define fseek fseeko
+# define ftell ftello
+#endif
+
+
+#if defined(WIN32) && !defined(Windows95) && !defined(__BORLANDC__) && \
+ !(defined(_MSC_VER) && _MSC_VER < 1400) && \
+ !(defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x800)
+ /*
+ Windows '95 and Borland C do not support _lseeki64
+ Visual Studio does not support _fseeki64 and _ftelli64 until the 2005 release.
+ Without these interfaces, files over 2GB in size are not supported for Windows.
+ */
+# define OPJ_FSEEK(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence)
+# define OPJ_FSTAT(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff)
+# define OPJ_FTELL(stream) /* __int64 */ _ftelli64(stream)
+# define OPJ_STAT_STRUCT_T struct _stati64
+# define OPJ_STAT(path,stat_buff) _stati64(path,/* struct _stati64 */ stat_buff)
+#else
+# define OPJ_FSEEK(stream,offset,whence) fseek(stream,offset,whence)
+# define OPJ_FSTAT(fildes,stat_buff) fstat(fildes,stat_buff)
+# define OPJ_FTELL(stream) ftell(stream)
+# define OPJ_STAT_STRUCT_T struct stat
+# define OPJ_STAT(path,stat_buff) stat(path,stat_buff)
+#endif
+
+
+/*
+ ==========================================================
+ OpenJPEG interface
+ ==========================================================
+ */
+#include "openjpeg.h"
+
+/*
+ ==========================================================
+ OpenJPEG modules
+ ==========================================================
+*/
+
+/* Ignore GCC attributes if this is not GCC */
+#ifndef __GNUC__
+ #define __attribute__(x) /* __attribute__(x) */
+#endif
+
+
+/* Are restricted pointers available? (C99) */
+#if (__STDC_VERSION__ != 199901L)
+ /* Not a C99 compiler */
+ #ifdef __GNUC__
+ #define restrict __restrict__
+ #else
+ #define restrict /* restrict */
+ #endif
+#endif
+
+/* MSVC before 2013 and Borland C do not have lrintf */
+#if defined(_MSC_VER) && (_MSC_VER < 1800) || defined(__BORLANDC__)
+static INLINE long lrintf(float f){
+#ifdef _M_X64
+ return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));
+#else
+ int i;
+
+ _asm{
+ fld f
+ fistp i
+ };
+
+ return i;
+#endif
+}
+#endif
+
+#include "opj_inttypes.h"
+#include "opj_clock.h"
+#include "opj_malloc.h"
+#include "function_list.h"
+#include "event.h"
+#include "bio.h"
+#include "cio.h"
+
+#include "image.h"
+#include "invert.h"
+#include "j2k.h"
+#include "jp2.h"
+
+#include "mqc.h"
+#include "raw.h"
+#include "bio.h"
+
+#include "pi.h"
+#include "tgt.h"
+#include "tcd.h"
+#include "t1.h"
+#include "dwt.h"
+#include "t2.h"
+#include "mct.h"
+#include "opj_intmath.h"
+
+#ifdef USE_JPIP
+#include "cidx_manager.h"
+#include "indexbox_manager.h"
+#endif
+
+/* JPWL>> */
+#ifdef USE_JPWL
+#include "openjpwl/jpwl.h"
+#endif /* USE_JPWL */
+/* <<JPWL */
+
+/* V2 */
+#include "opj_codec.h"
+
+
+#endif /* OPJ_INCLUDES_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_intmath.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_intmath.h
new file mode 100644
index 0000000000..dc89895301
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_intmath.h
@@ -0,0 +1,178 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __INT_H
+#define __INT_H
+/**
+@file opj_intmath.h
+@brief Implementation of operations on integers (INT)
+
+The functions in OPJ_INTMATH.H have for goal to realize operations on integers.
+*/
+
+/** @defgroup OPJ_INTMATH OPJ_INTMATH - Implementation of operations on integers */
+/*@{*/
+
+/** @name Exported functions (see also openjpeg.h) */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Get the minimum of two integers
+@return Returns a if a < b else b
+*/
+static INLINE OPJ_INT32 opj_int_min(OPJ_INT32 a, OPJ_INT32 b) {
+ return a < b ? a : b;
+}
+
+/**
+Get the minimum of two integers
+@return Returns a if a < b else b
+*/
+static INLINE OPJ_UINT32 opj_uint_min(OPJ_UINT32 a, OPJ_UINT32 b) {
+ return a < b ? a : b;
+}
+
+/**
+Get the maximum of two integers
+@return Returns a if a > b else b
+*/
+static INLINE OPJ_INT32 opj_int_max(OPJ_INT32 a, OPJ_INT32 b) {
+ return (a > b) ? a : b;
+}
+
+/**
+Get the maximum of two integers
+@return Returns a if a > b else b
+*/
+static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32 a, OPJ_UINT32 b) {
+ return (a > b) ? a : b;
+}
+
+/**
+Clamp an integer inside an interval
+@return
+<ul>
+<li>Returns a if (min < a < max)
+<li>Returns max if (a > max)
+<li>Returns min if (a < min)
+</ul>
+*/
+static INLINE OPJ_INT32 opj_int_clamp(OPJ_INT32 a, OPJ_INT32 min, OPJ_INT32 max) {
+ if (a < min)
+ return min;
+ if (a > max)
+ return max;
+ return a;
+}
+/**
+@return Get absolute value of integer
+*/
+static INLINE OPJ_INT32 opj_int_abs(OPJ_INT32 a) {
+ return a < 0 ? -a : a;
+}
+/**
+Divide an integer and round upwards
+@return Returns a divided by b
+*/
+static INLINE OPJ_INT32 opj_int_ceildiv(OPJ_INT32 a, OPJ_INT32 b) {
+ assert(b);
+ return (a + b - 1) / b;
+}
+
+/**
+Divide an integer and round upwards
+@return Returns a divided by b
+*/
+static INLINE OPJ_UINT32 opj_uint_ceildiv(OPJ_UINT32 a, OPJ_UINT32 b) {
+ return (a + b - 1) / b;
+}
+
+/**
+Divide an integer by a power of 2 and round upwards
+@return Returns a divided by 2^b
+*/
+static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b) {
+ return (OPJ_INT32)((a + (OPJ_INT64)(1 << b) - 1) >> b);
+}
+/**
+Divide an integer by a power of 2 and round downwards
+@return Returns a divided by 2^b
+*/
+static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) {
+ return a >> b;
+}
+/**
+Get logarithm of an integer and round downwards
+@return Returns log2(a)
+*/
+static INLINE OPJ_INT32 opj_int_floorlog2(OPJ_INT32 a) {
+ OPJ_INT32 l;
+ for (l = 0; a > 1; l++) {
+ a >>= 1;
+ }
+ return l;
+}
+/**
+Get logarithm of an integer and round downwards
+@return Returns log2(a)
+*/
+static INLINE OPJ_UINT32 opj_uint_floorlog2(OPJ_UINT32 a) {
+ OPJ_UINT32 l;
+ for (l = 0; a > 1; ++l)
+ {
+ a >>= 1;
+ }
+ return l;
+}
+
+/**
+Multiply two fixed-precision rational numbers.
+@param a
+@param b
+@return Returns a * b
+*/
+static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b) {
+ OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
+ temp += temp & 4096;
+ return (OPJ_INT32) (temp >> 13) ;
+}
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_inttypes.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_inttypes.h
new file mode 100644
index 0000000000..e74aed6a17
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_inttypes.h
@@ -0,0 +1,48 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2012, Mathieu Malaterre <mathieu.malaterre@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPJ_INTTYPES_H
+#define OPJ_INTTYPES_H
+
+#include "opj_config_private.h"
+#ifdef OPJ_HAVE_INTTYPES_H
+#include <inttypes.h>
+#else
+#if defined(_WIN32)
+#define PRId64 "I64d"
+#define PRIi64 "I64i"
+#define PRIu64 "I64u"
+#define PRIx64 "I64x"
+#else
+#error unsupported platform
+#endif
+#endif
+
+#endif /* OPJ_INTTYPES_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_malloc.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_malloc.h
new file mode 100644
index 0000000000..ed1dfaf983
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_malloc.h
@@ -0,0 +1,194 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __OPJ_MALLOC_H
+#define __OPJ_MALLOC_H
+/**
+@file opj_malloc.h
+@brief Internal functions
+
+The functions in opj_malloc.h are internal utilities used for memory management.
+*/
+
+/** @defgroup MISC MISC - Miscellaneous internal functions */
+/*@{*/
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Allocate an uninitialized memory block
+@param size Bytes to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+#define _FOXIT_MEM_MANAGER_
+#ifdef _FOXIT_MEM_MANAGER_
+void* opj_malloc(size_t size);
+void* opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
+void* opj_realloc(void * m, size_t s);
+void opj_free(void * m);
+
+#define opj_aligned_malloc(size) opj_malloc(size)
+#define opj_aligned_free(m) opj_free(m)
+#else
+#ifdef ALLOC_PERF_OPT
+void * OPJ_CALLCONV opj_malloc(size_t size);
+#else
+/* prevent assertion on overflow for MSVC */
+#ifdef _MSC_VER
+#define opj_malloc(size) ((size_t)(size) >= (size_t)-0x100 ? NULL : malloc(size))
+#else
+#define opj_malloc(size) malloc(size)
+#endif
+#endif
+
+/**
+Allocate a memory block with elements initialized to 0
+@param num Blocks to allocate
+@param size Bytes per block to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+#ifdef ALLOC_PERF_OPT
+void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
+#else
+/* prevent assertion on overflow for MSVC */
+#ifdef _MSC_VER
+#define opj_calloc(num, size) ((size_t)(num) != 0 && (size_t)(num) >= (size_t)-0x100 / (size_t)(size) ? NULL : calloc(num, size))
+#else
+#define opj_calloc(num, size) calloc(num, size)
+#endif
+#endif
+
+/**
+Allocate memory aligned to a 16 byte boundry
+@param size Bytes to allocate
+@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
+*/
+/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
+#ifdef _WIN32
+ /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
+ #ifdef __GNUC__
+ #include <mm_malloc.h>
+ #define HAVE_MM_MALLOC
+ #else /* MSVC, Intel C++ */
+ #include <malloc.h>
+ #ifdef _mm_malloc
+ #define HAVE_MM_MALLOC
+ #endif
+ #endif
+#else /* Not _WIN32 */
+ #if defined(__sun)
+ #define HAVE_MEMALIGN
+ #elif defined(__FreeBSD__)
+ #define HAVE_POSIX_MEMALIGN
+ /* Linux x86_64 and OSX always align allocations to 16 bytes */
+ #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
+ #define HAVE_MEMALIGN
+ #include <malloc.h>
+ #endif
+#endif
+
+#define opj_aligned_malloc(size) malloc(size)
+#define opj_aligned_free(m) free(m)
+
+#ifdef HAVE_MM_MALLOC
+ #undef opj_aligned_malloc
+ #define opj_aligned_malloc(size) _mm_malloc(size, 16)
+ #undef opj_aligned_free
+ #define opj_aligned_free(m) _mm_free(m)
+#endif
+
+#ifdef HAVE_MEMALIGN
+ extern void* memalign(size_t, size_t);
+ #undef opj_aligned_malloc
+ #define opj_aligned_malloc(size) memalign(16, (size))
+ #undef opj_aligned_free
+ #define opj_aligned_free(m) free(m)
+#endif
+
+#ifdef HAVE_POSIX_MEMALIGN
+ #undef opj_aligned_malloc
+ extern int posix_memalign(void**, size_t, size_t);
+
+ static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
+ void* mem = NULL;
+ posix_memalign(&mem, 16, size);
+ return mem;
+ }
+ #undef opj_aligned_free
+ #define opj_aligned_free(m) free(m)
+#endif
+
+#ifdef ALLOC_PERF_OPT
+ #undef opj_aligned_malloc
+ #define opj_aligned_malloc(size) opj_malloc(size)
+ #undef opj_aligned_free
+ #define opj_aligned_free(m) opj_free(m)
+#endif
+
+/**
+Reallocate memory blocks.
+@param m Pointer to previously allocated memory block
+@param s New size in bytes
+@return Returns a void pointer to the reallocated (and possibly moved) memory block
+*/
+#ifdef ALLOC_PERF_OPT
+void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
+#else
+/* prevent assertion on overflow for MSVC */
+#ifdef _MSC_VER
+#define opj_realloc(m, s) ((size_t)(s) >= (size_t)-0x100 ? NULL : realloc(m, s))
+#else
+#define opj_realloc(m, s) realloc(m, s)
+#endif
+#endif
+
+/**
+Deallocates or frees a memory block.
+@param m Previously allocated memory block to be freed
+*/
+#ifdef ALLOC_PERF_OPT
+void OPJ_CALLCONV opj_free(void * m);
+#else
+#define opj_free(m) free(m)
+#endif
+
+#ifdef __GNUC__
+#pragma GCC poison malloc calloc realloc free
+#endif
+#endif
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __OPJ_MALLOC_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_stdint.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_stdint.h
new file mode 100644
index 0000000000..b8556a1938
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/opj_stdint.h
@@ -0,0 +1,52 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2012, Mathieu Malaterre <mathieu.malaterre@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPJ_STDINT_H
+#define OPJ_STDINT_H
+
+#include "opj_config.h"
+#ifdef OPJ_HAVE_STDINT_H
+#include <stdint.h>
+#else
+#if defined(_WIN32)
+typedef signed __int8 int8_t;
+typedef unsigned __int8 uint8_t;
+typedef signed __int16 int16_t;
+typedef unsigned __int16 uint16_t;
+typedef signed __int32 int32_t;
+typedef unsigned __int32 uint32_t;
+typedef signed __int64 int64_t;
+typedef unsigned __int64 uint64_t;
+#else
+#error unsupported platform
+#endif
+#endif
+
+#endif /* OPJ_STDINT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.c
new file mode 100644
index 0000000000..02bbefeef9
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.c
@@ -0,0 +1,1876 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006-2007, Parvatha Elangovan
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup PI PI - Implementation of a packet iterator */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Get next packet in layer-resolution-component-precinct order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true
+*/
+static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
+/**
+Get next packet in resolution-layer-component-precinct order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true
+*/
+static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
+/**
+Get next packet in resolution-precinct-component-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true
+*/
+static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
+/**
+Get next packet in precinct-component-resolution-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true
+*/
+static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
+/**
+Get next packet in component-precinct-resolution-layer order.
+@param pi packet iterator to modify
+@return returns false if pi pointed to the last packet or else returns true
+*/
+static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
+
+/**
+ * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
+ *
+ * @param p_cp the coding parameters to modify
+ * @param p_tileno the tile index being concerned.
+ * @param p_tx0 X0 parameter for the tile
+ * @param p_tx1 X1 parameter for the tile
+ * @param p_ty0 Y0 parameter for the tile
+ * @param p_ty1 Y1 parameter for the tile
+ * @param p_max_prec the maximum precision for all the bands of the tile
+ * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
+ * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
+ * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
+ */
+static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
+ OPJ_UINT32 p_tileno,
+ OPJ_INT32 p_tx0,
+ OPJ_INT32 p_tx1,
+ OPJ_INT32 p_ty0,
+ OPJ_INT32 p_ty1,
+ OPJ_UINT32 p_max_prec,
+ OPJ_UINT32 p_max_res,
+ OPJ_UINT32 p_dx_min,
+ OPJ_UINT32 p_dy_min);
+
+/**
+ * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
+ *
+ * @param p_cp the coding parameters to modify
+ * @param p_num_comps the number of components
+ * @param p_tileno the tile index being concerned.
+ * @param p_tx0 X0 parameter for the tile
+ * @param p_tx1 X1 parameter for the tile
+ * @param p_ty0 Y0 parameter for the tile
+ * @param p_ty1 Y1 parameter for the tile
+ * @param p_max_prec the maximum precision for all the bands of the tile
+ * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
+ * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
+ * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
+ */
+static void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
+ OPJ_UINT32 p_num_comps,
+ OPJ_UINT32 p_tileno,
+ OPJ_INT32 p_tx0,
+ OPJ_INT32 p_tx1,
+ OPJ_INT32 p_ty0,
+ OPJ_INT32 p_ty1,
+ OPJ_UINT32 p_max_prec,
+ OPJ_UINT32 p_max_res,
+ OPJ_UINT32 p_dx_min,
+ OPJ_UINT32 p_dy_min);
+/**
+ * Gets the encoding parameters needed to update the coding parameters and all the pocs.
+ *
+ * @param p_image the image being encoded.
+ * @param p_cp the coding parameters.
+ * @param tileno the tile index of the tile being encoded.
+ * @param p_tx0 pointer that will hold the X0 parameter for the tile
+ * @param p_tx1 pointer that will hold the X1 parameter for the tile
+ * @param p_ty0 pointer that will hold the Y0 parameter for the tile
+ * @param p_ty1 pointer that will hold the Y1 parameter for the tile
+ * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
+ * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
+ * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
+ * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ */
+static void opj_get_encoding_parameters(const opj_image_t *p_image,
+ const opj_cp_t *p_cp,
+ OPJ_UINT32 tileno,
+ OPJ_INT32 * p_tx0,
+ OPJ_INT32 * p_tx1,
+ OPJ_INT32 * p_ty0,
+ OPJ_INT32 * p_ty1,
+ OPJ_UINT32 * p_dx_min,
+ OPJ_UINT32 * p_dy_min,
+ OPJ_UINT32 * p_max_prec,
+ OPJ_UINT32 * p_max_res );
+
+/**
+ * Gets the encoding parameters needed to update the coding parameters and all the pocs.
+ * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
+ * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
+ * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
+ * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
+ *
+ * @param p_image the image being encoded.
+ * @param p_cp the coding parameters.
+ * @param tileno the tile index of the tile being encoded.
+ * @param p_tx0 pointer that will hold the X0 parameter for the tile
+ * @param p_tx1 pointer that will hold the X1 parameter for the tile
+ * @param p_ty0 pointer that will hold the Y0 parameter for the tile
+ * @param p_ty1 pointer that will hold the Y1 parameter for the tile
+ * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
+ * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
+ * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
+ * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ * @param p_resolutions pointer to an area corresponding to the one described above.
+ */
+static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
+ const opj_cp_t *p_cp,
+ OPJ_UINT32 tileno,
+ OPJ_INT32 * p_tx0,
+ OPJ_INT32 * p_tx1,
+ OPJ_INT32 * p_ty0,
+ OPJ_INT32 * p_ty1,
+ OPJ_UINT32 * p_dx_min,
+ OPJ_UINT32 * p_dy_min,
+ OPJ_UINT32 * p_max_prec,
+ OPJ_UINT32 * p_max_res,
+ OPJ_UINT32 ** p_resolutions );
+/**
+ * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
+ * No other data is set. The include section of the packet iterator is not allocated.
+ *
+ * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
+ * @param p_cp the coding parameters.
+ * @param tileno the index of the tile from which creating the packet iterator.
+ */
+static opj_pi_iterator_t * opj_pi_create( const opj_image_t *p_image,
+ const opj_cp_t *p_cp,
+ OPJ_UINT32 tileno );
+/**
+ * FIXME DOC
+ */
+static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
+ opj_tcp_t * p_tcp,
+ OPJ_UINT32 p_max_precision,
+ OPJ_UINT32 p_max_res);
+/**
+ * FIXME DOC
+ */
+static void opj_pi_update_decode_poc ( opj_pi_iterator_t * p_pi,
+ opj_tcp_t * p_tcp,
+ OPJ_UINT32 p_max_precision,
+ OPJ_UINT32 p_max_res);
+
+/**
+ * FIXME DOC
+ */
+OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
+ opj_cp_t *cp,
+ OPJ_UINT32 tileno,
+ OPJ_UINT32 pino,
+ const OPJ_CHAR *prog);
+
+/*@}*/
+
+/*@}*/
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
+ opj_pi_comp_t *comp = NULL;
+ opj_pi_resolution_t *res = NULL;
+ OPJ_UINT32 index = 0;
+
+ if (!pi->first) {
+ comp = &pi->comps[pi->compno];
+ res = &comp->resolutions[pi->resno];
+ goto LABEL_SKIP;
+ } else {
+ pi->first = 0;
+ }
+
+ for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
+ for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
+ pi->resno++) {
+ for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+ comp = &pi->comps[pi->compno];
+ if (pi->resno >= comp->numresolutions) {
+ continue;
+ }
+ res = &comp->resolutions[pi->resno];
+ if (!pi->tp_on){
+ pi->poc.precno1 = res->pw * res->ph;
+ }
+ for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
+ index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+ if (!pi->include[index]) {
+ pi->include[index] = 1;
+ return OPJ_TRUE;
+ }
+LABEL_SKIP:;
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
+ opj_pi_comp_t *comp = NULL;
+ opj_pi_resolution_t *res = NULL;
+ OPJ_UINT32 index = 0;
+
+ if (!pi->first) {
+ comp = &pi->comps[pi->compno];
+ res = &comp->resolutions[pi->resno];
+ goto LABEL_SKIP;
+ } else {
+ pi->first = 0;
+ }
+
+ for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+ for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
+ for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+ comp = &pi->comps[pi->compno];
+ if (pi->resno >= comp->numresolutions) {
+ continue;
+ }
+ res = &comp->resolutions[pi->resno];
+ if(!pi->tp_on){
+ pi->poc.precno1 = res->pw * res->ph;
+ }
+ for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
+ index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+ if (!pi->include[index]) {
+ pi->include[index] = 1;
+ return OPJ_TRUE;
+ }
+LABEL_SKIP:;
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
+ opj_pi_comp_t *comp = NULL;
+ opj_pi_resolution_t *res = NULL;
+ OPJ_UINT32 index = 0;
+
+ if (!pi->first) {
+ goto LABEL_SKIP;
+ } else {
+ OPJ_UINT32 compno, resno;
+ pi->first = 0;
+ pi->dx = 0;
+ pi->dy = 0;
+ for (compno = 0; compno < pi->numcomps; compno++) {
+ comp = &pi->comps[compno];
+ for (resno = 0; resno < comp->numresolutions; resno++) {
+ OPJ_UINT32 dx, dy;
+ res = &comp->resolutions[resno];
+ dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+ dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+ pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+ pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
+ }
+ }
+ }
+if (!pi->tp_on){
+ pi->poc.ty0 = pi->ty0;
+ pi->poc.tx0 = pi->tx0;
+ pi->poc.ty1 = pi->ty1;
+ pi->poc.tx1 = pi->tx1;
+ }
+ for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
+ for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+ for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
+ for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+ OPJ_UINT32 levelno;
+ OPJ_INT32 trx0, try0;
+ OPJ_INT32 trx1, try1;
+ OPJ_UINT32 rpx, rpy;
+ OPJ_INT32 prci, prcj;
+ comp = &pi->comps[pi->compno];
+ if (pi->resno >= comp->numresolutions) {
+ continue;
+ }
+ res = &comp->resolutions[pi->resno];
+ levelno = comp->numresolutions - 1 - pi->resno;
+ trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+ try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+ trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+ try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+ rpx = res->pdx + levelno;
+ rpy = res->pdy + levelno;
+ if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+ continue;
+ }
+ if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+ continue;
+ }
+
+ if ((res->pw==0)||(res->ph==0)) continue;
+
+ if ((trx0==trx1)||(try0==try1)) continue;
+
+ prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+ - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+ prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+ - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+ pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
+ for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
+ index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+ if (!pi->include[index]) {
+ pi->include[index] = 1;
+ return OPJ_TRUE;
+ }
+LABEL_SKIP:;
+ }
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
+ opj_pi_comp_t *comp = NULL;
+ opj_pi_resolution_t *res = NULL;
+ OPJ_UINT32 index = 0;
+
+ if (!pi->first) {
+ comp = &pi->comps[pi->compno];
+ goto LABEL_SKIP;
+ } else {
+ OPJ_UINT32 compno, resno;
+ pi->first = 0;
+ pi->dx = 0;
+ pi->dy = 0;
+ for (compno = 0; compno < pi->numcomps; compno++) {
+ comp = &pi->comps[compno];
+ for (resno = 0; resno < comp->numresolutions; resno++) {
+ OPJ_UINT32 dx, dy;
+ res = &comp->resolutions[resno];
+ dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+ dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+ pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+ pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
+ }
+ }
+ }
+ if (!pi->tp_on){
+ pi->poc.ty0 = pi->ty0;
+ pi->poc.tx0 = pi->tx0;
+ pi->poc.ty1 = pi->ty1;
+ pi->poc.tx1 = pi->tx1;
+ }
+ for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+ for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
+ for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+ comp = &pi->comps[pi->compno];
+ for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
+ OPJ_UINT32 levelno;
+ OPJ_INT32 trx0, try0;
+ OPJ_INT32 trx1, try1;
+ OPJ_UINT32 rpx, rpy;
+ OPJ_INT32 prci, prcj;
+ res = &comp->resolutions[pi->resno];
+ levelno = comp->numresolutions - 1 - pi->resno;
+ trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+ try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+ trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+ try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+ rpx = res->pdx + levelno;
+ rpy = res->pdy + levelno;
+ if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+ continue;
+ }
+ if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+ continue;
+ }
+
+ if ((res->pw==0)||(res->ph==0)) continue;
+
+ if ((trx0==trx1)||(try0==try1)) continue;
+
+ prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+ - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+ prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+ - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+ pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
+ for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
+ index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+ if (!pi->include[index]) {
+ pi->include[index] = 1;
+ return OPJ_TRUE;
+ }
+LABEL_SKIP:;
+ }
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
+ opj_pi_comp_t *comp = NULL;
+ opj_pi_resolution_t *res = NULL;
+ OPJ_UINT32 index = 0;
+
+ if (!pi->first) {
+ comp = &pi->comps[pi->compno];
+ goto LABEL_SKIP;
+ } else {
+ pi->first = 0;
+ }
+
+ for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
+ OPJ_UINT32 resno;
+ comp = &pi->comps[pi->compno];
+ pi->dx = 0;
+ pi->dy = 0;
+ for (resno = 0; resno < comp->numresolutions; resno++) {
+ OPJ_UINT32 dx, dy;
+ res = &comp->resolutions[resno];
+ dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+ dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+ pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+ pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
+ }
+ if (!pi->tp_on){
+ pi->poc.ty0 = pi->ty0;
+ pi->poc.tx0 = pi->tx0;
+ pi->poc.ty1 = pi->ty1;
+ pi->poc.tx1 = pi->tx1;
+ }
+ for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+ for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
+ for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
+ OPJ_UINT32 levelno;
+ OPJ_INT32 trx0, try0;
+ OPJ_INT32 trx1, try1;
+ OPJ_UINT32 rpx, rpy;
+ OPJ_INT32 prci, prcj;
+ res = &comp->resolutions[pi->resno];
+ levelno = comp->numresolutions - 1 - pi->resno;
+ trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+ try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+ trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+ try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+ rpx = res->pdx + levelno;
+ rpy = res->pdy + levelno;
+ if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+ continue;
+ }
+ if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+ continue;
+ }
+
+ if ((res->pw==0)||(res->ph==0)) continue;
+
+ if ((trx0==trx1)||(try0==try1)) continue;
+
+ prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+ - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+ prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+ - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+ pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
+ for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
+ index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
+ if (!pi->include[index]) {
+ pi->include[index] = 1;
+ return OPJ_TRUE;
+ }
+LABEL_SKIP:;
+ }
+ }
+ }
+ }
+ }
+
+ return OPJ_FALSE;
+}
+
+void opj_get_encoding_parameters( const opj_image_t *p_image,
+ const opj_cp_t *p_cp,
+ OPJ_UINT32 p_tileno,
+ OPJ_INT32 * p_tx0,
+ OPJ_INT32 * p_tx1,
+ OPJ_INT32 * p_ty0,
+ OPJ_INT32 * p_ty1,
+ OPJ_UINT32 * p_dx_min,
+ OPJ_UINT32 * p_dy_min,
+ OPJ_UINT32 * p_max_prec,
+ OPJ_UINT32 * p_max_res )
+{
+ /* loop */
+ OPJ_UINT32 compno, resno;
+ /* pointers */
+ const opj_tcp_t *l_tcp = 00;
+ const opj_tccp_t * l_tccp = 00;
+ const opj_image_comp_t * l_img_comp = 00;
+
+ /* position in x and y of tile */
+ OPJ_UINT32 p, q;
+
+ /* preconditions */
+ assert(p_cp != 00);
+ assert(p_image != 00);
+ assert(p_tileno < p_cp->tw * p_cp->th);
+
+ /* initializations */
+ l_tcp = &p_cp->tcps [p_tileno];
+ l_img_comp = p_image->comps;
+ l_tccp = l_tcp->tccps;
+
+ /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
+ p = p_tileno % p_cp->tw;
+ q = p_tileno / p_cp->tw;
+
+ /* find extent of tile */
+ *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
+ *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
+ *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
+ *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
+
+ /* max precision is 0 (can only grow) */
+ *p_max_prec = 0;
+ *p_max_res = 0;
+
+ /* take the largest value for dx_min and dy_min */
+ *p_dx_min = 0x7fffffff;
+ *p_dy_min = 0x7fffffff;
+
+ for (compno = 0; compno < p_image->numcomps; ++compno) {
+ /* arithmetic variables to calculate */
+ OPJ_UINT32 l_level_no;
+ OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
+ OPJ_INT32 l_px0, l_py0, l_px1, py1;
+ OPJ_UINT32 l_pdx, l_pdy;
+ OPJ_UINT32 l_pw, l_ph;
+ OPJ_UINT32 l_product;
+ OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
+
+ l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
+ l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
+ l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
+ l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
+
+ if (l_tccp->numresolutions > *p_max_res) {
+ *p_max_res = l_tccp->numresolutions;
+ }
+
+ /* use custom size for precincts */
+ for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
+ OPJ_UINT32 l_dx, l_dy;
+
+ /* precinct width and height */
+ l_pdx = l_tccp->prcw[resno];
+ l_pdy = l_tccp->prch[resno];
+
+ l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
+ l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
+
+ /* take the minimum size for dx for each comp and resolution */
+ *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
+ *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
+
+ /* various calculations of extents */
+ l_level_no = l_tccp->numresolutions - 1 - resno;
+
+ l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
+ l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
+ l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
+ l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
+
+ l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
+ l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
+ l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
+
+ py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
+
+ l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
+ l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
+
+ l_product = l_pw * l_ph;
+
+ /* update precision */
+ if (l_product > *p_max_prec) {
+ *p_max_prec = l_product;
+ }
+ }
+ ++l_img_comp;
+ ++l_tccp;
+ }
+}
+
+
+void opj_get_all_encoding_parameters( const opj_image_t *p_image,
+ const opj_cp_t *p_cp,
+ OPJ_UINT32 tileno,
+ OPJ_INT32 * p_tx0,
+ OPJ_INT32 * p_tx1,
+ OPJ_INT32 * p_ty0,
+ OPJ_INT32 * p_ty1,
+ OPJ_UINT32 * p_dx_min,
+ OPJ_UINT32 * p_dy_min,
+ OPJ_UINT32 * p_max_prec,
+ OPJ_UINT32 * p_max_res,
+ OPJ_UINT32 ** p_resolutions )
+{
+ /* loop*/
+ OPJ_UINT32 compno, resno;
+
+ /* pointers*/
+ const opj_tcp_t *tcp = 00;
+ const opj_tccp_t * l_tccp = 00;
+ const opj_image_comp_t * l_img_comp = 00;
+
+ /* to store l_dx, l_dy, w and h for each resolution and component.*/
+ OPJ_UINT32 * lResolutionPtr;
+
+ /* position in x and y of tile*/
+ OPJ_UINT32 p, q;
+
+ /* preconditions in debug*/
+ assert(p_cp != 00);
+ assert(p_image != 00);
+ assert(tileno < p_cp->tw * p_cp->th);
+
+ /* initializations*/
+ tcp = &p_cp->tcps [tileno];
+ l_tccp = tcp->tccps;
+ l_img_comp = p_image->comps;
+
+ /* position in x and y of tile*/
+ p = tileno % p_cp->tw;
+ q = tileno / p_cp->tw;
+
+ /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
+ *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
+ *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
+ *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
+ *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
+
+ /* max precision and resolution is 0 (can only grow)*/
+ *p_max_prec = 0;
+ *p_max_res = 0;
+
+ /* take the largest value for dx_min and dy_min*/
+ *p_dx_min = 0x7fffffff;
+ *p_dy_min = 0x7fffffff;
+
+ for (compno = 0; compno < p_image->numcomps; ++compno) {
+ /* aritmetic variables to calculate*/
+ OPJ_UINT32 l_level_no;
+ OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
+ OPJ_INT32 l_px0, l_py0, l_px1, py1;
+ OPJ_UINT32 l_product;
+ OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
+ OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
+
+ lResolutionPtr = p_resolutions[compno];
+
+ l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
+ l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
+ l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
+ l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
+
+ if (l_tccp->numresolutions > *p_max_res) {
+ *p_max_res = l_tccp->numresolutions;
+ }
+
+ /* use custom size for precincts*/
+ l_level_no = l_tccp->numresolutions - 1;
+ for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
+ OPJ_UINT32 l_dx, l_dy;
+
+ /* precinct width and height*/
+ l_pdx = l_tccp->prcw[resno];
+ l_pdy = l_tccp->prch[resno];
+ *lResolutionPtr++ = l_pdx;
+ *lResolutionPtr++ = l_pdy;
+ l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
+ l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
+ /* take the minimum size for l_dx for each comp and resolution*/
+ *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dx_min, (OPJ_INT32)l_dx);
+ *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dy_min, (OPJ_INT32)l_dy);
+
+ /* various calculations of extents*/
+ l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
+ l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
+ l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
+ l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
+ l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
+ l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
+ l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
+ py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
+ l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
+ l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
+ *lResolutionPtr++ = l_pw;
+ *lResolutionPtr++ = l_ph;
+ l_product = l_pw * l_ph;
+
+ /* update precision*/
+ if (l_product > *p_max_prec) {
+ *p_max_prec = l_product;
+ }
+
+ --l_level_no;
+ }
+ ++l_tccp;
+ ++l_img_comp;
+ }
+}
+
+opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
+ const opj_cp_t *cp,
+ OPJ_UINT32 tileno )
+{
+ /* loop*/
+ OPJ_UINT32 pino, compno;
+ /* number of poc in the p_pi*/
+ OPJ_UINT32 l_poc_bound;
+
+ /* pointers to tile coding parameters and components.*/
+ opj_pi_iterator_t *l_pi = 00;
+ opj_tcp_t *tcp = 00;
+ const opj_tccp_t *tccp = 00;
+
+ /* current packet iterator being allocated*/
+ opj_pi_iterator_t *l_current_pi = 00;
+
+ /* preconditions in debug*/
+ assert(cp != 00);
+ assert(image != 00);
+ assert(tileno < cp->tw * cp->th);
+
+ /* initializations*/
+ tcp = &cp->tcps[tileno];
+ l_poc_bound = tcp->numpocs+1;
+
+ /* memory allocations*/
+ l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
+ if (!l_pi) {
+ return NULL;
+ }
+ memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
+
+ l_current_pi = l_pi;
+ for (pino = 0; pino < l_poc_bound ; ++pino) {
+
+ l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
+ if (! l_current_pi->comps) {
+ opj_pi_destroy(l_pi, l_poc_bound);
+ return NULL;
+ }
+
+ l_current_pi->numcomps = image->numcomps;
+ memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
+
+ for (compno = 0; compno < image->numcomps; ++compno) {
+ opj_pi_comp_t *comp = &l_current_pi->comps[compno];
+
+ tccp = &tcp->tccps[compno];
+
+ comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
+ if (!comp->resolutions) {
+ opj_pi_destroy(l_pi, l_poc_bound);
+ return 00;
+ }
+
+ comp->numresolutions = tccp->numresolutions;
+ memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
+ }
+ ++l_current_pi;
+ }
+ return l_pi;
+}
+
+void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
+ OPJ_UINT32 p_tileno,
+ OPJ_INT32 p_tx0,
+ OPJ_INT32 p_tx1,
+ OPJ_INT32 p_ty0,
+ OPJ_INT32 p_ty1,
+ OPJ_UINT32 p_max_prec,
+ OPJ_UINT32 p_max_res,
+ OPJ_UINT32 p_dx_min,
+ OPJ_UINT32 p_dy_min)
+{
+ /* loop*/
+ OPJ_UINT32 pino;
+ /* tile coding parameter*/
+ opj_tcp_t *l_tcp = 00;
+ /* current poc being updated*/
+ opj_poc_t * l_current_poc = 00;
+
+ /* number of pocs*/
+ OPJ_UINT32 l_poc_bound;
+
+ OPJ_ARG_NOT_USED(p_max_res);
+
+ /* preconditions in debug*/
+ assert(p_cp != 00);
+ assert(p_tileno < p_cp->tw * p_cp->th);
+
+ /* initializations*/
+ l_tcp = &p_cp->tcps [p_tileno];
+ /* number of iterations in the loop */
+ l_poc_bound = l_tcp->numpocs+1;
+
+ /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
+ store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
+ l_current_poc = l_tcp->pocs;
+
+ l_current_poc->compS = l_current_poc->compno0;
+ l_current_poc->compE = l_current_poc->compno1;
+ l_current_poc->resS = l_current_poc->resno0;
+ l_current_poc->resE = l_current_poc->resno1;
+ l_current_poc->layE = l_current_poc->layno1;
+
+ /* special treatment for the first element*/
+ l_current_poc->layS = 0;
+ l_current_poc->prg = l_current_poc->prg1;
+ l_current_poc->prcS = 0;
+
+ l_current_poc->prcE = p_max_prec;
+ l_current_poc->txS = (OPJ_UINT32)p_tx0;
+ l_current_poc->txE = (OPJ_UINT32)p_tx1;
+ l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+ l_current_poc->tyE = (OPJ_UINT32)p_ty1;
+ l_current_poc->dx = p_dx_min;
+ l_current_poc->dy = p_dy_min;
+
+ ++ l_current_poc;
+ for (pino = 1;pino < l_poc_bound ; ++pino) {
+ l_current_poc->compS = l_current_poc->compno0;
+ l_current_poc->compE= l_current_poc->compno1;
+ l_current_poc->resS = l_current_poc->resno0;
+ l_current_poc->resE = l_current_poc->resno1;
+ l_current_poc->layE = l_current_poc->layno1;
+ l_current_poc->prg = l_current_poc->prg1;
+ l_current_poc->prcS = 0;
+ /* special treatment here different from the first element*/
+ l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
+
+ l_current_poc->prcE = p_max_prec;
+ l_current_poc->txS = (OPJ_UINT32)p_tx0;
+ l_current_poc->txE = (OPJ_UINT32)p_tx1;
+ l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+ l_current_poc->tyE = (OPJ_UINT32)p_ty1;
+ l_current_poc->dx = p_dx_min;
+ l_current_poc->dy = p_dy_min;
+ ++ l_current_poc;
+ }
+}
+
+void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
+ OPJ_UINT32 p_num_comps,
+ OPJ_UINT32 p_tileno,
+ OPJ_INT32 p_tx0,
+ OPJ_INT32 p_tx1,
+ OPJ_INT32 p_ty0,
+ OPJ_INT32 p_ty1,
+ OPJ_UINT32 p_max_prec,
+ OPJ_UINT32 p_max_res,
+ OPJ_UINT32 p_dx_min,
+ OPJ_UINT32 p_dy_min)
+{
+ /* loop*/
+ OPJ_UINT32 pino;
+ /* tile coding parameter*/
+ opj_tcp_t *l_tcp = 00;
+ /* current poc being updated*/
+ opj_poc_t * l_current_poc = 00;
+ /* number of pocs*/
+ OPJ_UINT32 l_poc_bound;
+
+ /* preconditions in debug*/
+ assert(p_cp != 00);
+ assert(p_tileno < p_cp->tw * p_cp->th);
+
+ /* initializations*/
+ l_tcp = &p_cp->tcps [p_tileno];
+
+ /* number of iterations in the loop */
+ l_poc_bound = l_tcp->numpocs+1;
+
+ /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
+ store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
+ l_current_poc = l_tcp->pocs;
+
+ for (pino = 0; pino < l_poc_bound ; ++pino) {
+ l_current_poc->compS = 0;
+ l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
+ l_current_poc->resS = 0;
+ l_current_poc->resE = p_max_res;
+ l_current_poc->layS = 0;
+ l_current_poc->layE = l_tcp->numlayers;
+ l_current_poc->prg = l_tcp->prg;
+ l_current_poc->prcS = 0;
+ l_current_poc->prcE = p_max_prec;
+ l_current_poc->txS = (OPJ_UINT32)p_tx0;
+ l_current_poc->txE = (OPJ_UINT32)p_tx1;
+ l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+ l_current_poc->tyE = (OPJ_UINT32)p_ty1;
+ l_current_poc->dx = p_dx_min;
+ l_current_poc->dy = p_dy_min;
+ ++ l_current_poc;
+ }
+}
+
+void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
+ opj_tcp_t * p_tcp,
+ OPJ_UINT32 p_max_precision,
+ OPJ_UINT32 p_max_res)
+{
+ /* loop*/
+ OPJ_UINT32 pino;
+
+ /* encoding prameters to set*/
+ OPJ_UINT32 l_bound;
+
+ opj_pi_iterator_t * l_current_pi = 00;
+ opj_poc_t* l_current_poc = 0;
+
+ OPJ_ARG_NOT_USED(p_max_res);
+
+ /* preconditions in debug*/
+ assert(p_pi != 00);
+ assert(p_tcp != 00);
+
+ /* initializations*/
+ l_bound = p_tcp->numpocs+1;
+ l_current_pi = p_pi;
+ l_current_poc = p_tcp->pocs;
+
+ for (pino = 0;pino<l_bound;++pino) {
+ l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
+ l_current_pi->first = 1;
+
+ l_current_pi->poc.resno0 = l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
+ l_current_pi->poc.compno0 = l_current_poc->compno0; /* Component Index #0 (Start) */
+ l_current_pi->poc.layno0 = 0;
+ l_current_pi->poc.precno0 = 0;
+ l_current_pi->poc.resno1 = l_current_poc->resno1; /* Resolution Level Index #0 (End) */
+ l_current_pi->poc.compno1 = l_current_poc->compno1; /* Component Index #0 (End) */
+ l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
+ l_current_pi->poc.precno1 = p_max_precision;
+ ++l_current_pi;
+ ++l_current_poc;
+ }
+}
+
+void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
+ opj_tcp_t * p_tcp,
+ OPJ_UINT32 p_max_precision,
+ OPJ_UINT32 p_max_res)
+{
+ /* loop*/
+ OPJ_UINT32 pino;
+
+ /* encoding prameters to set*/
+ OPJ_UINT32 l_bound;
+
+ opj_pi_iterator_t * l_current_pi = 00;
+ /* preconditions in debug*/
+ assert(p_tcp != 00);
+ assert(p_pi != 00);
+
+ /* initializations*/
+ l_bound = p_tcp->numpocs+1;
+ l_current_pi = p_pi;
+
+ for (pino = 0;pino<l_bound;++pino) {
+ l_current_pi->poc.prg = p_tcp->prg;
+ l_current_pi->first = 1;
+ l_current_pi->poc.resno0 = 0;
+ l_current_pi->poc.compno0 = 0;
+ l_current_pi->poc.layno0 = 0;
+ l_current_pi->poc.precno0 = 0;
+ l_current_pi->poc.resno1 = p_max_res;
+ l_current_pi->poc.compno1 = l_current_pi->numcomps;
+ l_current_pi->poc.layno1 = p_tcp->numlayers;
+ l_current_pi->poc.precno1 = p_max_precision;
+ ++l_current_pi;
+ }
+}
+
+
+
+OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
+ opj_cp_t *cp,
+ OPJ_UINT32 tileno,
+ OPJ_UINT32 pino,
+ const OPJ_CHAR *prog)
+{
+ OPJ_INT32 i;
+ opj_tcp_t *tcps =&cp->tcps[tileno];
+ opj_poc_t *tcp = &tcps->pocs[pino];
+
+ if(pos>=0){
+ for(i=pos;pos>=0;i--){
+ switch(prog[i]){
+ case 'R':
+ if(tcp->res_t==tcp->resE){
+ if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
+ return OPJ_TRUE;
+ }else{
+ return OPJ_FALSE;
+ }
+ }else{
+ return OPJ_TRUE;
+ }
+ break;
+ case 'C':
+ if(tcp->comp_t==tcp->compE){
+ if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
+ return OPJ_TRUE;
+ }else{
+ return OPJ_FALSE;
+ }
+ }else{
+ return OPJ_TRUE;
+ }
+ break;
+ case 'L':
+ if(tcp->lay_t==tcp->layE){
+ if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
+ return OPJ_TRUE;
+ }else{
+ return OPJ_FALSE;
+ }
+ }else{
+ return OPJ_TRUE;
+ }
+ break;
+ case 'P':
+ switch(tcp->prg){
+ case OPJ_LRCP||OPJ_RLCP:
+ if(tcp->prc_t == tcp->prcE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ return OPJ_TRUE;
+ }else{
+ return OPJ_FALSE;
+ }
+ }else{
+ return OPJ_TRUE;
+ }
+ break;
+ default:
+ if(tcp->tx0_t == tcp->txE){
+ /*TY*/
+ if(tcp->ty0_t == tcp->tyE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ return OPJ_TRUE;
+ }else{
+ return OPJ_FALSE;
+ }
+ }else{
+ return OPJ_TRUE;
+ }/*TY*/
+ }else{
+ return OPJ_TRUE;
+ }
+ break;
+ }/*end case P*/
+ }/*end switch*/
+ }/*end for*/
+ }/*end if*/
+ return OPJ_FALSE;
+}
+
+
+/*
+==========================================================
+ Packet iterator interface
+==========================================================
+*/
+opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
+ opj_cp_t *p_cp,
+ OPJ_UINT32 p_tile_no)
+{
+ /* loop */
+ OPJ_UINT32 pino;
+ OPJ_UINT32 compno, resno;
+
+ /* to store w, h, dx and dy fro all components and resolutions */
+ OPJ_UINT32 * l_tmp_data;
+ OPJ_UINT32 ** l_tmp_ptr;
+
+ /* encoding prameters to set */
+ OPJ_UINT32 l_max_res;
+ OPJ_UINT32 l_max_prec;
+ OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
+ OPJ_UINT32 l_dx_min,l_dy_min;
+ OPJ_UINT32 l_bound;
+ OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
+ OPJ_UINT32 l_data_stride;
+
+ /* pointers */
+ opj_pi_iterator_t *l_pi = 00;
+ opj_tcp_t *l_tcp = 00;
+ const opj_tccp_t *l_tccp = 00;
+ opj_pi_comp_t *l_current_comp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_pi_iterator_t * l_current_pi = 00;
+ OPJ_UINT32 * l_encoding_value_ptr = 00;
+
+ /* preconditions in debug */
+ assert(p_cp != 00);
+ assert(p_image != 00);
+ assert(p_tile_no < p_cp->tw * p_cp->th);
+
+ /* initializations */
+ l_tcp = &p_cp->tcps[p_tile_no];
+ l_bound = l_tcp->numpocs+1;
+
+ l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
+ l_tmp_data = (OPJ_UINT32*)opj_malloc(
+ l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
+ if
+ (! l_tmp_data)
+ {
+ return 00;
+ }
+ l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
+ p_image->numcomps * sizeof(OPJ_UINT32 *));
+ if
+ (! l_tmp_ptr)
+ {
+ opj_free(l_tmp_data);
+ return 00;
+ }
+
+ /* memory allocation for pi */
+ l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
+ if (!l_pi) {
+ opj_free(l_tmp_data);
+ opj_free(l_tmp_ptr);
+ return 00;
+ }
+
+ l_encoding_value_ptr = l_tmp_data;
+ /* update pointer array */
+ for
+ (compno = 0; compno < p_image->numcomps; ++compno)
+ {
+ l_tmp_ptr[compno] = l_encoding_value_ptr;
+ l_encoding_value_ptr += l_data_stride;
+ }
+ /* get encoding parameters */
+ opj_get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
+
+ /* step calculations */
+ l_step_p = 1;
+ l_step_c = l_max_prec * l_step_p;
+ l_step_r = p_image->numcomps * l_step_c;
+ l_step_l = l_max_res * l_step_r;
+
+ /* set values for first packet iterator */
+ l_current_pi = l_pi;
+
+ /* memory allocation for include */
+ l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
+ if
+ (!l_current_pi->include)
+ {
+ opj_free(l_tmp_data);
+ opj_free(l_tmp_ptr);
+ opj_pi_destroy(l_pi, l_bound);
+ return 00;
+ }
+ memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
+
+ /* special treatment for the first packet iterator */
+ l_current_comp = l_current_pi->comps;
+ l_img_comp = p_image->comps;
+ l_tccp = l_tcp->tccps;
+
+ l_current_pi->tx0 = l_tx0;
+ l_current_pi->ty0 = l_ty0;
+ l_current_pi->tx1 = l_tx1;
+ l_current_pi->ty1 = l_ty1;
+
+ /*l_current_pi->dx = l_img_comp->dx;*/
+ /*l_current_pi->dy = l_img_comp->dy;*/
+
+ l_current_pi->step_p = l_step_p;
+ l_current_pi->step_c = l_step_c;
+ l_current_pi->step_r = l_step_r;
+ l_current_pi->step_l = l_step_l;
+
+ /* allocation for components and number of components has already been calculated by opj_pi_create */
+ for
+ (compno = 0; compno < l_current_pi->numcomps; ++compno)
+ {
+ opj_pi_resolution_t *l_res = l_current_comp->resolutions;
+ l_encoding_value_ptr = l_tmp_ptr[compno];
+
+ l_current_comp->dx = l_img_comp->dx;
+ l_current_comp->dy = l_img_comp->dy;
+ /* resolutions have already been initialized */
+ for
+ (resno = 0; resno < l_current_comp->numresolutions; resno++)
+ {
+ l_res->pdx = *(l_encoding_value_ptr++);
+ l_res->pdy = *(l_encoding_value_ptr++);
+ l_res->pw = *(l_encoding_value_ptr++);
+ l_res->ph = *(l_encoding_value_ptr++);
+ ++l_res;
+ }
+ ++l_current_comp;
+ ++l_img_comp;
+ ++l_tccp;
+ }
+ ++l_current_pi;
+
+ for (pino = 1 ; pino<l_bound ; ++pino )
+ {
+ l_current_comp = l_current_pi->comps;
+ l_img_comp = p_image->comps;
+ l_tccp = l_tcp->tccps;
+
+ l_current_pi->tx0 = l_tx0;
+ l_current_pi->ty0 = l_ty0;
+ l_current_pi->tx1 = l_tx1;
+ l_current_pi->ty1 = l_ty1;
+ /*l_current_pi->dx = l_dx_min;*/
+ /*l_current_pi->dy = l_dy_min;*/
+ l_current_pi->step_p = l_step_p;
+ l_current_pi->step_c = l_step_c;
+ l_current_pi->step_r = l_step_r;
+ l_current_pi->step_l = l_step_l;
+
+ /* allocation for components and number of components has already been calculated by opj_pi_create */
+ for
+ (compno = 0; compno < l_current_pi->numcomps; ++compno)
+ {
+ opj_pi_resolution_t *l_res = l_current_comp->resolutions;
+ l_encoding_value_ptr = l_tmp_ptr[compno];
+
+ l_current_comp->dx = l_img_comp->dx;
+ l_current_comp->dy = l_img_comp->dy;
+ /* resolutions have already been initialized */
+ for
+ (resno = 0; resno < l_current_comp->numresolutions; resno++)
+ {
+ l_res->pdx = *(l_encoding_value_ptr++);
+ l_res->pdy = *(l_encoding_value_ptr++);
+ l_res->pw = *(l_encoding_value_ptr++);
+ l_res->ph = *(l_encoding_value_ptr++);
+ ++l_res;
+ }
+ ++l_current_comp;
+ ++l_img_comp;
+ ++l_tccp;
+ }
+ /* special treatment*/
+ l_current_pi->include = (l_current_pi-1)->include;
+ ++l_current_pi;
+ }
+ opj_free(l_tmp_data);
+ l_tmp_data = 00;
+ opj_free(l_tmp_ptr);
+ l_tmp_ptr = 00;
+ if
+ (l_tcp->POC)
+ {
+ opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
+ }
+ else
+ {
+ opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
+ }
+ return l_pi;
+}
+
+
+
+opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
+ opj_cp_t *p_cp,
+ OPJ_UINT32 p_tile_no,
+ J2K_T2_MODE p_t2_mode )
+{
+ /* loop*/
+ OPJ_UINT32 pino;
+ OPJ_UINT32 compno, resno;
+
+ /* to store w, h, dx and dy fro all components and resolutions*/
+ OPJ_UINT32 * l_tmp_data;
+ OPJ_UINT32 ** l_tmp_ptr;
+
+ /* encoding prameters to set*/
+ OPJ_UINT32 l_max_res;
+ OPJ_UINT32 l_max_prec;
+ OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
+ OPJ_UINT32 l_dx_min,l_dy_min;
+ OPJ_UINT32 l_bound;
+ OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
+ OPJ_UINT32 l_data_stride;
+
+ /* pointers*/
+ opj_pi_iterator_t *l_pi = 00;
+ opj_tcp_t *l_tcp = 00;
+ const opj_tccp_t *l_tccp = 00;
+ opj_pi_comp_t *l_current_comp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_pi_iterator_t * l_current_pi = 00;
+ OPJ_UINT32 * l_encoding_value_ptr = 00;
+
+ /* preconditions in debug*/
+ assert(p_cp != 00);
+ assert(p_image != 00);
+ assert(p_tile_no < p_cp->tw * p_cp->th);
+
+ /* initializations*/
+ l_tcp = &p_cp->tcps[p_tile_no];
+ l_bound = l_tcp->numpocs+1;
+
+ l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
+ l_tmp_data = (OPJ_UINT32*)opj_malloc(
+ l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
+ if (! l_tmp_data) {
+ return 00;
+ }
+
+ l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
+ p_image->numcomps * sizeof(OPJ_UINT32 *));
+ if (! l_tmp_ptr) {
+ opj_free(l_tmp_data);
+ return 00;
+ }
+
+ /* memory allocation for pi*/
+ l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
+ if (!l_pi) {
+ opj_free(l_tmp_data);
+ opj_free(l_tmp_ptr);
+ return 00;
+ }
+
+ l_encoding_value_ptr = l_tmp_data;
+ /* update pointer array*/
+ for (compno = 0; compno < p_image->numcomps; ++compno) {
+ l_tmp_ptr[compno] = l_encoding_value_ptr;
+ l_encoding_value_ptr += l_data_stride;
+ }
+
+ /* get encoding parameters*/
+ opj_get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
+
+ /* step calculations*/
+ l_step_p = 1;
+ l_step_c = l_max_prec * l_step_p;
+ l_step_r = p_image->numcomps * l_step_c;
+ l_step_l = l_max_res * l_step_r;
+
+ /* set values for first packet iterator*/
+ l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
+ l_current_pi = l_pi;
+
+ /* memory allocation for include*/
+ l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
+ if (!l_current_pi->include) {
+ opj_free(l_tmp_data);
+ opj_free(l_tmp_ptr);
+ opj_pi_destroy(l_pi, l_bound);
+ return 00;
+ }
+ memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
+
+ /* special treatment for the first packet iterator*/
+ l_current_comp = l_current_pi->comps;
+ l_img_comp = p_image->comps;
+ l_tccp = l_tcp->tccps;
+ l_current_pi->tx0 = l_tx0;
+ l_current_pi->ty0 = l_ty0;
+ l_current_pi->tx1 = l_tx1;
+ l_current_pi->ty1 = l_ty1;
+ l_current_pi->dx = l_dx_min;
+ l_current_pi->dy = l_dy_min;
+ l_current_pi->step_p = l_step_p;
+ l_current_pi->step_c = l_step_c;
+ l_current_pi->step_r = l_step_r;
+ l_current_pi->step_l = l_step_l;
+
+ /* allocation for components and number of components has already been calculated by opj_pi_create */
+ for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
+ opj_pi_resolution_t *l_res = l_current_comp->resolutions;
+ l_encoding_value_ptr = l_tmp_ptr[compno];
+
+ l_current_comp->dx = l_img_comp->dx;
+ l_current_comp->dy = l_img_comp->dy;
+
+ /* resolutions have already been initialized */
+ for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
+ l_res->pdx = *(l_encoding_value_ptr++);
+ l_res->pdy = *(l_encoding_value_ptr++);
+ l_res->pw = *(l_encoding_value_ptr++);
+ l_res->ph = *(l_encoding_value_ptr++);
+ ++l_res;
+ }
+
+ ++l_current_comp;
+ ++l_img_comp;
+ ++l_tccp;
+ }
+ ++l_current_pi;
+
+ for (pino = 1 ; pino<l_bound ; ++pino ) {
+ l_current_comp = l_current_pi->comps;
+ l_img_comp = p_image->comps;
+ l_tccp = l_tcp->tccps;
+
+ l_current_pi->tx0 = l_tx0;
+ l_current_pi->ty0 = l_ty0;
+ l_current_pi->tx1 = l_tx1;
+ l_current_pi->ty1 = l_ty1;
+ l_current_pi->dx = l_dx_min;
+ l_current_pi->dy = l_dy_min;
+ l_current_pi->step_p = l_step_p;
+ l_current_pi->step_c = l_step_c;
+ l_current_pi->step_r = l_step_r;
+ l_current_pi->step_l = l_step_l;
+
+ /* allocation for components and number of components has already been calculated by opj_pi_create */
+ for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
+ opj_pi_resolution_t *l_res = l_current_comp->resolutions;
+ l_encoding_value_ptr = l_tmp_ptr[compno];
+
+ l_current_comp->dx = l_img_comp->dx;
+ l_current_comp->dy = l_img_comp->dy;
+ /* resolutions have already been initialized */
+ for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
+ l_res->pdx = *(l_encoding_value_ptr++);
+ l_res->pdy = *(l_encoding_value_ptr++);
+ l_res->pw = *(l_encoding_value_ptr++);
+ l_res->ph = *(l_encoding_value_ptr++);
+ ++l_res;
+ }
+ ++l_current_comp;
+ ++l_img_comp;
+ ++l_tccp;
+ }
+
+ /* special treatment*/
+ l_current_pi->include = (l_current_pi-1)->include;
+ ++l_current_pi;
+ }
+
+ opj_free(l_tmp_data);
+ l_tmp_data = 00;
+ opj_free(l_tmp_ptr);
+ l_tmp_ptr = 00;
+
+ if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
+ opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
+ }
+ else {
+ opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
+ }
+
+ return l_pi;
+}
+
+void opj_pi_create_encode( opj_pi_iterator_t *pi,
+ opj_cp_t *cp,
+ OPJ_UINT32 tileno,
+ OPJ_UINT32 pino,
+ OPJ_UINT32 tpnum,
+ OPJ_INT32 tppos,
+ J2K_T2_MODE t2_mode)
+{
+ const OPJ_CHAR *prog;
+ OPJ_INT32 i;
+ OPJ_UINT32 incr_top=1,resetX=0;
+ opj_tcp_t *tcps =&cp->tcps[tileno];
+ opj_poc_t *tcp= &tcps->pocs[pino];
+
+ prog = opj_j2k_convert_progression_order(tcp->prg);
+
+ pi[pino].first = 1;
+ pi[pino].poc.prg = tcp->prg;
+
+ if(!(cp->m_specific_param.m_enc.m_tp_on && ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
+ pi[pino].poc.resno0 = tcp->resS;
+ pi[pino].poc.resno1 = tcp->resE;
+ pi[pino].poc.compno0 = tcp->compS;
+ pi[pino].poc.compno1 = tcp->compE;
+ pi[pino].poc.layno0 = tcp->layS;
+ pi[pino].poc.layno1 = tcp->layE;
+ pi[pino].poc.precno0 = tcp->prcS;
+ pi[pino].poc.precno1 = tcp->prcE;
+ pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
+ pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
+ pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
+ pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
+ }else {
+ for(i=tppos+1;i<4;i++){
+ switch(prog[i]){
+ case 'R':
+ pi[pino].poc.resno0 = tcp->resS;
+ pi[pino].poc.resno1 = tcp->resE;
+ break;
+ case 'C':
+ pi[pino].poc.compno0 = tcp->compS;
+ pi[pino].poc.compno1 = tcp->compE;
+ break;
+ case 'L':
+ pi[pino].poc.layno0 = tcp->layS;
+ pi[pino].poc.layno1 = tcp->layE;
+ break;
+ case 'P':
+ switch(tcp->prg){
+ case OPJ_LRCP:
+ case OPJ_RLCP:
+ pi[pino].poc.precno0 = tcp->prcS;
+ pi[pino].poc.precno1 = tcp->prcE;
+ break;
+ default:
+ pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
+ pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
+ pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
+ pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
+ break;
+ }
+ break;
+ }
+ }
+
+ if(tpnum==0){
+ for(i=tppos;i>=0;i--){
+ switch(prog[i]){
+ case 'C':
+ tcp->comp_t = tcp->compS;
+ pi[pino].poc.compno0 = tcp->comp_t;
+ pi[pino].poc.compno1 = tcp->comp_t+1;
+ tcp->comp_t+=1;
+ break;
+ case 'R':
+ tcp->res_t = tcp->resS;
+ pi[pino].poc.resno0 = tcp->res_t;
+ pi[pino].poc.resno1 = tcp->res_t+1;
+ tcp->res_t+=1;
+ break;
+ case 'L':
+ tcp->lay_t = tcp->layS;
+ pi[pino].poc.layno0 = tcp->lay_t;
+ pi[pino].poc.layno1 = tcp->lay_t+1;
+ tcp->lay_t+=1;
+ break;
+ case 'P':
+ switch(tcp->prg){
+ case OPJ_LRCP:
+ case OPJ_RLCP:
+ tcp->prc_t = tcp->prcS;
+ pi[pino].poc.precno0 = tcp->prc_t;
+ pi[pino].poc.precno1 = tcp->prc_t+1;
+ tcp->prc_t+=1;
+ break;
+ default:
+ tcp->tx0_t = tcp->txS;
+ tcp->ty0_t = tcp->tyS;
+ pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+ pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
+ pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+ pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+ tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
+ tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
+ break;
+ }
+ break;
+ }
+ }
+ incr_top=1;
+ }else{
+ for(i=tppos;i>=0;i--){
+ switch(prog[i]){
+ case 'C':
+ pi[pino].poc.compno0 = tcp->comp_t-1;
+ pi[pino].poc.compno1 = tcp->comp_t;
+ break;
+ case 'R':
+ pi[pino].poc.resno0 = tcp->res_t-1;
+ pi[pino].poc.resno1 = tcp->res_t;
+ break;
+ case 'L':
+ pi[pino].poc.layno0 = tcp->lay_t-1;
+ pi[pino].poc.layno1 = tcp->lay_t;
+ break;
+ case 'P':
+ switch(tcp->prg){
+ case OPJ_LRCP:
+ case OPJ_RLCP:
+ pi[pino].poc.precno0 = tcp->prc_t-1;
+ pi[pino].poc.precno1 = tcp->prc_t;
+ break;
+ default:
+ pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
+ pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
+ pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
+ pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
+ break;
+ }
+ break;
+ }
+ if(incr_top==1){
+ switch(prog[i]){
+ case 'R':
+ if(tcp->res_t==tcp->resE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ tcp->res_t = tcp->resS;
+ pi[pino].poc.resno0 = tcp->res_t;
+ pi[pino].poc.resno1 = tcp->res_t+1;
+ tcp->res_t+=1;
+ incr_top=1;
+ }else{
+ incr_top=0;
+ }
+ }else{
+ pi[pino].poc.resno0 = tcp->res_t;
+ pi[pino].poc.resno1 = tcp->res_t+1;
+ tcp->res_t+=1;
+ incr_top=0;
+ }
+ break;
+ case 'C':
+ if(tcp->comp_t ==tcp->compE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ tcp->comp_t = tcp->compS;
+ pi[pino].poc.compno0 = tcp->comp_t;
+ pi[pino].poc.compno1 = tcp->comp_t+1;
+ tcp->comp_t+=1;
+ incr_top=1;
+ }else{
+ incr_top=0;
+ }
+ }else{
+ pi[pino].poc.compno0 = tcp->comp_t;
+ pi[pino].poc.compno1 = tcp->comp_t+1;
+ tcp->comp_t+=1;
+ incr_top=0;
+ }
+ break;
+ case 'L':
+ if(tcp->lay_t == tcp->layE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ tcp->lay_t = tcp->layS;
+ pi[pino].poc.layno0 = tcp->lay_t;
+ pi[pino].poc.layno1 = tcp->lay_t+1;
+ tcp->lay_t+=1;
+ incr_top=1;
+ }else{
+ incr_top=0;
+ }
+ }else{
+ pi[pino].poc.layno0 = tcp->lay_t;
+ pi[pino].poc.layno1 = tcp->lay_t+1;
+ tcp->lay_t+=1;
+ incr_top=0;
+ }
+ break;
+ case 'P':
+ switch(tcp->prg){
+ case OPJ_LRCP:
+ case OPJ_RLCP:
+ if(tcp->prc_t == tcp->prcE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ tcp->prc_t = tcp->prcS;
+ pi[pino].poc.precno0 = tcp->prc_t;
+ pi[pino].poc.precno1 = tcp->prc_t+1;
+ tcp->prc_t+=1;
+ incr_top=1;
+ }else{
+ incr_top=0;
+ }
+ }else{
+ pi[pino].poc.precno0 = tcp->prc_t;
+ pi[pino].poc.precno1 = tcp->prc_t+1;
+ tcp->prc_t+=1;
+ incr_top=0;
+ }
+ break;
+ default:
+ if(tcp->tx0_t >= tcp->txE){
+ if(tcp->ty0_t >= tcp->tyE){
+ if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
+ tcp->ty0_t = tcp->tyS;
+ pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+ pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+ tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
+ incr_top=1;resetX=1;
+ }else{
+ incr_top=0;resetX=0;
+ }
+ }else{
+ pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+ pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+ tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
+ incr_top=0;resetX=1;
+ }
+ if(resetX==1){
+ tcp->tx0_t = tcp->txS;
+ pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+ pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
+ tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
+ }
+ }else{
+ pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+ pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
+ tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
+ incr_top=0;
+ }
+ break;
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+
+void opj_pi_destroy(opj_pi_iterator_t *p_pi,
+ OPJ_UINT32 p_nb_elements)
+{
+ OPJ_UINT32 compno, pino;
+ opj_pi_iterator_t *l_current_pi = p_pi;
+ if (p_pi) {
+ if (p_pi->include) {
+ opj_free(p_pi->include);
+ p_pi->include = 00;
+ }
+ for (pino = 0; pino < p_nb_elements; ++pino){
+ if(l_current_pi->comps) {
+ opj_pi_comp_t *l_current_component = l_current_pi->comps;
+ for (compno = 0; compno < l_current_pi->numcomps; compno++){
+ if(l_current_component->resolutions) {
+ opj_free(l_current_component->resolutions);
+ l_current_component->resolutions = 00;
+ }
+
+ ++l_current_component;
+ }
+ opj_free(l_current_pi->comps);
+ l_current_pi->comps = 0;
+ }
+ ++l_current_pi;
+ }
+ opj_free(p_pi);
+ }
+}
+
+
+
+void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
+ opj_cp_t *p_cp,
+ OPJ_UINT32 p_tile_no )
+{
+ /* encoding parameters to set */
+ OPJ_UINT32 l_max_res;
+ OPJ_UINT32 l_max_prec;
+ OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
+ OPJ_UINT32 l_dx_min,l_dy_min;
+
+ /* pointers */
+ opj_tcp_t *l_tcp = 00;
+
+ /* preconditions */
+ assert(p_cp != 00);
+ assert(p_image != 00);
+ assert(p_tile_no < p_cp->tw * p_cp->th);
+
+ l_tcp = &(p_cp->tcps[p_tile_no]);
+
+ /* get encoding parameters */
+ opj_get_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res);
+
+ if (l_tcp->POC) {
+ opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
+ }
+ else {
+ opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
+ }
+}
+
+OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) {
+ switch (pi->poc.prg) {
+ case OPJ_LRCP:
+ return opj_pi_next_lrcp(pi);
+ case OPJ_RLCP:
+ return opj_pi_next_rlcp(pi);
+ case OPJ_RPCL:
+ return opj_pi_next_rpcl(pi);
+ case OPJ_PCRL:
+ return opj_pi_next_pcrl(pi);
+ case OPJ_CPRL:
+ return opj_pi_next_cprl(pi);
+ case OPJ_PROG_UNKNOWN:
+ return OPJ_FALSE;
+ }
+
+ return OPJ_FALSE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.h
new file mode 100644
index 0000000000..f239679f5b
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.h
@@ -0,0 +1,188 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __PI_H
+#define __PI_H
+/**
+@file pi.h
+@brief Implementation of a packet iterator (PI)
+
+The functions in PI.C have for goal to realize a packet iterator that permits to get the next
+packet following the progression order and change of it. The functions in PI.C are used
+by some function in T2.C.
+*/
+
+/** @defgroup PI PI - Implementation of a packet iterator */
+/*@{*/
+
+/**
+FIXME DOC
+*/
+typedef struct opj_pi_resolution {
+ OPJ_UINT32 pdx, pdy;
+ OPJ_UINT32 pw, ph;
+} opj_pi_resolution_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_pi_comp {
+ OPJ_UINT32 dx, dy;
+ /** number of resolution levels */
+ OPJ_UINT32 numresolutions;
+ opj_pi_resolution_t *resolutions;
+} opj_pi_comp_t;
+
+/**
+Packet iterator
+*/
+typedef struct opj_pi_iterator {
+ /** Enabling Tile part generation*/
+ OPJ_BYTE tp_on;
+ /** precise if the packet has been already used (usefull for progression order change) */
+ OPJ_INT16 *include;
+ /** layer step used to localize the packet in the include vector */
+ OPJ_UINT32 step_l;
+ /** resolution step used to localize the packet in the include vector */
+ OPJ_UINT32 step_r;
+ /** component step used to localize the packet in the include vector */
+ OPJ_UINT32 step_c;
+ /** precinct step used to localize the packet in the include vector */
+ OPJ_UINT32 step_p;
+ /** component that identify the packet */
+ OPJ_UINT32 compno;
+ /** resolution that identify the packet */
+ OPJ_UINT32 resno;
+ /** precinct that identify the packet */
+ OPJ_UINT32 precno;
+ /** layer that identify the packet */
+ OPJ_UINT32 layno;
+ /** 0 if the first packet */
+ OPJ_BOOL first;
+ /** progression order change information */
+ opj_poc_t poc;
+ /** number of components in the image */
+ OPJ_UINT32 numcomps;
+ /** Components*/
+ opj_pi_comp_t *comps;
+ /** FIXME DOC*/
+ OPJ_INT32 tx0, ty0, tx1, ty1;
+ /** FIXME DOC*/
+ OPJ_INT32 x, y;
+ /** FIXME DOC*/
+ OPJ_UINT32 dx, dy;
+} opj_pi_iterator_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+ * Creates a packet iterator for encoding.
+ *
+ * @param image the image being encoded.
+ * @param cp the coding parameters.
+ * @param tileno index of the tile being encoded.
+ * @param t2_mode the type of pass for generating the packet iterator
+ *
+ * @return a list of packet iterator that points to the first packet of the tile (not true).
+*/
+opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *image,
+ opj_cp_t *cp,
+ OPJ_UINT32 tileno,
+ J2K_T2_MODE t2_mode);
+
+/**
+ * Updates the encoding parameters of the codec.
+ *
+ * @param p_image the image being encoded.
+ * @param p_cp the coding parameters.
+ * @param p_tile_no index of the tile being encoded.
+*/
+void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
+ opj_cp_t *p_cp,
+ OPJ_UINT32 p_tile_no );
+
+/**
+Modify the packet iterator for enabling tile part generation
+@param pi Handle to the packet iterator generated in pi_initialise_encode
+@param cp Coding parameters
+@param tileno Number that identifies the tile for which to list the packets
+@param pino FIXME DOC
+@param tpnum Tile part number of the current tile
+@param tppos The position of the tile part flag in the progression order
+@param t2_mode FIXME DOC
+*/
+void opj_pi_create_encode( opj_pi_iterator_t *pi,
+ opj_cp_t *cp,
+ OPJ_UINT32 tileno,
+ OPJ_UINT32 pino,
+ OPJ_UINT32 tpnum,
+ OPJ_INT32 tppos,
+ J2K_T2_MODE t2_mode);
+
+/**
+Create a packet iterator for Decoder
+@param image Raw image for which the packets will be listed
+@param cp Coding parameters
+@param tileno Number that identifies the tile for which to list the packets
+@return Returns a packet iterator that points to the first packet of the tile
+@see opj_pi_destroy
+*/
+opj_pi_iterator_t *opj_pi_create_decode(opj_image_t * image,
+ opj_cp_t * cp,
+ OPJ_UINT32 tileno);
+/**
+ * Destroys a packet iterator array.
+ *
+ * @param p_pi the packet iterator array to destroy.
+ * @param p_nb_elements the number of elements in the array.
+ */
+void opj_pi_destroy(opj_pi_iterator_t *p_pi,
+ OPJ_UINT32 p_nb_elements);
+
+/**
+Modify the packet iterator to point to the next packet
+@param pi Packet iterator to modify
+@return Returns false if pi pointed to the last packet or else returns true
+*/
+OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __PI_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.c
new file mode 100644
index 0000000000..2498761c42
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.c
@@ -0,0 +1,95 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+
+/*
+==========================================================
+ RAW encoding interface
+==========================================================
+*/
+
+opj_raw_t* opj_raw_create(void) {
+ opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t));
+ return raw;
+}
+
+void opj_raw_destroy(opj_raw_t *raw) {
+ if(raw) {
+ opj_free(raw);
+ }
+}
+
+OPJ_UINT32 opj_raw_numbytes(opj_raw_t *raw) {
+ const ptrdiff_t diff = raw->bp - raw->start;
+ assert( diff <= (ptrdiff_t)0xffffffff && diff >= 0 ); /* UINT32_MAX */
+ return (OPJ_UINT32)diff;
+}
+
+void opj_raw_init_dec(opj_raw_t *raw, OPJ_BYTE *bp, OPJ_UINT32 len) {
+ raw->start = bp;
+ raw->lenmax = len;
+ raw->len = 0;
+ raw->c = 0;
+ raw->ct = 0;
+}
+
+OPJ_UINT32 opj_raw_decode(opj_raw_t *raw) {
+ OPJ_UINT32 d;
+ if (raw->ct == 0) {
+ raw->ct = 8;
+ if (raw->len == raw->lenmax) {
+ raw->c = 0xff;
+ } else {
+ if (raw->c == 0xff) {
+ raw->ct = 7;
+ }
+ raw->c = *(raw->start + raw->len);
+ raw->len++;
+ }
+ }
+ raw->ct--;
+ d = (raw->c >> raw->ct) & 0x01;
+
+ return d;
+}
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.h
new file mode 100644
index 0000000000..572c666176
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/raw.h
@@ -0,0 +1,106 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __RAW_H
+#define __RAW_H
+/**
+@file raw.h
+@brief Implementation of operations for raw encoding (RAW)
+
+The functions in RAW.C have for goal to realize the operation of raw encoding linked
+with the corresponding mode switch.
+*/
+
+/** @defgroup RAW RAW - Implementation of operations for raw encoding */
+/*@{*/
+
+/**
+RAW encoding operations
+*/
+typedef struct opj_raw {
+ /** temporary buffer where bits are coded or decoded */
+ OPJ_BYTE c;
+ /** number of bits already read or free to write */
+ OPJ_UINT32 ct;
+ /** maximum length to decode */
+ OPJ_UINT32 lenmax;
+ /** length decoded */
+ OPJ_UINT32 len;
+ /** pointer to the current position in the buffer */
+ OPJ_BYTE *bp;
+ /** pointer to the start of the buffer */
+ OPJ_BYTE *start;
+ /** pointer to the end of the buffer */
+ OPJ_BYTE *end;
+} opj_raw_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a new RAW handle
+@return Returns a new RAW handle if successful, returns NULL otherwise
+*/
+opj_raw_t* opj_raw_create(void);
+/**
+Destroy a previously created RAW handle
+@param raw RAW handle to destroy
+*/
+void opj_raw_destroy(opj_raw_t *raw);
+/**
+Return the number of bytes written/read since initialisation
+@param raw RAW handle to destroy
+@return Returns the number of bytes already encoded
+*/
+OPJ_UINT32 opj_raw_numbytes(opj_raw_t *raw);
+/**
+Initialize the decoder
+@param raw RAW handle
+@param bp Pointer to the start of the buffer from which the bytes will be read
+@param len Length of the input buffer
+*/
+void opj_raw_init_dec(opj_raw_t *raw, OPJ_BYTE *bp, OPJ_UINT32 len);
+/**
+Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
+@param raw RAW handle
+@return Returns the decoded symbol (0 or 1)
+*/
+OPJ_UINT32 opj_raw_decode(opj_raw_t *raw);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __RAW_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.c
new file mode 100644
index 0000000000..0d6c2f606a
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.c
@@ -0,0 +1,1757 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+#include "t1_luts.h"
+
+/** @defgroup T1 T1 - Implementation of the tier-1 coding */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
+static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
+static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
+static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
+static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
+static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
+static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
+/**
+Encode significant pass
+*/
+static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc);
+
+/**
+Decode significant pass
+*/
+#if 0
+static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc);
+#endif
+
+static INLINE void opj_t1_dec_sigpass_step_raw(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 vsc);
+static INLINE void opj_t1_dec_sigpass_step_mqc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf);
+static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 vsc);
+
+
+/**
+Encode significant pass
+*/
+static void opj_t1_enc_sigpass( opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 orient,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 cblksty);
+
+/**
+Decode significant pass
+*/
+static void opj_t1_dec_sigpass_raw(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient,
+ OPJ_INT32 cblksty);
+static void opj_t1_dec_sigpass_mqc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient);
+static void opj_t1_dec_sigpass_mqc_vsc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient);
+
+
+
+/**
+Encode refinement pass
+*/
+static void opj_t1_enc_refpass_step(opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc);
+
+
+/**
+Encode refinement pass
+*/
+static void opj_t1_enc_refpass( opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 cblksty);
+
+/**
+Decode refinement pass
+*/
+static void opj_t1_dec_refpass_raw(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 cblksty);
+static void opj_t1_dec_refpass_mqc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno);
+static void opj_t1_dec_refpass_mqc_vsc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno);
+
+
+/**
+Decode refinement pass
+*/
+#if 0
+static void opj_t1_dec_refpass_step(opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc);
+#endif
+
+static INLINE void opj_t1_dec_refpass_step_raw(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_INT32 vsc);
+static INLINE void opj_t1_dec_refpass_step_mqc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf);
+static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_INT32 vsc);
+
+
+
+/**
+Encode clean-up pass
+*/
+static void opj_t1_enc_clnpass_step(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_UINT32 partial,
+ OPJ_UINT32 vsc);
+/**
+Decode clean-up pass
+*/
+static void opj_t1_dec_clnpass_step_partial(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf);
+static void opj_t1_dec_clnpass_step(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf);
+static void opj_t1_dec_clnpass_step_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 partial,
+ OPJ_INT32 vsc);
+/**
+Encode clean-up pass
+*/
+static void opj_t1_enc_clnpass(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 orient,
+ OPJ_INT32 *nmsedec,
+ OPJ_UINT32 cblksty);
+/**
+Decode clean-up pass
+*/
+static void opj_t1_dec_clnpass(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient,
+ OPJ_INT32 cblksty);
+
+static OPJ_FLOAT64 opj_t1_getwmsedec(
+ OPJ_INT32 nmsedec,
+ OPJ_UINT32 compno,
+ OPJ_UINT32 level,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 qmfbid,
+ OPJ_FLOAT64 stepsize,
+ OPJ_UINT32 numcomps,
+ const OPJ_FLOAT64 * mct_norms);
+
+static void opj_t1_encode_cblk( opj_t1_t *t1,
+ opj_tcd_cblk_enc_t* cblk,
+ OPJ_UINT32 orient,
+ OPJ_UINT32 compno,
+ OPJ_UINT32 level,
+ OPJ_UINT32 qmfbid,
+ OPJ_FLOAT64 stepsize,
+ OPJ_UINT32 cblksty,
+ OPJ_UINT32 numcomps,
+ opj_tcd_tile_t * tile,
+ const OPJ_FLOAT64 * mct_norms);
+
+/**
+Decode 1 code-block
+@param t1 T1 handle
+@param cblk Code-block coding parameters
+@param orient
+@param roishift Region of interest shifting value
+@param cblksty Code-block style
+*/
+static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
+ opj_tcd_cblk_dec_t* cblk,
+ OPJ_UINT32 orient,
+ OPJ_UINT32 roishift,
+ OPJ_UINT32 cblksty);
+
+OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
+ OPJ_UINT32 w,
+ OPJ_UINT32 h);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
+ return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
+}
+
+OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
+ return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
+}
+
+OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
+ OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
+ OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
+ return (tmp2);
+}
+
+OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
+ return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
+}
+
+OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
+ if (bitpos > T1_NMSEDEC_FRACBITS) {
+ return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+ }
+
+ return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
+ if (bitpos > T1_NMSEDEC_FRACBITS) {
+ return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
+ }
+
+ return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
+}
+
+void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
+ opj_flag_t *np = flagsp - stride;
+ opj_flag_t *sp = flagsp + stride;
+
+ static const opj_flag_t mod[] = {
+ T1_SIG_S, T1_SIG_S|T1_SGN_S,
+ T1_SIG_E, T1_SIG_E|T1_SGN_E,
+ T1_SIG_W, T1_SIG_W|T1_SGN_W,
+ T1_SIG_N, T1_SIG_N|T1_SGN_N
+ };
+
+ np[-1] |= T1_SIG_SE;
+ np[0] |= mod[s];
+ np[1] |= T1_SIG_SW;
+
+ flagsp[-1] |= mod[s+2];
+ flagsp[0] |= T1_SIG;
+ flagsp[1] |= mod[s+4];
+
+ sp[-1] |= T1_SIG_NE;
+ sp[0] |= mod[s+6];
+ sp[1] |= T1_SIG_NW;
+}
+
+void opj_t1_enc_sigpass_step( opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc
+ )
+{
+ OPJ_INT32 v;
+ OPJ_UINT32 flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
+ if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+ v = opj_int_abs(*datap) & one ? 1 : 0;
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
+ if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
+ opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
+ } else {
+ opj_mqc_encode(mqc, (OPJ_UINT32)v);
+ }
+ if (v) {
+ v = *datap < 0 ? 1 : 0;
+ *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
+ if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
+ opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
+ } else {
+ opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
+ }
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ *flagsp |= T1_VISIT;
+ }
+}
+
+
+static INLINE void opj_t1_dec_sigpass_step_raw(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 vsc)
+{
+ OPJ_INT32 v, flag;
+ opj_raw_t *raw = t1->raw; /* RAW component */
+ OPJ_ARG_NOT_USED(orient);
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+ if (opj_raw_decode(raw)) {
+ v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ *flagsp |= T1_VISIT;
+ }
+}
+
+INLINE void opj_t1_dec_sigpass_step_mqc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf)
+{
+ OPJ_INT32 v, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = *flagsp;
+ if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
+ if (opj_mqc_decode(mqc)) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ *flagsp |= T1_VISIT;
+ }
+} /* VSC and BYPASS by Antonin */
+
+INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 vsc)
+{
+ OPJ_INT32 v, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
+ if (opj_mqc_decode(mqc)) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ *flagsp |= T1_VISIT;
+ }
+} /* VSC and BYPASS by Antonin */
+
+
+
+void opj_t1_enc_sigpass(opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 orient,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 cblksty
+ )
+{
+ OPJ_UINT32 i, j, k, vsc;
+ OPJ_INT32 one;
+
+ *nmsedec = 0;
+ one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_enc_sigpass_step(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ orient,
+ bpno,
+ one,
+ nmsedec,
+ type,
+ vsc);
+ }
+ }
+ }
+}
+
+void opj_t1_dec_sigpass_raw(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient,
+ OPJ_INT32 cblksty)
+{
+ OPJ_INT32 one, half, oneplushalf, vsc;
+ OPJ_UINT32 i, j, k;
+ one = 1 << bpno;
+ half = one >> 1;
+ oneplushalf = one | half;
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_dec_sigpass_step_raw(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ orient,
+ oneplushalf,
+ vsc);
+ }
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+void opj_t1_dec_sigpass_mqc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient)
+{
+ OPJ_INT32 one, half, oneplushalf;
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 *data1 = t1->data;
+ opj_flag_t *flags1 = &t1->flags[1];
+ one = 1 << bpno;
+ half = one >> 1;
+ oneplushalf = one | half;
+ for (k = 0; k < (t1->h & ~3u); k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ }
+ data1 += t1->w << 2;
+ flags1 += t1->flags_stride << 2;
+ }
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ for (j = k; j < t1->h; ++j) {
+ flags2 += t1->flags_stride;
+ opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+void opj_t1_dec_sigpass_mqc_vsc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient)
+{
+ OPJ_INT32 one, half, oneplushalf, vsc;
+ OPJ_UINT32 i, j, k;
+ one = 1 << bpno;
+ half = one >> 1;
+ oneplushalf = one | half;
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
+ opj_t1_dec_sigpass_step_mqc_vsc(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ orient,
+ oneplushalf,
+ vsc);
+ }
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+
+
+void opj_t1_enc_refpass_step( opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc)
+{
+ OPJ_INT32 v;
+ OPJ_UINT32 flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
+ if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+ *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
+ v = opj_int_abs(*datap) & one ? 1 : 0;
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
+ if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
+ opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
+ } else {
+ opj_mqc_encode(mqc, (OPJ_UINT32)v);
+ }
+ *flagsp |= T1_REFINE;
+ }
+}
+
+INLINE void opj_t1_dec_refpass_step_raw(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_INT32 vsc)
+{
+ OPJ_INT32 v, t, flag;
+
+ opj_raw_t *raw = t1->raw; /* RAW component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+ v = (OPJ_INT32)opj_raw_decode(raw);
+ t = v ? poshalf : neghalf;
+ *datap += *datap < 0 ? -t : t;
+ *flagsp |= T1_REFINE;
+ }
+} /* VSC and BYPASS by Antonin */
+
+INLINE void opj_t1_dec_refpass_step_mqc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf)
+{
+ OPJ_INT32 v, t, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = *flagsp;
+ if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
+ v = opj_mqc_decode(mqc);
+ t = v ? poshalf : neghalf;
+ *datap += *datap < 0 ? -t : t;
+ *flagsp |= T1_REFINE;
+ }
+} /* VSC and BYPASS by Antonin */
+
+INLINE void opj_t1_dec_refpass_step_mqc_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_INT32 vsc)
+{
+ OPJ_INT32 v, t, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
+ v = opj_mqc_decode(mqc);
+ t = v ? poshalf : neghalf;
+ *datap += *datap < 0 ? -t : t;
+ *flagsp |= T1_REFINE;
+ }
+} /* VSC and BYPASS by Antonin */
+
+
+void opj_t1_enc_refpass(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 *nmsedec,
+ OPJ_BYTE type,
+ OPJ_UINT32 cblksty)
+{
+ OPJ_UINT32 i, j, k, vsc;
+ OPJ_INT32 one;
+
+ *nmsedec = 0;
+ one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_enc_refpass_step(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ bpno,
+ one,
+ nmsedec,
+ type,
+ vsc);
+ }
+ }
+ }
+}
+
+void opj_t1_dec_refpass_raw(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 cblksty)
+{
+ OPJ_INT32 one, poshalf, neghalf;
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 vsc;
+ one = 1 << bpno;
+ poshalf = one >> 1;
+ neghalf = bpno > 0 ? -poshalf : -1;
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_dec_refpass_step_raw(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ poshalf,
+ neghalf,
+ vsc);
+ }
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+void opj_t1_dec_refpass_mqc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno)
+{
+ OPJ_INT32 one, poshalf, neghalf;
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 *data1 = t1->data;
+ opj_flag_t *flags1 = &t1->flags[1];
+ one = 1 << bpno;
+ poshalf = one >> 1;
+ neghalf = bpno > 0 ? -poshalf : -1;
+ for (k = 0; k < (t1->h & ~3u); k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+ data2 += t1->w;
+ }
+ data1 += t1->w << 2;
+ flags1 += t1->flags_stride << 2;
+ }
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ for (j = k; j < t1->h; ++j) {
+ flags2 += t1->flags_stride;
+ opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+ data2 += t1->w;
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+void opj_t1_dec_refpass_mqc_vsc(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno)
+{
+ OPJ_INT32 one, poshalf, neghalf;
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 vsc;
+ one = 1 << bpno;
+ poshalf = one >> 1;
+ neghalf = bpno > 0 ? -poshalf : -1;
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ for (j = k; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_dec_refpass_step_mqc_vsc(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ poshalf,
+ neghalf,
+ vsc);
+ }
+ }
+ }
+} /* VSC and BYPASS by Antonin */
+
+
+void opj_t1_enc_clnpass_step(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_INT32 one,
+ OPJ_INT32 *nmsedec,
+ OPJ_UINT32 partial,
+ OPJ_UINT32 vsc)
+{
+ OPJ_INT32 v;
+ OPJ_UINT32 flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
+ if (partial) {
+ goto LABEL_PARTIAL;
+ }
+ if (!(*flagsp & (T1_SIG | T1_VISIT))) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
+ v = opj_int_abs(*datap) & one ? 1 : 0;
+ opj_mqc_encode(mqc, (OPJ_UINT32)v);
+ if (v) {
+LABEL_PARTIAL:
+ *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
+ v = *datap < 0 ? 1 : 0;
+ opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ }
+ *flagsp &= ~T1_VISIT;
+}
+
+static void opj_t1_dec_clnpass_step_partial(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf)
+{
+ OPJ_INT32 v, flag;
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ OPJ_ARG_NOT_USED(orient);
+
+ flag = *flagsp;
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ *flagsp &= ~T1_VISIT;
+} /* VSC and BYPASS by Antonin */
+
+static void opj_t1_dec_clnpass_step(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf)
+{
+ OPJ_INT32 v, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = *flagsp;
+ if (!(flag & (T1_SIG | T1_VISIT))) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
+ if (opj_mqc_decode(mqc)) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ }
+ *flagsp &= ~T1_VISIT;
+} /* VSC and BYPASS by Antonin */
+
+static void opj_t1_dec_clnpass_step_vsc(
+ opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_INT32 partial,
+ OPJ_INT32 vsc)
+{
+ OPJ_INT32 v, flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if (partial) {
+ goto LABEL_PARTIAL;
+ }
+ if (!(flag & (T1_SIG | T1_VISIT))) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
+ if (opj_mqc_decode(mqc)) {
+LABEL_PARTIAL:
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
+ }
+ }
+ *flagsp &= ~T1_VISIT;
+}
+
+void opj_t1_enc_clnpass(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 orient,
+ OPJ_INT32 *nmsedec,
+ OPJ_UINT32 cblksty)
+{
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 one;
+ OPJ_UINT32 agg, runlen, vsc;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ *nmsedec = 0;
+ one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ if (k + 3 < t1->h) {
+ if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+ agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || (MACRO_t1_flags(1 + k + 3,1 + i)
+ & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+ } else {
+ agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+ }
+ } else {
+ agg = 0;
+ }
+ if (agg) {
+ for (runlen = 0; runlen < 4; ++runlen) {
+ if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
+ break;
+ }
+ opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
+ opj_mqc_encode(mqc, runlen != 4);
+ if (runlen == 4) {
+ continue;
+ }
+ opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
+ opj_mqc_encode(mqc, runlen >> 1);
+ opj_mqc_encode(mqc, runlen & 1);
+ } else {
+ runlen = 0;
+ }
+ for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
+ vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+ opj_t1_enc_clnpass_step(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ orient,
+ bpno,
+ one,
+ nmsedec,
+ agg && (j == k + runlen),
+ vsc);
+ }
+ }
+ }
+}
+
+static void opj_t1_dec_clnpass(
+ opj_t1_t *t1,
+ OPJ_INT32 bpno,
+ OPJ_INT32 orient,
+ OPJ_INT32 cblksty)
+{
+ OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
+ OPJ_UINT32 i, j, k;
+ OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ one = 1 << bpno;
+ half = one >> 1;
+ oneplushalf = one | half;
+ if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+ for (k = 0; k < t1->h; k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ if (k + 3 < t1->h) {
+ agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || (MACRO_t1_flags(1 + k + 3,1 + i)
+ & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+ } else {
+ agg = 0;
+ }
+ if (agg) {
+ opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
+ if (!opj_mqc_decode(mqc)) {
+ continue;
+ }
+ opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
+ runlen = opj_mqc_decode(mqc);
+ runlen = (runlen << 1) | opj_mqc_decode(mqc);
+ } else {
+ runlen = 0;
+ }
+ for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
+ vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
+ opj_t1_dec_clnpass_step_vsc(
+ t1,
+ &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+ &t1->data[(j * t1->w) + i],
+ orient,
+ oneplushalf,
+ agg && (j == k + (OPJ_UINT32)runlen),
+ vsc);
+ }
+ }
+ }
+ } else {
+ OPJ_INT32 *data1 = t1->data;
+ opj_flag_t *flags1 = &t1->flags[1];
+ for (k = 0; k < (t1->h & ~3u); k += 4) {
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
+ || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
+ if (agg) {
+ opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
+ if (!opj_mqc_decode(mqc)) {
+ continue;
+ }
+ opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
+ runlen = opj_mqc_decode(mqc);
+ runlen = (runlen << 1) | opj_mqc_decode(mqc);
+ flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
+ data2 += (OPJ_UINT32)runlen * t1->w;
+ for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
+ flags2 += t1->flags_stride;
+ if (agg && (j == k + (OPJ_UINT32)runlen)) {
+ opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
+ } else {
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ }
+ data2 += t1->w;
+ }
+ } else {
+ flags2 += t1->flags_stride;
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ flags2 += t1->flags_stride;
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ }
+ }
+ data1 += t1->w << 2;
+ flags1 += t1->flags_stride << 2;
+ }
+ for (i = 0; i < t1->w; ++i) {
+ OPJ_INT32 *data2 = data1 + i;
+ opj_flag_t *flags2 = flags1 + i;
+ for (j = k; j < t1->h; ++j) {
+ flags2 += t1->flags_stride;
+ opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+ data2 += t1->w;
+ }
+ }
+ }
+
+ if (segsym) {
+ OPJ_INT32 v = 0;
+ opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
+ v = opj_mqc_decode(mqc);
+ v = (v << 1) | opj_mqc_decode(mqc);
+ v = (v << 1) | opj_mqc_decode(mqc);
+ v = (v << 1) | opj_mqc_decode(mqc);
+ /*
+ if (v!=0xa) {
+ opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
+ }
+ */
+ }
+} /* VSC and BYPASS by Antonin */
+
+
+/** mod fixed_quality */
+static OPJ_FLOAT64 opj_t1_getwmsedec(
+ OPJ_INT32 nmsedec,
+ OPJ_UINT32 compno,
+ OPJ_UINT32 level,
+ OPJ_UINT32 orient,
+ OPJ_INT32 bpno,
+ OPJ_UINT32 qmfbid,
+ OPJ_FLOAT64 stepsize,
+ OPJ_UINT32 numcomps,
+ const OPJ_FLOAT64 * mct_norms)
+{
+ OPJ_FLOAT64 w1 = 1, w2, wmsedec;
+ OPJ_ARG_NOT_USED(numcomps);
+
+ if (mct_norms) {
+ w1 = mct_norms[compno];
+ }
+
+ if (qmfbid == 1) {
+ w2 = opj_dwt_getnorm(level, orient);
+ } else { /* if (qmfbid == 0) */
+ w2 = opj_dwt_getnorm_real(level, orient);
+ }
+
+ wmsedec = w1 * w2 * stepsize * (1 << bpno);
+ wmsedec *= wmsedec * nmsedec / 8192.0;
+
+ return wmsedec;
+}
+
+OPJ_BOOL opj_t1_allocate_buffers(
+ opj_t1_t *t1,
+ OPJ_UINT32 w,
+ OPJ_UINT32 h)
+{
+ OPJ_UINT32 datasize=w * h;
+ OPJ_UINT32 flagssize;
+
+ if(datasize > t1->datasize){
+ opj_aligned_free(t1->data);
+ t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
+ if(!t1->data){
+ return OPJ_FALSE;
+ }
+ t1->datasize=datasize;
+ }
+ memset(t1->data,0,datasize * sizeof(OPJ_INT32));
+
+ t1->flags_stride=w+2;
+ flagssize=t1->flags_stride * (h+2);
+
+ if(flagssize > t1->flagssize){
+ opj_aligned_free(t1->flags);
+ t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
+ if(!t1->flags){
+ return OPJ_FALSE;
+ }
+ t1->flagssize=flagssize;
+ }
+ memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
+
+ t1->w=w;
+ t1->h=h;
+
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/**
+ * Creates a new Tier 1 handle
+ * and initializes the look-up tables of the Tier-1 coder/decoder
+ * @return a new T1 handle if successful, returns NULL otherwise
+*/
+opj_t1_t* opj_t1_create()
+{
+ opj_t1_t *l_t1 = 00;
+
+ l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
+ if (!l_t1) {
+ return 00;
+ }
+ memset(l_t1,0,sizeof(opj_t1_t));
+
+ /* create MQC and RAW handles */
+ l_t1->mqc = opj_mqc_create();
+ if (! l_t1->mqc) {
+ opj_t1_destroy(l_t1);
+ return 00;
+ }
+
+ l_t1->raw = opj_raw_create();
+ if (! l_t1->raw) {
+ opj_t1_destroy(l_t1);
+ return 00;
+ }
+
+ return l_t1;
+}
+
+
+/**
+ * Destroys a previously created T1 handle
+ *
+ * @param p_t1 Tier 1 handle to destroy
+*/
+void opj_t1_destroy(opj_t1_t *p_t1)
+{
+ if (! p_t1) {
+ return;
+ }
+
+ /* destroy MQC and RAW handles */
+ opj_mqc_destroy(p_t1->mqc);
+ p_t1->mqc = 00;
+ opj_raw_destroy(p_t1->raw);
+ p_t1->raw = 00;
+
+ if (p_t1->data) {
+ opj_aligned_free(p_t1->data);
+ p_t1->data = 00;
+ }
+
+ if (p_t1->flags) {
+ opj_aligned_free(p_t1->flags);
+ p_t1->flags = 00;
+ }
+
+ opj_free(p_t1);
+}
+
+OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
+ opj_tcd_tilecomp_t* tilec,
+ opj_tccp_t* tccp
+ )
+{
+ OPJ_UINT32 resno, bandno, precno, cblkno;
+ OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
+
+ for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
+ opj_tcd_resolution_t* res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; ++bandno) {
+ opj_tcd_band_t* restrict band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; ++precno) {
+ opj_tcd_precinct_t* precinct = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
+ opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
+ OPJ_INT32* restrict datap;
+ /*void* restrict tiledp;*/
+ OPJ_UINT32 cblk_w, cblk_h;
+ OPJ_INT32 x, y;
+ OPJ_UINT32 i, j;
+
+ if (OPJ_FALSE == opj_t1_decode_cblk(
+ t1,
+ cblk,
+ band->bandno,
+ (OPJ_UINT32)tccp->roishift,
+ tccp->cblksty)) {
+ return OPJ_FALSE;
+ }
+
+ x = cblk->x0 - band->x0;
+ y = cblk->y0 - band->y0;
+ if (band->bandno & 1) {
+ opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
+ x += pres->x1 - pres->x0;
+ }
+ if (band->bandno & 2) {
+ opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
+ y += pres->y1 - pres->y0;
+ }
+
+ datap=t1->data;
+ cblk_w = t1->w;
+ cblk_h = t1->h;
+
+ if (tccp->roishift) {
+ OPJ_INT32 thresh = 1 << tccp->roishift;
+ for (j = 0; j < cblk_h; ++j) {
+ for (i = 0; i < cblk_w; ++i) {
+ OPJ_INT32 val = datap[(j * cblk_w) + i];
+ OPJ_INT32 mag = abs(val);
+ if (mag >= thresh) {
+ mag >>= tccp->roishift;
+ datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
+ }
+ }
+ }
+ }
+
+ /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
+ if (tccp->qmfbid == 1) {
+ OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
+ for (j = 0; j < cblk_h; ++j) {
+ for (i = 0; i < cblk_w; ++i) {
+ OPJ_INT32 tmp = datap[(j * cblk_w) + i];
+ ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
+ }
+ }
+ } else { /* if (tccp->qmfbid == 0) */
+ OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
+ for (j = 0; j < cblk_h; ++j) {
+ OPJ_FLOAT32* restrict tiledp2 = tiledp;
+ for (i = 0; i < cblk_w; ++i) {
+ OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
+ *tiledp2 = tmp;
+ datap++;
+ tiledp2++;
+ /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
+ ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
+
+ }
+ tiledp += tile_w;
+ }
+ }
+ /*opj_free(cblk->data);
+ opj_free(cblk->segs);*/
+ /*cblk->segs = 00;*/
+ } /* cblkno */
+ /*opj_free(precinct->cblks.dec);*/
+ } /* precno */
+ } /* bandno */
+ } /* resno */
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
+ opj_tcd_cblk_dec_t* cblk,
+ OPJ_UINT32 orient,
+ OPJ_UINT32 roishift,
+ OPJ_UINT32 cblksty)
+{
+ opj_raw_t *raw = t1->raw; /* RAW component */
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ OPJ_INT32 bpno;
+ OPJ_UINT32 passtype;
+ OPJ_UINT32 segno, passno;
+ OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
+
+ if(!opj_t1_allocate_buffers(
+ t1,
+ (OPJ_UINT32)(cblk->x1 - cblk->x0),
+ (OPJ_UINT32)(cblk->y1 - cblk->y0)))
+ {
+ return OPJ_FALSE;
+ }
+
+ bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
+ passtype = 2;
+
+ opj_mqc_resetstates(mqc);
+ opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
+ opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
+ opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
+
+ for (segno = 0; segno < cblk->real_num_segs; ++segno) {
+ opj_tcd_seg_t *seg = &cblk->segs[segno];
+
+ /* BYPASS mode */
+ type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+ /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
+ if(seg->data == 00){
+ continue;
+ }
+ if (type == T1_TYPE_RAW) {
+ opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
+ } else {
+ if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ for (passno = 0; passno < seg->real_num_passes; ++passno) {
+ switch (passtype) {
+ case 0:
+ if (type == T1_TYPE_RAW) {
+ opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
+ } else {
+ if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+ opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
+ } else {
+ opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
+ }
+ }
+ break;
+ case 1:
+ if (type == T1_TYPE_RAW) {
+ opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
+ } else {
+ if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+ opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
+ } else {
+ opj_t1_dec_refpass_mqc(t1, bpno+1);
+ }
+ }
+ break;
+ case 2:
+ opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
+ break;
+ }
+
+ if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
+ opj_mqc_resetstates(mqc);
+ opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
+ opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
+ opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
+ }
+ if (++passtype == 3) {
+ passtype = 0;
+ bpno--;
+ }
+ }
+ }
+ return OPJ_TRUE;
+}
+
+
+
+
+OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
+ opj_tcd_tile_t *tile,
+ opj_tcp_t *tcp,
+ const OPJ_FLOAT64 * mct_norms
+ )
+{
+ OPJ_UINT32 compno, resno, bandno, precno, cblkno;
+
+ tile->distotile = 0; /* fixed_quality */
+
+ for (compno = 0; compno < tile->numcomps; ++compno) {
+ opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
+ opj_tccp_t* tccp = &tcp->tccps[compno];
+ OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
+
+ for (resno = 0; resno < tilec->numresolutions; ++resno) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; ++bandno) {
+ opj_tcd_band_t* restrict band = &res->bands[bandno];
+ OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
+
+ for (precno = 0; precno < res->pw * res->ph; ++precno) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
+ opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
+ OPJ_INT32 * restrict datap;
+ OPJ_INT32* restrict tiledp;
+ OPJ_UINT32 cblk_w;
+ OPJ_UINT32 cblk_h;
+ OPJ_UINT32 i, j;
+
+ OPJ_INT32 x = cblk->x0 - band->x0;
+ OPJ_INT32 y = cblk->y0 - band->y0;
+ if (band->bandno & 1) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x += pres->x1 - pres->x0;
+ }
+ if (band->bandno & 2) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ y += pres->y1 - pres->y0;
+ }
+
+ if(!opj_t1_allocate_buffers(
+ t1,
+ (OPJ_UINT32)(cblk->x1 - cblk->x0),
+ (OPJ_UINT32)(cblk->y1 - cblk->y0)))
+ {
+ return OPJ_FALSE;
+ }
+
+ datap=t1->data;
+ cblk_w = t1->w;
+ cblk_h = t1->h;
+
+ tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
+ if (tccp->qmfbid == 1) {
+ for (j = 0; j < cblk_h; ++j) {
+ for (i = 0; i < cblk_w; ++i) {
+ OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
+ datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
+ }
+ }
+ } else { /* if (tccp->qmfbid == 0) */
+ for (j = 0; j < cblk_h; ++j) {
+ for (i = 0; i < cblk_w; ++i) {
+ OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
+ datap[(j * cblk_w) + i] =
+ opj_int_fix_mul(
+ tmp,
+ bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
+ }
+ }
+ }
+
+ opj_t1_encode_cblk(
+ t1,
+ cblk,
+ band->bandno,
+ compno,
+ tilec->numresolutions - 1 - resno,
+ tccp->qmfbid,
+ band->stepsize,
+ tccp->cblksty,
+ tile->numcomps,
+ tile,
+ mct_norms);
+
+ } /* cblkno */
+ } /* precno */
+ } /* bandno */
+ } /* resno */
+ } /* compno */
+ return OPJ_TRUE;
+}
+
+/** mod fixed_quality */
+void opj_t1_encode_cblk(opj_t1_t *t1,
+ opj_tcd_cblk_enc_t* cblk,
+ OPJ_UINT32 orient,
+ OPJ_UINT32 compno,
+ OPJ_UINT32 level,
+ OPJ_UINT32 qmfbid,
+ OPJ_FLOAT64 stepsize,
+ OPJ_UINT32 cblksty,
+ OPJ_UINT32 numcomps,
+ opj_tcd_tile_t * tile,
+ const OPJ_FLOAT64 * mct_norms)
+{
+ OPJ_FLOAT64 cumwmsedec = 0.0;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ OPJ_UINT32 passno;
+ OPJ_INT32 bpno;
+ OPJ_UINT32 passtype;
+ OPJ_INT32 nmsedec = 0;
+ OPJ_INT32 max;
+ OPJ_UINT32 i;
+ OPJ_BYTE type = T1_TYPE_MQ;
+ OPJ_FLOAT64 tempwmsedec;
+
+ max = 0;
+ for (i = 0; i < t1->w * t1->h; ++i) {
+ OPJ_INT32 tmp = abs(t1->data[i]);
+ max = opj_int_max(max, tmp);
+ }
+
+ cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
+
+ bpno = (OPJ_INT32)(cblk->numbps - 1);
+ passtype = 2;
+
+ opj_mqc_resetstates(mqc);
+ opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
+ opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
+ opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
+ opj_mqc_init_enc(mqc, cblk->data);
+
+ for (passno = 0; bpno >= 0; ++passno) {
+ opj_tcd_pass_t *pass = &cblk->passes[passno];
+ OPJ_UINT32 correction = 3;
+ type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+
+ switch (passtype) {
+ case 0:
+ opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
+ break;
+ case 1:
+ opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
+ break;
+ case 2:
+ opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
+ /* code switch SEGMARK (i.e. SEGSYM) */
+ if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
+ opj_mqc_segmark_enc(mqc);
+ break;
+ }
+
+ /* fixed_quality */
+ tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
+ cumwmsedec += tempwmsedec;
+ tile->distotile += tempwmsedec;
+
+ /* Code switch "RESTART" (i.e. TERMALL) */
+ if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
+ if (type == T1_TYPE_RAW) {
+ opj_mqc_flush(mqc);
+ correction = 1;
+ /* correction = mqc_bypass_flush_enc(); */
+ } else { /* correction = mqc_restart_enc(); */
+ opj_mqc_flush(mqc);
+ correction = 1;
+ }
+ pass->term = 1;
+ } else {
+ if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
+ || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
+ if (type == T1_TYPE_RAW) {
+ opj_mqc_flush(mqc);
+ correction = 1;
+ /* correction = mqc_bypass_flush_enc(); */
+ } else { /* correction = mqc_restart_enc(); */
+ opj_mqc_flush(mqc);
+ correction = 1;
+ }
+ pass->term = 1;
+ } else {
+ pass->term = 0;
+ }
+ }
+
+ if (++passtype == 3) {
+ passtype = 0;
+ bpno--;
+ }
+
+ if (pass->term && bpno > 0) {
+ type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
+ if (type == T1_TYPE_RAW)
+ opj_mqc_bypass_init_enc(mqc);
+ else
+ opj_mqc_restart_init_enc(mqc);
+ }
+
+ pass->distortiondec = cumwmsedec;
+ pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
+
+ /* Code-switch "RESET" */
+ if (cblksty & J2K_CCP_CBLKSTY_RESET)
+ opj_mqc_reset_enc(mqc);
+ }
+
+ /* Code switch "ERTERM" (i.e. PTERM) */
+ if (cblksty & J2K_CCP_CBLKSTY_PTERM)
+ opj_mqc_erterm_enc(mqc);
+ else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
+ opj_mqc_flush(mqc);
+
+ cblk->totalpasses = passno;
+
+ for (passno = 0; passno<cblk->totalpasses; passno++) {
+ opj_tcd_pass_t *pass = &cblk->passes[passno];
+ if (pass->rate > opj_mqc_numbytes(mqc))
+ pass->rate = opj_mqc_numbytes(mqc);
+ /*Preventing generation of FF as last data byte of a pass*/
+ if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
+ pass->rate--;
+ }
+ pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
+ }
+}
+
+#if 0
+void opj_t1_dec_refpass_step( opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_INT32 poshalf,
+ OPJ_INT32 neghalf,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc)
+{
+ OPJ_INT32 t;
+ OPJ_UINT32 v,flag;
+
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+ opj_raw_t *raw = t1->raw; /* RAW component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
+ if (type == T1_TYPE_RAW) {
+ v = opj_raw_decode(raw);
+ } else {
+ v = opj_mqc_decode(mqc);
+ }
+ t = v ? poshalf : neghalf;
+ *datap += *datap < 0 ? -t : t;
+ *flagsp |= T1_REFINE;
+ }
+} /* VSC and BYPASS by Antonin */
+#endif
+
+
+
+#if 0
+void opj_t1_dec_sigpass_step( opj_t1_t *t1,
+ opj_flag_t *flagsp,
+ OPJ_INT32 *datap,
+ OPJ_UINT32 orient,
+ OPJ_INT32 oneplushalf,
+ OPJ_BYTE type,
+ OPJ_UINT32 vsc)
+{
+ OPJ_UINT32 v, flag;
+
+ opj_raw_t *raw = t1->raw; /* RAW component */
+ opj_mqc_t *mqc = t1->mqc; /* MQC component */
+
+ flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+ if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+ if (type == T1_TYPE_RAW) {
+ if (opj_raw_decode(raw)) {
+ v = opj_raw_decode(raw); /* ESSAI */
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, v, t1->flags_stride);
+ }
+ } else {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
+ if (opj_mqc_decode(mqc)) {
+ opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
+ v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
+ *datap = v ? -oneplushalf : oneplushalf;
+ opj_t1_updateflags(flagsp, v, t1->flags_stride);
+ }
+ }
+ *flagsp |= T1_VISIT;
+ }
+} /* VSC and BYPASS by Antonin */
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.h
new file mode 100644
index 0000000000..e5be70ed5b
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1.h
@@ -0,0 +1,163 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __T1_H
+#define __T1_H
+/**
+@file t1.h
+@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
+
+The functions in T1.C have for goal to realize the tier-1 coding operation. The functions
+in T1.C are used by some function in TCD.C.
+*/
+
+/** @defgroup T1 T1 - Implementation of the tier-1 coding */
+/*@{*/
+
+/* ----------------------------------------------------------------------- */
+#define T1_NMSEDEC_BITS 7
+
+#define T1_SIG_NE 0x0001 /**< Context orientation : North-East direction */
+#define T1_SIG_SE 0x0002 /**< Context orientation : South-East direction */
+#define T1_SIG_SW 0x0004 /**< Context orientation : South-West direction */
+#define T1_SIG_NW 0x0008 /**< Context orientation : North-West direction */
+#define T1_SIG_N 0x0010 /**< Context orientation : North direction */
+#define T1_SIG_E 0x0020 /**< Context orientation : East direction */
+#define T1_SIG_S 0x0040 /**< Context orientation : South direction */
+#define T1_SIG_W 0x0080 /**< Context orientation : West direction */
+#define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)
+#define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)
+
+#define T1_SGN_N 0x0100
+#define T1_SGN_E 0x0200
+#define T1_SGN_S 0x0400
+#define T1_SGN_W 0x0800
+#define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)
+
+#define T1_SIG 0x1000
+#define T1_REFINE 0x2000
+#define T1_VISIT 0x4000
+
+#define T1_NUMCTXS_ZC 9
+#define T1_NUMCTXS_SC 5
+#define T1_NUMCTXS_MAG 3
+#define T1_NUMCTXS_AGG 1
+#define T1_NUMCTXS_UNI 1
+
+#define T1_CTXNO_ZC 0
+#define T1_CTXNO_SC (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
+#define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC)
+#define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
+#define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
+#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
+
+#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
+
+#define T1_TYPE_MQ 0 /**< Normal coding using entropy coder */
+#define T1_TYPE_RAW 1 /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/
+
+/* ----------------------------------------------------------------------- */
+
+typedef OPJ_INT16 opj_flag_t;
+
+/**
+Tier-1 coding (coding of code-block coefficients)
+*/
+typedef struct opj_t1 {
+
+ /** MQC component */
+ opj_mqc_t *mqc;
+ /** RAW component */
+ opj_raw_t *raw;
+
+ OPJ_INT32 *data;
+ opj_flag_t *flags;
+ OPJ_UINT32 w;
+ OPJ_UINT32 h;
+ OPJ_UINT32 datasize;
+ OPJ_UINT32 flagssize;
+ OPJ_UINT32 flags_stride;
+} opj_t1_t;
+
+#define MACRO_t1_flags(x,y) t1->flags[((x)*(t1->flags_stride))+(y)]
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Encode the code-blocks of a tile
+@param t1 T1 handle
+@param tile The tile to encode
+@param tcp Tile coding parameters
+@param mct_norms FIXME DOC
+*/
+OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
+ opj_tcd_tile_t *tile,
+ opj_tcp_t *tcp,
+ const OPJ_FLOAT64 * mct_norms);
+
+/**
+Decode the code-blocks of a tile
+@param t1 T1 handle
+@param tilec The tile to decode
+@param tccp Tile coding parameters
+*/
+OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
+ opj_tcd_tilecomp_t* tilec,
+ opj_tccp_t* tccp);
+
+
+
+/**
+ * Creates a new Tier 1 handle
+ * and initializes the look-up tables of the Tier-1 coder/decoder
+ * @return a new T1 handle if successful, returns NULL otherwise
+*/
+opj_t1_t* opj_t1_create(void);
+
+/**
+ * Destroys a previously created T1 handle
+ *
+ * @param p_t1 Tier 1 handle to destroy
+*/
+void opj_t1_destroy(opj_t1_t *p_t1);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __T1_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_generate_luts.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_generate_luts.c
new file mode 100644
index 0000000000..1997d39977
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_generate_luts.c
@@ -0,0 +1,282 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+static int t1_init_ctxno_zc(int f, int orient) {
+ int h, v, d, n, t, hv;
+ n = 0;
+ h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
+ v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
+ d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
+
+ switch (orient) {
+ case 2:
+ t = h;
+ h = v;
+ v = t;
+ case 0:
+ case 1:
+ if (!h) {
+ if (!v) {
+ if (!d)
+ n = 0;
+ else if (d == 1)
+ n = 1;
+ else
+ n = 2;
+ } else if (v == 1) {
+ n = 3;
+ } else {
+ n = 4;
+ }
+ } else if (h == 1) {
+ if (!v) {
+ if (!d)
+ n = 5;
+ else
+ n = 6;
+ } else {
+ n = 7;
+ }
+ } else
+ n = 8;
+ break;
+ case 3:
+ hv = h + v;
+ if (!d) {
+ if (!hv) {
+ n = 0;
+ } else if (hv == 1) {
+ n = 1;
+ } else {
+ n = 2;
+ }
+ } else if (d == 1) {
+ if (!hv) {
+ n = 3;
+ } else if (hv == 1) {
+ n = 4;
+ } else {
+ n = 5;
+ }
+ } else if (d == 2) {
+ if (!hv) {
+ n = 6;
+ } else {
+ n = 7;
+ }
+ } else {
+ n = 8;
+ }
+ break;
+ }
+
+ return (T1_CTXNO_ZC + n);
+}
+
+static int t1_init_ctxno_sc(int f) {
+ int hc, vc, n;
+ n = 0;
+
+ hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+ T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+ 1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+ (T1_SIG_E | T1_SGN_E)) +
+ ((f & (T1_SIG_W | T1_SGN_W)) ==
+ (T1_SIG_W | T1_SGN_W)), 1);
+
+ vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+ T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+ 1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+ (T1_SIG_N | T1_SGN_N)) +
+ ((f & (T1_SIG_S | T1_SGN_S)) ==
+ (T1_SIG_S | T1_SGN_S)), 1);
+
+ if (hc < 0) {
+ hc = -hc;
+ vc = -vc;
+ }
+ if (!hc) {
+ if (vc == -1)
+ n = 1;
+ else if (!vc)
+ n = 0;
+ else
+ n = 1;
+ } else if (hc == 1) {
+ if (vc == -1)
+ n = 2;
+ else if (!vc)
+ n = 3;
+ else
+ n = 4;
+ }
+
+ return (T1_CTXNO_SC + n);
+}
+
+static int t1_init_spb(int f) {
+ int hc, vc, n;
+
+ hc = opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+ T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
+ 1) - opj_int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
+ (T1_SIG_E | T1_SGN_E)) +
+ ((f & (T1_SIG_W | T1_SGN_W)) ==
+ (T1_SIG_W | T1_SGN_W)), 1);
+
+ vc = opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+ T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
+ 1) - opj_int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
+ (T1_SIG_N | T1_SGN_N)) +
+ ((f & (T1_SIG_S | T1_SGN_S)) ==
+ (T1_SIG_S | T1_SGN_S)), 1);
+
+ if (!hc && !vc)
+ n = 0;
+ else
+ n = (!(hc > 0 || (!hc && vc > 0)));
+
+ return n;
+}
+
+static void dump_array16(int array[],int size){
+ int i;
+ --size;
+ for (i = 0; i < size; ++i) {
+ printf("0x%04x, ", array[i]);
+ if(!((i+1)&0x7))
+ printf("\n ");
+ }
+ printf("0x%04x\n};\n\n", array[size]);
+}
+
+int main(int argc, char **argv)
+{
+ int i, j;
+ double u, v, t;
+
+ int lut_ctxno_zc[1024];
+ int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
+ int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
+ int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
+ int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
+ (void)argc; (void)argv;
+
+ printf("/* This file was automatically generated by t1_generate_luts.c */\n\n");
+
+ /* lut_ctxno_zc */
+ for (j = 0; j < 4; ++j) {
+ for (i = 0; i < 256; ++i) {
+ int orient = j;
+ if (orient == 2) {
+ orient = 1;
+ } else if (orient == 1) {
+ orient = 2;
+ }
+ lut_ctxno_zc[(orient << 8) | i] = t1_init_ctxno_zc(i, j);
+ }
+ }
+
+ printf("static OPJ_BYTE lut_ctxno_zc[1024] = {\n ");
+ for (i = 0; i < 1023; ++i) {
+ printf("%i, ", lut_ctxno_zc[i]);
+ if(!((i+1)&0x1f))
+ printf("\n ");
+ }
+ printf("%i\n};\n\n", lut_ctxno_zc[1023]);
+
+ /* lut_ctxno_sc */
+ printf("static OPJ_BYTE lut_ctxno_sc[256] = {\n ");
+ for (i = 0; i < 255; ++i) {
+ printf("0x%x, ", t1_init_ctxno_sc(i << 4));
+ if(!((i+1)&0xf))
+ printf("\n ");
+ }
+ printf("0x%x\n};\n\n", t1_init_ctxno_sc(255 << 4));
+
+ /* lut_spb */
+ printf("static OPJ_BYTE lut_spb[256] = {\n ");
+ for (i = 0; i < 255; ++i) {
+ printf("%i, ", t1_init_spb(i << 4));
+ if(!((i+1)&0x1f))
+ printf("\n ");
+ }
+ printf("%i\n};\n\n", t1_init_spb(255 << 4));
+
+ /* FIXME FIXME FIXME */
+ /* fprintf(stdout,"nmsedec luts:\n"); */
+ for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) {
+ t = i / pow(2, T1_NMSEDEC_FRACBITS);
+ u = t;
+ v = t - 1.5;
+ lut_nmsedec_sig[i] =
+ opj_int_max(0,
+ (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+ lut_nmsedec_sig0[i] =
+ opj_int_max(0,
+ (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+ u = t - 1.0;
+ if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
+ v = t - 1.5;
+ } else {
+ v = t - 0.5;
+ }
+ lut_nmsedec_ref[i] =
+ opj_int_max(0,
+ (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+ lut_nmsedec_ref0[i] =
+ opj_int_max(0,
+ (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
+ }
+
+ printf("static OPJ_INT16 lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n ");
+ dump_array16(lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS);
+
+ printf("static OPJ_INT16 lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n ");
+ dump_array16(lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS);
+
+ printf("static OPJ_INT16 lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n ");
+ dump_array16(lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS);
+
+ printf("static OPJ_INT16 lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n ");
+ dump_array16(lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS);
+
+ return 0;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_luts.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_luts.h
new file mode 100644
index 0000000000..37776b65a1
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t1_luts.h
@@ -0,0 +1,143 @@
+/* This file was automatically generated by t1_generate_luts.c */
+
+static OPJ_BYTE lut_ctxno_zc[1024] = {
+ 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+ 0, 3, 3, 6, 3, 6, 6, 8, 3, 6, 6, 8, 6, 8, 8, 8, 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8,
+ 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8,
+ 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8
+};
+
+static OPJ_BYTE lut_ctxno_sc[256] = {
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xd, 0xc, 0xd, 0xd, 0xd, 0xd, 0xd,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xb, 0xc, 0xb, 0xd, 0xc, 0xd, 0xc,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xd, 0x9, 0xa, 0xd, 0xd, 0xa, 0xa,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xb, 0x9, 0xa, 0xd, 0xc, 0xa, 0x9,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xd, 0xc, 0xd, 0xb, 0xc, 0xb, 0xc,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xb, 0xc, 0xb, 0xb, 0xb, 0xb, 0xb,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xd, 0x9, 0xa, 0xb, 0xc, 0xa, 0x9,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xb, 0x9, 0xa, 0xb, 0xb, 0xa, 0xa,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xb, 0x9, 0xa, 0xb, 0xb, 0xa, 0xa,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xd, 0x9, 0xa, 0xb, 0xc, 0xa, 0x9,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xb, 0xc, 0xb, 0xb, 0xb, 0xb, 0xb,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xd, 0xc, 0xd, 0xb, 0xc, 0xb, 0xc,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xb, 0x9, 0xa, 0xd, 0xc, 0xa, 0x9,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xd, 0x9, 0xa, 0xd, 0xd, 0xa, 0xa,
+ 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xb, 0xc, 0xb, 0xd, 0xc, 0xd, 0xc,
+ 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xd, 0xc, 0xd, 0xd, 0xd, 0xd, 0xd
+};
+
+static OPJ_BYTE lut_spb[256] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0,
+ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0,
+ 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1,
+ 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+};
+
+static OPJ_INT16 lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0180, 0x0300, 0x0480, 0x0600, 0x0780, 0x0900, 0x0a80,
+ 0x0c00, 0x0d80, 0x0f00, 0x1080, 0x1200, 0x1380, 0x1500, 0x1680,
+ 0x1800, 0x1980, 0x1b00, 0x1c80, 0x1e00, 0x1f80, 0x2100, 0x2280,
+ 0x2400, 0x2580, 0x2700, 0x2880, 0x2a00, 0x2b80, 0x2d00, 0x2e80,
+ 0x3000, 0x3180, 0x3300, 0x3480, 0x3600, 0x3780, 0x3900, 0x3a80,
+ 0x3c00, 0x3d80, 0x3f00, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680,
+ 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5100, 0x5280,
+ 0x5400, 0x5580, 0x5700, 0x5880, 0x5a00, 0x5b80, 0x5d00, 0x5e80,
+ 0x6000, 0x6180, 0x6300, 0x6480, 0x6600, 0x6780, 0x6900, 0x6a80,
+ 0x6c00, 0x6d80, 0x6f00, 0x7080, 0x7200, 0x7380, 0x7500, 0x7680
+};
+
+static OPJ_INT16 lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080,
+ 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200,
+ 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400,
+ 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780,
+ 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00,
+ 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180,
+ 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780,
+ 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00,
+ 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2680, 0x2780,
+ 0x2880, 0x2980, 0x2b00, 0x2c00, 0x2d00, 0x2e80, 0x2f80, 0x3100,
+ 0x3200, 0x3380, 0x3480, 0x3600, 0x3700, 0x3880, 0x3a00, 0x3b00,
+ 0x3c80, 0x3e00, 0x3f80, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680,
+ 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5180, 0x5300,
+ 0x5480, 0x5600, 0x5800, 0x5980, 0x5b00, 0x5d00, 0x5e80, 0x6080,
+ 0x6200, 0x6400, 0x6580, 0x6780, 0x6900, 0x6b00, 0x6d00, 0x6e80,
+ 0x7080, 0x7280, 0x7480, 0x7600, 0x7800, 0x7a00, 0x7c00, 0x7e00
+};
+
+static OPJ_INT16 lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {
+ 0x1800, 0x1780, 0x1700, 0x1680, 0x1600, 0x1580, 0x1500, 0x1480,
+ 0x1400, 0x1380, 0x1300, 0x1280, 0x1200, 0x1180, 0x1100, 0x1080,
+ 0x1000, 0x0f80, 0x0f00, 0x0e80, 0x0e00, 0x0d80, 0x0d00, 0x0c80,
+ 0x0c00, 0x0b80, 0x0b00, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880,
+ 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0500, 0x0480,
+ 0x0400, 0x0380, 0x0300, 0x0280, 0x0200, 0x0180, 0x0100, 0x0080,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380,
+ 0x0400, 0x0480, 0x0500, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780,
+ 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b00, 0x0b80,
+ 0x0c00, 0x0c80, 0x0d00, 0x0d80, 0x0e00, 0x0e80, 0x0f00, 0x0f80,
+ 0x1000, 0x1080, 0x1100, 0x1180, 0x1200, 0x1280, 0x1300, 0x1380,
+ 0x1400, 0x1480, 0x1500, 0x1580, 0x1600, 0x1680, 0x1700, 0x1780
+};
+
+static OPJ_INT16 lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {
+ 0x2000, 0x1f00, 0x1e00, 0x1d00, 0x1c00, 0x1b00, 0x1a80, 0x1980,
+ 0x1880, 0x1780, 0x1700, 0x1600, 0x1500, 0x1480, 0x1380, 0x1300,
+ 0x1200, 0x1180, 0x1080, 0x1000, 0x0f00, 0x0e80, 0x0e00, 0x0d00,
+ 0x0c80, 0x0c00, 0x0b80, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880,
+ 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0580, 0x0500,
+ 0x0480, 0x0400, 0x0400, 0x0380, 0x0300, 0x0300, 0x0280, 0x0280,
+ 0x0200, 0x0200, 0x0180, 0x0180, 0x0100, 0x0100, 0x0100, 0x0080,
+ 0x0080, 0x0080, 0x0080, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080,
+ 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200,
+ 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400,
+ 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780,
+ 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00,
+ 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180,
+ 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780,
+ 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00
+};
+
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.c
new file mode 100644
index 0000000000..de054acc54
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.c
@@ -0,0 +1,1344 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup T2 T2 - Implementation of a tier-2 coding */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
+
+static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
+/**
+Variable length code for signalling delta Zil (truncation point)
+@param bio Bit Input/Output component
+@param n delta Zil
+*/
+static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
+static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
+
+/**
+Encode a packet of a tile to a destination buffer
+@param tileno Number of the tile encoded
+@param tile Tile for which to write the packets
+@param tcp Tile coding parameters
+@param pi Packet identity
+@param dest Destination buffer
+@param p_data_written FIXME DOC
+@param len Length of the destination buffer
+@param cstr_info Codestream information structure
+@return
+*/
+static OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno,
+ opj_tcd_tile_t *tile,
+ opj_tcp_t *tcp,
+ opj_pi_iterator_t *pi,
+ OPJ_BYTE *dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 len,
+ opj_codestream_info_t *cstr_info);
+
+/**
+Decode a packet of a tile from a source buffer
+@param t2 T2 handle
+@param tile Tile for which to write the packets
+@param tcp Tile coding parameters
+@param pi Packet identity
+@param src Source buffer
+@param data_read FIXME DOC
+@param max_length FIXME DOC
+@param pack_info Packet information
+
+@return FIXME DOC
+*/
+static OPJ_BOOL opj_t2_decode_packet( opj_t2_t* t2,
+ opj_tcd_tile_t *tile,
+ opj_tcp_t *tcp,
+ opj_pi_iterator_t *pi,
+ OPJ_BYTE *src,
+ OPJ_UINT32 * data_read,
+ OPJ_UINT32 max_length,
+ opj_packet_info_t *pack_info);
+
+static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_tcp_t *p_tcp,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BYTE *p_src,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *p_pack_info);
+
+static OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_tcp_t *p_tcp,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BOOL * p_is_data_present,
+ OPJ_BYTE *p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *p_pack_info);
+
+static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BYTE *p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *pack_info);
+
+static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_pi_iterator_t *p_pi,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *pack_info);
+
+/**
+@param cblk
+@param index
+@param cblksty
+@param first
+*/
+static OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
+ OPJ_UINT32 index,
+ OPJ_UINT32 cblksty,
+ OPJ_UINT32 first);
+
+/*@}*/
+
+/*@}*/
+
+/* ----------------------------------------------------------------------- */
+
+/* #define RESTART 0x04 */
+static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) {
+ while (--n >= 0) {
+ opj_bio_write(bio, 1, 1);
+ }
+ opj_bio_write(bio, 0, 1);
+}
+
+OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio)
+{
+ OPJ_UINT32 n = 0;
+ while (opj_bio_read(bio, 1)) {
+ ++n;
+ }
+ return n;
+}
+
+void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
+ if (n == 1) {
+ opj_bio_write(bio, 0, 1);
+ } else if (n == 2) {
+ opj_bio_write(bio, 2, 2);
+ } else if (n <= 5) {
+ opj_bio_write(bio, 0xc | (n - 3), 4);
+ } else if (n <= 36) {
+ opj_bio_write(bio, 0x1e0 | (n - 6), 9);
+ } else if (n <= 164) {
+ opj_bio_write(bio, 0xff80 | (n - 37), 16);
+ }
+}
+
+OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) {
+ OPJ_UINT32 n;
+ if (!opj_bio_read(bio, 1))
+ return 1;
+ if (!opj_bio_read(bio, 1))
+ return 2;
+ if ((n = opj_bio_read(bio, 2)) != 3)
+ return (3 + n);
+ if ((n = opj_bio_read(bio, 5)) != 31)
+ return (6 + n);
+ return (37 + opj_bio_read(bio, 7));
+}
+
+/* ----------------------------------------------------------------------- */
+
+OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2,
+ OPJ_UINT32 p_tile_no,
+ opj_tcd_tile_t *p_tile,
+ OPJ_UINT32 p_maxlayers,
+ OPJ_BYTE *p_dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_max_len,
+ opj_codestream_info_t *cstr_info,
+ OPJ_UINT32 p_tp_num,
+ OPJ_INT32 p_tp_pos,
+ OPJ_UINT32 p_pino,
+ J2K_T2_MODE p_t2_mode)
+{
+ OPJ_BYTE *l_current_data = p_dest;
+ OPJ_UINT32 l_nb_bytes = 0;
+ OPJ_UINT32 compno;
+ OPJ_UINT32 poc;
+ opj_pi_iterator_t *l_pi = 00;
+ opj_pi_iterator_t *l_current_pi = 00;
+ opj_image_t *l_image = p_t2->image;
+ opj_cp_t *l_cp = p_t2->cp;
+ opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
+ OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24? 2: 1;
+ OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
+ OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
+
+ l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
+ if (!l_pi) {
+ return OPJ_FALSE;
+ }
+
+ * p_data_written = 0;
+
+ if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
+ l_current_pi = l_pi;
+
+ for (compno = 0; compno < l_max_comp; ++compno) {
+ OPJ_UINT32 l_comp_len = 0;
+ l_current_pi = l_pi;
+
+ for (poc = 0; poc < pocno ; ++poc) {
+ OPJ_UINT32 l_tp_num = compno;
+
+ /* TODO MSD : check why this function cannot fail (cf. v1) */
+ opj_pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
+
+ while (opj_pi_next(l_current_pi)) {
+ if (l_current_pi->layno < p_maxlayers) {
+ l_nb_bytes = 0;
+
+ if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
+ opj_pi_destroy(l_pi, l_nb_pocs);
+ return OPJ_FALSE;
+ }
+
+ l_comp_len += l_nb_bytes;
+ l_current_data += l_nb_bytes;
+ p_max_len -= l_nb_bytes;
+
+ * p_data_written += l_nb_bytes;
+ }
+ }
+
+ if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
+ if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
+ opj_pi_destroy(l_pi, l_nb_pocs);
+ return OPJ_FALSE;
+ }
+ }
+
+ ++l_current_pi;
+ }
+ }
+ }
+ else { /* t2_mode == FINAL_PASS */
+ opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
+
+ l_current_pi = &l_pi[p_pino];
+
+ while (opj_pi_next(l_current_pi)) {
+ if (l_current_pi->layno < p_maxlayers) {
+ l_nb_bytes=0;
+
+ if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
+ opj_pi_destroy(l_pi, l_nb_pocs);
+ return OPJ_FALSE;
+ }
+
+ l_current_data += l_nb_bytes;
+ p_max_len -= l_nb_bytes;
+
+ * p_data_written += l_nb_bytes;
+
+ /* INDEX >> */
+ if(cstr_info) {
+ if(cstr_info->index_write) {
+ opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
+ opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
+ if (!cstr_info->packno) {
+ info_PK->start_pos = info_TL->end_header + 1;
+ } else {
+ info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
+ }
+ info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
+ info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance
+ to start of packet is incremented by value of start of packet*/
+ }
+
+ cstr_info->packno++;
+ }
+ /* << INDEX */
+ ++p_tile->packno;
+ }
+ }
+ }
+
+ opj_pi_destroy(l_pi, l_nb_pocs);
+
+ return OPJ_TRUE;
+}
+
+/* see issue 80 */
+#if 0
+#define JAS_FPRINTF fprintf
+#else
+/* issue 290 */
+static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
+{
+ (void)file;
+ (void)format;
+}
+#define JAS_FPRINTF opj_null_jas_fprintf
+#endif
+
+OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2,
+ OPJ_UINT32 p_tile_no,
+ opj_tcd_tile_t *p_tile,
+ OPJ_BYTE *p_src,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_len,
+ opj_codestream_index_t *p_cstr_index)
+{
+ OPJ_BYTE *l_current_data = p_src;
+ opj_pi_iterator_t *l_pi = 00;
+ OPJ_UINT32 pino;
+ opj_image_t *l_image = p_t2->image;
+ opj_cp_t *l_cp = p_t2->cp;
+ opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
+ OPJ_UINT32 l_nb_bytes_read;
+ OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
+ opj_pi_iterator_t *l_current_pi = 00;
+#ifdef TODO_MSD
+ OPJ_UINT32 curtp = 0;
+ OPJ_UINT32 tp_start_packno;
+#endif
+ opj_packet_info_t *l_pack_info = 00;
+ opj_image_comp_t* l_img_comp = 00;
+
+ OPJ_ARG_NOT_USED(p_cstr_index);
+
+#ifdef TODO_MSD
+ if (p_cstr_index) {
+ l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
+ }
+#endif
+
+ /* create a packet iterator */
+ l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
+ if (!l_pi) {
+ return OPJ_FALSE;
+ }
+
+
+ l_current_pi = l_pi;
+
+ for (pino = 0; pino <= l_tcp->numpocs; ++pino) {
+
+ /* if the resolution needed is too low, one dim of the tilec could be equal to zero
+ * and no packets are used to decode this resolution and
+ * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
+ * and no l_img_comp->resno_decoded are computed
+ */
+ OPJ_BOOL* first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
+ if (!first_pass_failed)
+ {
+ opj_pi_destroy(l_pi,l_nb_pocs);
+ return OPJ_FALSE;
+ }
+ memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
+
+ while (opj_pi_next(l_current_pi)) {
+ JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
+ l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
+
+ if (l_tcp->num_layers_to_decode > l_current_pi->layno
+ && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
+ l_nb_bytes_read = 0;
+
+ first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
+
+ if (! opj_t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
+ opj_pi_destroy(l_pi,l_nb_pocs);
+ opj_free(first_pass_failed);
+ return OPJ_FALSE;
+ }
+
+ l_img_comp = &(l_image->comps[l_current_pi->compno]);
+ l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
+ }
+ else {
+ l_nb_bytes_read = 0;
+ if (! opj_t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
+ opj_pi_destroy(l_pi,l_nb_pocs);
+ opj_free(first_pass_failed);
+ return OPJ_FALSE;
+ }
+ }
+
+ if (first_pass_failed[l_current_pi->compno]) {
+ l_img_comp = &(l_image->comps[l_current_pi->compno]);
+ if (l_img_comp->resno_decoded == 0)
+ l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
+ }
+
+ l_current_data += l_nb_bytes_read;
+ p_max_len -= l_nb_bytes_read;
+
+ /* INDEX >> */
+#ifdef TODO_MSD
+ if(p_cstr_info) {
+ opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
+ opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
+ tp_start_packno = 0;
+ if (!p_cstr_info->packno) {
+ info_PK->start_pos = info_TL->end_header + 1;
+ } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */
+ info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
+ tp_start_packno = p_cstr_info->packno;
+ curtp++;
+ info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
+ } else {
+ info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1;
+ }
+ info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
+ info_PK->end_ph_pos += info_PK->start_pos - 1; /* End of packet header which now only represents the distance */
+ ++p_cstr_info->packno;
+ }
+#endif
+ /* << INDEX */
+ }
+ ++l_current_pi;
+
+ opj_free(first_pass_failed);
+ }
+ /* INDEX >> */
+#ifdef TODO_MSD
+ if
+ (p_cstr_info) {
+ p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */
+ }
+#endif
+ /* << INDEX */
+
+ /* don't forget to release pi */
+ opj_pi_destroy(l_pi,l_nb_pocs);
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+
+/**
+ * Creates a Tier 2 handle
+ *
+ * @param p_image Source or destination image
+ * @param p_cp Image coding parameters.
+ * @return a new T2 handle if successful, NULL otherwise.
+*/
+opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
+{
+ /* create the t2 structure */
+ opj_t2_t *l_t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
+ if (!l_t2) {
+ return NULL;
+ }
+ memset(l_t2,0,sizeof(opj_t2_t));
+
+ l_t2->image = p_image;
+ l_t2->cp = p_cp;
+
+ return l_t2;
+}
+
+void opj_t2_destroy(opj_t2_t *t2) {
+ if(t2) {
+ opj_free(t2);
+ }
+}
+
+OPJ_BOOL opj_t2_decode_packet( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_tcp_t *p_tcp,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BYTE *p_src,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *p_pack_info)
+{
+ OPJ_BOOL l_read_data;
+ OPJ_UINT32 l_nb_bytes_read = 0;
+ OPJ_UINT32 l_nb_total_bytes_read = 0;
+
+ *p_data_read = 0;
+
+ if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
+ return OPJ_FALSE;
+ }
+
+ p_src += l_nb_bytes_read;
+ l_nb_total_bytes_read += l_nb_bytes_read;
+ p_max_length -= l_nb_bytes_read;
+
+ /* we should read data for the packet */
+ if (l_read_data) {
+ l_nb_bytes_read = 0;
+
+ if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_total_bytes_read += l_nb_bytes_read;
+ }
+
+ *p_data_read = l_nb_total_bytes_read;
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_t2_encode_packet( OPJ_UINT32 tileno,
+ opj_tcd_tile_t * tile,
+ opj_tcp_t * tcp,
+ opj_pi_iterator_t *pi,
+ OPJ_BYTE *dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 length,
+ opj_codestream_info_t *cstr_info)
+{
+ OPJ_UINT32 bandno, cblkno;
+ OPJ_BYTE* c = dest;
+ OPJ_UINT32 l_nb_bytes;
+ OPJ_UINT32 compno = pi->compno; /* component value */
+ OPJ_UINT32 resno = pi->resno; /* resolution level value */
+ OPJ_UINT32 precno = pi->precno; /* precinct value */
+ OPJ_UINT32 layno = pi->layno; /* quality layer value */
+ OPJ_UINT32 l_nb_blocks;
+ opj_tcd_band_t *band = 00;
+ opj_tcd_cblk_enc_t* cblk = 00;
+ opj_tcd_pass_t *pass = 00;
+
+ opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ opj_bio_t *bio = 00; /* BIO component */
+
+ /* <SOP 0xff91> */
+ if (tcp->csty & J2K_CP_CSTY_SOP) {
+ c[0] = 255;
+ c[1] = 145;
+ c[2] = 0;
+ c[3] = 4;
+#if 0
+ c[4] = (tile->packno % 65536) / 256;
+ c[5] = (tile->packno % 65536) % 256;
+#else
+ c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
+ c[5] = tile->packno & 0xff;
+#endif
+ c += 6;
+ length -= 6;
+ }
+ /* </SOP> */
+
+ if (!layno) {
+ band = res->bands;
+
+ for(bandno = 0; bandno < res->numbands; ++bandno) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ opj_tgt_reset(prc->incltree);
+ opj_tgt_reset(prc->imsbtree);
+
+ l_nb_blocks = prc->cw * prc->ch;
+ for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
+ cblk = &prc->cblks.enc[cblkno];
+
+ cblk->numpasses = 0;
+ opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
+ }
+ ++band;
+ }
+ }
+
+ bio = opj_bio_create();
+ opj_bio_init_enc(bio, c, length);
+ opj_bio_write(bio, 1, 1); /* Empty header bit */
+
+ /* Writing Packet header */
+ band = res->bands;
+ for (bandno = 0; bandno < res->numbands; ++bandno) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ l_nb_blocks = prc->cw * prc->ch;
+ cblk = prc->cblks.enc;
+
+ for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
+ opj_tcd_layer_t *layer = &cblk->layers[layno];
+
+ if (!cblk->numpasses && layer->numpasses) {
+ opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
+ }
+
+ ++cblk;
+ }
+
+ cblk = prc->cblks.enc;
+ for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
+ opj_tcd_layer_t *layer = &cblk->layers[layno];
+ OPJ_UINT32 increment = 0;
+ OPJ_UINT32 nump = 0;
+ OPJ_UINT32 len = 0, passno;
+ OPJ_UINT32 l_nb_passes;
+
+ /* cblk inclusion bits */
+ if (!cblk->numpasses) {
+ opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
+ } else {
+ opj_bio_write(bio, layer->numpasses != 0, 1);
+ }
+
+ /* if cblk not included, go to the next cblk */
+ if (!layer->numpasses) {
+ ++cblk;
+ continue;
+ }
+
+ /* if first instance of cblk --> zero bit-planes information */
+ if (!cblk->numpasses) {
+ cblk->numlenbits = 3;
+ opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
+ }
+
+ /* number of coding passes included */
+ opj_t2_putnumpasses(bio, layer->numpasses);
+ l_nb_passes = cblk->numpasses + layer->numpasses;
+ pass = cblk->passes + cblk->numpasses;
+
+ /* computation of the increase of the length indicator and insertion in the header */
+ for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
+ ++nump;
+ len += pass->len;
+
+ if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+ increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
+ - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
+ len = 0;
+ nump = 0;
+ }
+
+ ++pass;
+ }
+ opj_t2_putcommacode(bio, (OPJ_INT32)increment);
+
+ /* computation of the new Length indicator */
+ cblk->numlenbits += increment;
+
+ pass = cblk->passes + cblk->numpasses;
+ /* insertion of the codeword segment length */
+ for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
+ nump++;
+ len += pass->len;
+
+ if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
+ opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
+ len = 0;
+ nump = 0;
+ }
+ ++pass;
+ }
+
+ ++cblk;
+ }
+
+ ++band;
+ }
+
+ if (!opj_bio_flush(bio)) {
+ opj_bio_destroy(bio);
+ return OPJ_FALSE; /* modified to eliminate longjmp !! */
+ }
+
+ l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
+ c += l_nb_bytes;
+ length -= l_nb_bytes;
+
+ opj_bio_destroy(bio);
+
+ /* <EPH 0xff92> */
+ if (tcp->csty & J2K_CP_CSTY_EPH) {
+ c[0] = 255;
+ c[1] = 146;
+ c += 2;
+ length -= 2;
+ }
+ /* </EPH> */
+
+ /* << INDEX */
+ /* End of packet header position. Currently only represents the distance to start of packet
+ Will be updated later by incrementing with packet start value*/
+ if(cstr_info && cstr_info->index_write) {
+ opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
+ info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
+ }
+ /* INDEX >> */
+
+ /* Writing the packet body */
+ band = res->bands;
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ l_nb_blocks = prc->cw * prc->ch;
+ cblk = prc->cblks.enc;
+
+ for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
+ opj_tcd_layer_t *layer = &cblk->layers[layno];
+
+ if (!layer->numpasses) {
+ ++cblk;
+ continue;
+ }
+
+ if (layer->len > length) {
+ return OPJ_FALSE;
+ }
+
+ memcpy(c, layer->data, layer->len);
+ cblk->numpasses += layer->numpasses;
+ c += layer->len;
+ length -= layer->len;
+
+ /* << INDEX */
+ if(cstr_info && cstr_info->index_write) {
+ opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
+ info_PK->disto += layer->disto;
+ if (cstr_info->D_max < info_PK->disto) {
+ cstr_info->D_max = info_PK->disto;
+ }
+ }
+
+ ++cblk;
+ /* INDEX >> */
+ }
+ ++band;
+ }
+
+ assert( c >= dest );
+ * p_data_written += (OPJ_UINT32)(c - dest);
+
+ return OPJ_TRUE;
+}
+
+static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_tcp_t *p_tcp,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BYTE *p_src,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *p_pack_info)
+{
+ OPJ_BOOL l_read_data;
+ OPJ_UINT32 l_nb_bytes_read = 0;
+ OPJ_UINT32 l_nb_total_bytes_read = 0;
+
+ *p_data_read = 0;
+
+ if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
+ return OPJ_FALSE;
+ }
+
+ p_src += l_nb_bytes_read;
+ l_nb_total_bytes_read += l_nb_bytes_read;
+ p_max_length -= l_nb_bytes_read;
+
+ /* we should read data for the packet */
+ if (l_read_data) {
+ l_nb_bytes_read = 0;
+
+ if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
+ return OPJ_FALSE;
+ }
+
+ l_nb_total_bytes_read += l_nb_bytes_read;
+ }
+ *p_data_read = l_nb_total_bytes_read;
+
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_tcp_t *p_tcp,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BOOL * p_is_data_present,
+ OPJ_BYTE *p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *p_pack_info)
+
+{
+ /* loop */
+ OPJ_UINT32 bandno, cblkno;
+ OPJ_UINT32 l_nb_code_blocks;
+ OPJ_UINT32 l_remaining_length;
+ OPJ_UINT32 l_header_length;
+ OPJ_UINT32 * l_modified_length_ptr = 00;
+ OPJ_BYTE *l_current_data = p_src_data;
+ opj_cp_t *l_cp = p_t2->cp;
+ opj_bio_t *l_bio = 00; /* BIO component */
+ opj_tcd_band_t *l_band = 00;
+ opj_tcd_cblk_dec_t* l_cblk = 00;
+ opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
+
+ OPJ_BYTE *l_header_data = 00;
+ OPJ_BYTE **l_header_data_start = 00;
+
+ OPJ_UINT32 l_present;
+
+ if (p_pi->layno == 0) {
+ l_band = l_res->bands;
+
+ /* reset tagtrees */
+ for (bandno = 0; bandno < l_res->numbands; ++bandno) {
+ opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
+
+ if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
+ opj_tgt_reset(l_prc->incltree);
+ opj_tgt_reset(l_prc->imsbtree);
+ l_cblk = l_prc->cblks.dec;
+
+ l_nb_code_blocks = l_prc->cw * l_prc->ch;
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
+ l_cblk->numsegs = 0;
+ l_cblk->real_num_segs = 0;
+ ++l_cblk;
+ }
+ }
+
+ ++l_band;
+ }
+ }
+
+ /* SOP markers */
+
+ if (p_tcp->csty & J2K_CP_CSTY_SOP) {
+ if (p_max_length < 6) {
+ /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Not enough space for expected SOP marker\n"); */
+ printf("Not enough space for expected SOP marker\n");
+ } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
+ /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
+ printf("Expected SOP marker\n");
+ fprintf(stderr, "Error : expected SOP marker\n");
+ } else {
+ l_current_data += 6;
+ }
+
+ /** TODO : check the Nsop value */
+ }
+
+ /*
+ When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
+ This part deal with this caracteristic
+ step 1: Read packet header in the saved structure
+ step 2: Return to codestream for decoding
+ */
+
+ l_bio = opj_bio_create();
+ if (! l_bio) {
+ return OPJ_FALSE;
+ }
+
+ if (l_cp->ppm == 1) { /* PPM */
+ l_header_data_start = &l_cp->ppm_data;
+ l_header_data = *l_header_data_start;
+ l_modified_length_ptr = &(l_cp->ppm_len);
+
+ }
+ else if (p_tcp->ppt == 1) { /* PPT */
+ l_header_data_start = &(p_tcp->ppt_data);
+ l_header_data = *l_header_data_start;
+ l_modified_length_ptr = &(p_tcp->ppt_len);
+ }
+ else { /* Normal Case */
+ l_header_data_start = &(l_current_data);
+ l_header_data = *l_header_data_start;
+ l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
+ l_modified_length_ptr = &(l_remaining_length);
+ }
+
+ opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
+
+ l_present = opj_bio_read(l_bio, 1);
+ JAS_FPRINTF(stderr, "present=%d \n", l_present );
+ if (!l_present) {
+ /* TODO MSD: no test to control the output of this function*/
+ opj_bio_inalign(l_bio);
+ l_header_data += opj_bio_numbytes(l_bio);
+ opj_bio_destroy(l_bio);
+
+ /* EPH markers */
+ if (p_tcp->csty & J2K_CP_CSTY_EPH) {
+ if (p_max_length < 2) {
+ fprintf(stderr, "Not enough space for expected EPH marker\n");
+ } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
+ fprintf(stderr, "Error : expected EPH marker\n");
+ } else {
+ l_header_data += 2;
+ }
+ }
+
+ l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
+ *l_modified_length_ptr -= l_header_length;
+ *l_header_data_start += l_header_length;
+
+ /* << INDEX */
+ /* End of packet header position. Currently only represents the distance to start of packet
+ Will be updated later by incrementing with packet start value */
+ if (p_pack_info) {
+ p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
+ }
+ /* INDEX >> */
+
+ * p_is_data_present = OPJ_FALSE;
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
+ return OPJ_TRUE;
+ }
+
+ l_band = l_res->bands;
+ for (bandno = 0; bandno < l_res->numbands; ++bandno) {
+ opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
+
+ if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
+ ++l_band;
+ continue;
+ }
+
+ l_nb_code_blocks = l_prc->cw * l_prc->ch;
+ l_cblk = l_prc->cblks.dec;
+ for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
+ OPJ_UINT32 l_included,l_increment, l_segno;
+ OPJ_INT32 n;
+
+ /* if cblk not yet included before --> inclusion tagtree */
+ if (!l_cblk->numsegs) {
+ l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
+ /* else one bit */
+ }
+ else {
+ l_included = opj_bio_read(l_bio, 1);
+ }
+
+ /* if cblk not included */
+ if (!l_included) {
+ l_cblk->numnewpasses = 0;
+ ++l_cblk;
+ JAS_FPRINTF(stderr, "included=%d \n", l_included);
+ continue;
+ }
+
+ /* if cblk not yet included --> zero-bitplane tagtree */
+ if (!l_cblk->numsegs) {
+ OPJ_UINT32 i = 0;
+
+ while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
+ ++i;
+ }
+
+ l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
+ l_cblk->numlenbits = 3;
+ }
+
+ /* number of coding passes */
+ l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
+ l_increment = opj_t2_getcommacode(l_bio);
+
+ /* length indicator increment */
+ l_cblk->numlenbits += l_increment;
+ l_segno = 0;
+
+ if (!l_cblk->numsegs) {
+ if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
+ opj_bio_destroy(l_bio);
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ l_segno = l_cblk->numsegs - 1;
+ if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
+ ++l_segno;
+ if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
+ opj_bio_destroy(l_bio);
+ return OPJ_FALSE;
+ }
+ }
+ }
+ n = (OPJ_INT32)l_cblk->numnewpasses;
+
+ do {
+ l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
+ l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
+ JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, l_cblk->segs[l_segno].newlen );
+
+ n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
+ if (n > 0) {
+ ++l_segno;
+
+ if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
+ opj_bio_destroy(l_bio);
+ return OPJ_FALSE;
+ }
+ }
+ } while (n > 0);
+
+ ++l_cblk;
+ }
+
+ ++l_band;
+ }
+
+ if (!opj_bio_inalign(l_bio)) {
+ opj_bio_destroy(l_bio);
+ return OPJ_FALSE;
+ }
+
+ l_header_data += opj_bio_numbytes(l_bio);
+ opj_bio_destroy(l_bio);
+
+ /* EPH markers */
+ if (p_tcp->csty & J2K_CP_CSTY_EPH) {
+ if (p_max_length < 2) {
+ fprintf(stderr, "Not enough space for expected EPH marker\n");
+ } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
+ /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
+ fprintf(stderr, "Error : expected EPH marker\n");
+ } else {
+ l_header_data += 2;
+ }
+ }
+
+ l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
+ JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
+ JAS_FPRINTF( stderr, "packet body\n");
+ *l_modified_length_ptr -= l_header_length;
+ *l_header_data_start += l_header_length;
+
+ /* << INDEX */
+ /* End of packet header position. Currently only represents the distance to start of packet
+ Will be updated later by incrementing with packet start value */
+ if (p_pack_info) {
+ p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
+ }
+ /* INDEX >> */
+
+ *p_is_data_present = OPJ_TRUE;
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_t2_read_packet_data( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_pi_iterator_t *p_pi,
+ OPJ_BYTE *p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *pack_info)
+{
+ OPJ_UINT32 bandno, cblkno;
+ OPJ_UINT32 l_nb_code_blocks;
+ OPJ_BYTE *l_current_data = p_src_data;
+ opj_tcd_band_t *l_band = 00;
+ opj_tcd_cblk_dec_t* l_cblk = 00;
+ opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
+
+ OPJ_ARG_NOT_USED(p_t2);
+ OPJ_ARG_NOT_USED(pack_info);
+
+ l_band = l_res->bands;
+ for (bandno = 0; bandno < l_res->numbands; ++bandno) {
+ opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
+
+ if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
+ ++l_band;
+ continue;
+ }
+
+ l_nb_code_blocks = l_prc->cw * l_prc->ch;
+ l_cblk = l_prc->cblks.dec;
+
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
+ opj_tcd_seg_t *l_seg = 00;
+
+ if (!l_cblk->numnewpasses) {
+ /* nothing to do */
+ ++l_cblk;
+ continue;
+ }
+
+ if (!l_cblk->numsegs) {
+ l_seg = l_cblk->segs;
+ ++l_cblk->numsegs;
+ l_cblk->data_current_size = 0;
+ }
+ else {
+ l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
+
+ if (l_seg->numpasses == l_seg->maxpasses) {
+ ++l_seg;
+ ++l_cblk->numsegs;
+ }
+ }
+
+ do {
+ if (l_current_data + l_seg->newlen > p_src_data + p_max_length) {
+ fprintf(stderr, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
+ l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
+ return OPJ_FALSE;
+ }
+
+#ifdef USE_JPWL
+ /* we need here a j2k handle to verify if making a check to
+ the validity of cblocks parameters is selected from user (-W) */
+
+ /* let's check that we are not exceeding */
+ if ((l_cblk->len + l_seg->newlen) > 8192) {
+ opj_event_msg(p_t2->cinfo, EVT_WARNING,
+ "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
+ l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ l_seg->newlen = 8192 - l_cblk->len;
+ opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
+ break;
+ };
+
+#endif /* USE_JPWL */
+ /* Check if the cblk->data have allocated enough memory */
+ if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
+ OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
+ if(! new_cblk_data) {
+ opj_free(l_cblk->data);
+ l_cblk->data_max_size = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
+ return OPJ_FALSE;
+ }
+ l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
+ l_cblk->data = new_cblk_data;
+ }
+
+ memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
+
+ if (l_seg->numpasses == 0) {
+ l_seg->data = &l_cblk->data;
+ l_seg->dataindex = l_cblk->data_current_size;
+ }
+
+ l_current_data += l_seg->newlen;
+ l_seg->numpasses += l_seg->numnewpasses;
+ l_cblk->numnewpasses -= l_seg->numnewpasses;
+
+ l_seg->real_num_passes = l_seg->numpasses;
+ l_cblk->data_current_size += l_seg->newlen;
+ l_seg->len += l_seg->newlen;
+
+ if (l_cblk->numnewpasses > 0) {
+ ++l_seg;
+ ++l_cblk->numsegs;
+ }
+ } while (l_cblk->numnewpasses > 0);
+
+ l_cblk->real_num_segs = l_cblk->numsegs;
+ ++l_cblk;
+ } /* next code_block */
+
+ ++l_band;
+ }
+
+ *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_t2_skip_packet_data( opj_t2_t* p_t2,
+ opj_tcd_tile_t *p_tile,
+ opj_pi_iterator_t *p_pi,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_length,
+ opj_packet_info_t *pack_info)
+{
+ OPJ_UINT32 bandno, cblkno;
+ OPJ_UINT32 l_nb_code_blocks;
+ opj_tcd_band_t *l_band = 00;
+ opj_tcd_cblk_dec_t* l_cblk = 00;
+ opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
+
+ OPJ_ARG_NOT_USED(p_t2);
+ OPJ_ARG_NOT_USED(pack_info);
+
+ *p_data_read = 0;
+ l_band = l_res->bands;
+
+ for (bandno = 0; bandno < l_res->numbands; ++bandno) {
+ opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
+
+ if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
+ ++l_band;
+ continue;
+ }
+
+ l_nb_code_blocks = l_prc->cw * l_prc->ch;
+ l_cblk = l_prc->cblks.dec;
+
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
+ opj_tcd_seg_t *l_seg = 00;
+
+ if (!l_cblk->numnewpasses) {
+ /* nothing to do */
+ ++l_cblk;
+ continue;
+ }
+
+ if (!l_cblk->numsegs) {
+ l_seg = l_cblk->segs;
+ ++l_cblk->numsegs;
+ l_cblk->data_current_size = 0;
+ }
+ else {
+ l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
+
+ if (l_seg->numpasses == l_seg->maxpasses) {
+ ++l_seg;
+ ++l_cblk->numsegs;
+ }
+ }
+
+ do {
+ if (* p_data_read + l_seg->newlen > p_max_length) {
+ fprintf(stderr, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
+ l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
+ return OPJ_FALSE;
+ }
+
+#ifdef USE_JPWL
+ /* we need here a j2k handle to verify if making a check to
+ the validity of cblocks parameters is selected from user (-W) */
+
+ /* let's check that we are not exceeding */
+ if ((l_cblk->len + l_seg->newlen) > 8192) {
+ opj_event_msg(p_t2->cinfo, EVT_WARNING,
+ "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
+ l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return -999;
+ }
+ l_seg->newlen = 8192 - l_cblk->len;
+ opj_event_msg(p_t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", l_seg->newlen);
+ break;
+ };
+
+#endif /* USE_JPWL */
+ JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
+ *(p_data_read) += l_seg->newlen;
+
+ l_seg->numpasses += l_seg->numnewpasses;
+ l_cblk->numnewpasses -= l_seg->numnewpasses;
+ if (l_cblk->numnewpasses > 0)
+ {
+ ++l_seg;
+ ++l_cblk->numsegs;
+ }
+ } while (l_cblk->numnewpasses > 0);
+
+ ++l_cblk;
+ }
+
+ ++l_band;
+ }
+
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_t2_init_seg( opj_tcd_cblk_dec_t* cblk,
+ OPJ_UINT32 index,
+ OPJ_UINT32 cblksty,
+ OPJ_UINT32 first)
+{
+ opj_tcd_seg_t* seg = 00;
+ OPJ_UINT32 l_nb_segs = index + 1;
+
+ if (l_nb_segs > cblk->m_current_max_segs) {
+ opj_tcd_seg_t* new_segs;
+ cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
+
+ new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
+ if(! new_segs) {
+ opj_free(cblk->segs);
+ cblk->segs = NULL;
+ cblk->m_current_max_segs = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
+ return OPJ_FALSE;
+ }
+ cblk->segs = new_segs;
+ }
+
+ seg = &cblk->segs[index];
+ memset(seg,0,sizeof(opj_tcd_seg_t));
+
+ if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
+ seg->maxpasses = 1;
+ }
+ else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
+ if (first) {
+ seg->maxpasses = 10;
+ } else {
+ seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
+ }
+ } else {
+ seg->maxpasses = 109;
+ }
+
+ return OPJ_TRUE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.h
new file mode 100644
index 0000000000..931141e057
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/t2.h
@@ -0,0 +1,133 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __T2_H
+#define __T2_H
+/**
+@file t2.h
+@brief Implementation of a tier-2 coding (packetization of code-block data) (T2)
+
+*/
+
+/** @defgroup T2 T2 - Implementation of a tier-2 coding */
+/*@{*/
+
+/**
+Tier-2 coding
+*/
+typedef struct opj_t2 {
+
+ /** Encoding: pointer to the src image. Decoding: pointer to the dst image. */
+ opj_image_t *image;
+ /** pointer to the image coding parameters */
+ opj_cp_t *cp;
+} opj_t2_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Encode the packets of a tile to a destination buffer
+@param t2 T2 handle
+@param tileno number of the tile encoded
+@param tile the tile for which to write the packets
+@param maxlayers maximum number of layers
+@param dest the destination buffer
+@param p_data_written FIXME DOC
+@param len the length of the destination buffer
+@param cstr_info Codestream information structure
+@param tpnum Tile part number of the current tile
+@param tppos The position of the tile part flag in the progression order
+@param pino FIXME DOC
+@param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass
+*/
+OPJ_BOOL opj_t2_encode_packets( opj_t2_t* t2,
+ OPJ_UINT32 tileno,
+ opj_tcd_tile_t *tile,
+ OPJ_UINT32 maxlayers,
+ OPJ_BYTE *dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 len,
+ opj_codestream_info_t *cstr_info,
+ OPJ_UINT32 tpnum,
+ OPJ_INT32 tppos,
+ OPJ_UINT32 pino,
+ J2K_T2_MODE t2_mode);
+
+/**
+Decode the packets of a tile from a source buffer
+@param t2 T2 handle
+@param tileno number that identifies the tile for which to decode the packets
+@param tile tile for which to decode the packets
+@param src FIXME DOC
+@param p_data_read the source buffer
+@param len length of the source buffer
+@param cstr_info FIXME DOC
+
+@return FIXME DOC
+ */
+OPJ_BOOL opj_t2_decode_packets( opj_t2_t *t2,
+ OPJ_UINT32 tileno,
+ opj_tcd_tile_t *tile,
+ OPJ_BYTE *src,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 len,
+ opj_codestream_index_t *cstr_info);
+
+/**
+ * Creates a Tier 2 handle
+ *
+ * @param p_image Source or destination image
+ * @param p_cp Image coding parameters.
+ * @return a new T2 handle if successful, NULL otherwise.
+*/
+opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp);
+
+/**
+Destroy a T2 handle
+@param t2 T2 handle to destroy
+*/
+void opj_t2_destroy(opj_t2_t *t2);
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __T2_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.c
new file mode 100644
index 0000000000..94feb17e4a
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.c
@@ -0,0 +1,2129 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2006-2007, Parvatha Elangovan
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/* ----------------------------------------------------------------------- */
+
+/* TODO MSD: */
+#ifdef TODO_MSD
+void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
+ int tileno, compno, resno, bandno, precno;/*, cblkno;*/
+
+ fprintf(fd, "image {\n");
+ fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
+ img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
+
+ for (tileno = 0; tileno < img->th * img->tw; tileno++) {
+ opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
+ fprintf(fd, " tile {\n");
+ fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
+ tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
+ for (compno = 0; compno < tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+ fprintf(fd, " tilec {\n");
+ fprintf(fd,
+ " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
+ tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+ fprintf(fd, "\n res {\n");
+ fprintf(fd,
+ " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
+ res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+ fprintf(fd, " band {\n");
+ fprintf(fd,
+ " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
+ band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prec = &band->precincts[precno];
+ fprintf(fd, " prec {\n");
+ fprintf(fd,
+ " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
+ prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
+ /*
+ for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
+ opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
+ fprintf(fd, " cblk {\n");
+ fprintf(fd,
+ " x0=%d, y0=%d, x1=%d, y1=%d\n",
+ cblk->x0, cblk->y0, cblk->x1, cblk->y1);
+ fprintf(fd, " }\n");
+ }
+ */
+ fprintf(fd, " }\n");
+ }
+ fprintf(fd, " }\n");
+ }
+ fprintf(fd, " }\n");
+ }
+ fprintf(fd, " }\n");
+ }
+ fprintf(fd, " }\n");
+ }
+ fprintf(fd, "}\n");
+}
+#endif
+/**
+* Allocates memory for a decoding code block.
+*/
+static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
+
+/**
+ * Deallocates the decoding data of the given precinct.
+ */
+static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
+
+/**
+ * Allocates memory for an encoding code block.
+ */
+static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
+
+/**
+ * Deallocates the encoding data of the given precinct.
+ */
+static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
+
+
+/**
+Free the memory allocated for encoding
+@param tcd TCD handle
+*/
+static void opj_tcd_free_tile(opj_tcd_t *tcd);
+
+
+static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_src_size,
+ opj_codestream_index_t *p_cstr_index );
+
+static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
+
+static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
+
+static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
+
+static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
+
+
+static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
+
+static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
+
+static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
+
+static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
+
+static OPJ_BOOL opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_max_dest_size,
+ opj_codestream_info_t *p_cstr_info );
+
+static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest_data,
+ OPJ_UINT32 p_max_dest_size,
+ opj_codestream_info_t *p_cstr_info );
+
+/* ----------------------------------------------------------------------- */
+
+/**
+Create a new TCD handle
+*/
+opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
+{
+ opj_tcd_t *l_tcd = 00;
+
+ /* create the tcd structure */
+ l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t));
+ if (!l_tcd) {
+ return 00;
+ }
+ memset(l_tcd,0,sizeof(opj_tcd_t));
+
+ l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
+
+ l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
+ if (!l_tcd->tcd_image) {
+ opj_free(l_tcd);
+ return 00;
+ }
+ memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
+
+ return l_tcd;
+}
+
+
+/* ----------------------------------------------------------------------- */
+
+void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
+ OPJ_UINT32 layno;
+
+ for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
+ opj_tcd_makelayer_fixed(tcd, layno, 1);
+ }
+}
+
+
+void opj_tcd_makelayer( opj_tcd_t *tcd,
+ OPJ_UINT32 layno,
+ OPJ_FLOAT64 thresh,
+ OPJ_UINT32 final)
+{
+ OPJ_UINT32 compno, resno, bandno, precno, cblkno;
+ OPJ_UINT32 passno;
+
+ opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
+
+ tcd_tile->distolayer[layno] = 0; /* fixed_quality */
+
+ for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
+ opj_tcd_layer_t *layer = &cblk->layers[layno];
+ OPJ_UINT32 n;
+
+ if (layno == 0) {
+ cblk->numpassesinlayers = 0;
+ }
+
+ n = cblk->numpassesinlayers;
+
+ for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
+ OPJ_UINT32 dr;
+ OPJ_FLOAT64 dd;
+ opj_tcd_pass_t *pass = &cblk->passes[passno];
+
+ if (n == 0) {
+ dr = pass->rate;
+ dd = pass->distortiondec;
+ } else {
+ dr = pass->rate - cblk->passes[n - 1].rate;
+ dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
+ }
+
+ if (!dr) {
+ if (dd != 0)
+ n = passno + 1;
+ continue;
+ }
+ if (dd / dr >= thresh)
+ n = passno + 1;
+ }
+
+ layer->numpasses = n - cblk->numpassesinlayers;
+
+ if (!layer->numpasses) {
+ layer->disto = 0;
+ continue;
+ }
+
+ if (cblk->numpassesinlayers == 0) {
+ layer->len = cblk->passes[n - 1].rate;
+ layer->data = cblk->data;
+ layer->disto = cblk->passes[n - 1].distortiondec;
+ } else {
+ layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
+ layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
+ layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
+ }
+
+ tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
+
+ if (final)
+ cblk->numpassesinlayers = n;
+ }
+ }
+ }
+ }
+ }
+}
+
+void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
+ OPJ_UINT32 compno, resno, bandno, precno, cblkno;
+ OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
+ OPJ_INT32 matrice[10][10][3];
+ OPJ_UINT32 i, j, k;
+
+ opj_cp_t *cp = tcd->cp;
+ opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
+ opj_tcp_t *tcd_tcp = tcd->tcp;
+
+ for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+
+ for (i = 0; i < tcd_tcp->numlayers; i++) {
+ for (j = 0; j < tilec->numresolutions; j++) {
+ for (k = 0; k < 3; k++) {
+ matrice[i][j][k] =
+ (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
+ * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
+ }
+ }
+ }
+
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
+ opj_tcd_layer_t *layer = &cblk->layers[layno];
+ OPJ_UINT32 n;
+ OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
+
+ /* Correction of the matrix of coefficient to include the IMSB information */
+ if (layno == 0) {
+ value = matrice[layno][resno][bandno];
+ if (imsb >= value) {
+ value = 0;
+ } else {
+ value -= imsb;
+ }
+ } else {
+ value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
+ if (imsb >= matrice[layno - 1][resno][bandno]) {
+ value -= (imsb - matrice[layno - 1][resno][bandno]);
+ if (value < 0) {
+ value = 0;
+ }
+ }
+ }
+
+ if (layno == 0) {
+ cblk->numpassesinlayers = 0;
+ }
+
+ n = cblk->numpassesinlayers;
+ if (cblk->numpassesinlayers == 0) {
+ if (value != 0) {
+ n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
+ } else {
+ n = cblk->numpassesinlayers;
+ }
+ } else {
+ n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
+ }
+
+ layer->numpasses = n - cblk->numpassesinlayers;
+
+ if (!layer->numpasses)
+ continue;
+
+ if (cblk->numpassesinlayers == 0) {
+ layer->len = cblk->passes[n - 1].rate;
+ layer->data = cblk->data;
+ } else {
+ layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
+ layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
+ }
+
+ if (final)
+ cblk->numpassesinlayers = n;
+ }
+ }
+ }
+ }
+ }
+}
+
+OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
+ OPJ_BYTE *dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 len,
+ opj_codestream_info_t *cstr_info)
+{
+ OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
+ OPJ_UINT32 passno;
+ OPJ_FLOAT64 min, max;
+ OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
+ const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
+ OPJ_FLOAT64 maxSE = 0;
+
+ opj_cp_t *cp = tcd->cp;
+ opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
+ opj_tcp_t *tcd_tcp = tcd->tcp;
+
+ min = DBL_MAX;
+ max = 0;
+
+ tcd_tile->numpix = 0; /* fixed_quality */
+
+ for (compno = 0; compno < tcd_tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
+ tilec->numpix = 0;
+
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
+
+ for (passno = 0; passno < cblk->totalpasses; passno++) {
+ opj_tcd_pass_t *pass = &cblk->passes[passno];
+ OPJ_INT32 dr;
+ OPJ_FLOAT64 dd, rdslope;
+
+ if (passno == 0) {
+ dr = (OPJ_INT32)pass->rate;
+ dd = pass->distortiondec;
+ } else {
+ dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
+ dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
+ }
+
+ if (dr == 0) {
+ continue;
+ }
+
+ rdslope = dd / dr;
+ if (rdslope < min) {
+ min = rdslope;
+ }
+
+ if (rdslope > max) {
+ max = rdslope;
+ }
+ } /* passno */
+
+ /* fixed_quality */
+ tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
+ tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
+ } /* cbklno */
+ } /* precno */
+ } /* bandno */
+ } /* resno */
+
+ maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
+ * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
+ * ((OPJ_FLOAT64)(tilec->numpix));
+ } /* compno */
+
+ /* index file */
+ if(cstr_info) {
+ opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
+ tile_info->numpix = tcd_tile->numpix;
+ tile_info->distotile = tcd_tile->distotile;
+ tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
+ }
+
+ for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
+ OPJ_FLOAT64 lo = min;
+ OPJ_FLOAT64 hi = max;
+ OPJ_BOOL success = OPJ_FALSE;
+ OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
+ OPJ_FLOAT64 goodthresh = 0;
+ OPJ_FLOAT64 stable_thresh = 0;
+ OPJ_UINT32 i;
+ OPJ_FLOAT64 distotarget; /* fixed_quality */
+
+ /* fixed_quality */
+ distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
+
+ /* Don't try to find an optimal threshold but rather take everything not included yet, if
+ -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
+ -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
+ ==> possible to have some lossy layers and the last layer for sure lossless */
+ if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
+ opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
+ OPJ_FLOAT64 thresh = 0;
+
+ if (t2 == 00) {
+ return OPJ_FALSE;
+ }
+
+ for (i = 0; i < 128; ++i) {
+ OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
+
+ thresh = (lo + hi) / 2;
+
+ opj_tcd_makelayer(tcd, layno, thresh, 0);
+
+ if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
+ if(cp->m_specific_param.m_enc.m_cinema){
+ if (! opj_t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
+
+ lo = thresh;
+ continue;
+ }
+ else {
+ distoachieved = layno == 0 ?
+ tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
+
+ if (distoachieved < distotarget) {
+ hi=thresh;
+ stable_thresh = thresh;
+ continue;
+ }else{
+ lo=thresh;
+ }
+ }
+ }else{
+ distoachieved = (layno == 0) ?
+ tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
+
+ if (distoachieved < distotarget) {
+ hi = thresh;
+ stable_thresh = thresh;
+ continue;
+ }
+ lo = thresh;
+ }
+ } else {
+ if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
+ {
+ /* TODO: what to do with l ??? seek / tell ??? */
+ /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
+ lo = thresh;
+ continue;
+ }
+
+ hi = thresh;
+ stable_thresh = thresh;
+ }
+ }
+
+ success = OPJ_TRUE;
+ goodthresh = stable_thresh == 0? thresh : stable_thresh;
+
+ opj_t2_destroy(t2);
+ } else {
+ success = OPJ_TRUE;
+ goodthresh = min;
+ }
+
+ if (!success) {
+ return OPJ_FALSE;
+ }
+
+ if(cstr_info) { /* Threshold for Marcela Index */
+ cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
+ }
+
+ opj_tcd_makelayer(tcd, layno, goodthresh, 1);
+
+ /* fixed_quality */
+ cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
+ opj_image_t * p_image,
+ opj_cp_t * p_cp )
+{
+ OPJ_UINT32 l_tile_comp_size;
+
+ p_tcd->image = p_image;
+ p_tcd->cp = p_cp;
+
+ p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
+ if (! p_tcd->tcd_image->tiles) {
+ return OPJ_FALSE;
+ }
+ memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
+
+ l_tile_comp_size = p_image->numcomps * (OPJ_UINT32)sizeof(opj_tcd_tilecomp_t);
+ p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
+ if (! p_tcd->tcd_image->tiles->comps ) {
+ return OPJ_FALSE;
+ }
+ memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
+
+ p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
+ p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
+
+ return OPJ_TRUE;
+}
+
+/**
+Destroy a previously created TCD handle
+*/
+void opj_tcd_destroy(opj_tcd_t *tcd) {
+ if (tcd) {
+ opj_tcd_free_tile(tcd);
+
+ if (tcd->tcd_image) {
+ opj_free(tcd->tcd_image);
+ tcd->tcd_image = 00;
+ }
+ opj_free(tcd);
+ }
+}
+
+/* ----------------------------------------------------------------------- */
+#define OPJ_MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
+OPJ_BOOL FUNCTION ( opj_tcd_t *p_tcd, \
+ OPJ_UINT32 p_tile_no \
+ ) \
+{ \
+ OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
+ OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
+ opj_tcp_t * l_tcp = 00; \
+ opj_cp_t * l_cp = 00; \
+ opj_tcd_tile_t * l_tile = 00; \
+ opj_tccp_t *l_tccp = 00; \
+ opj_tcd_tilecomp_t *l_tilec = 00; \
+ opj_image_comp_t * l_image_comp = 00; \
+ opj_tcd_resolution_t *l_res = 00; \
+ opj_tcd_band_t *l_band = 00; \
+ opj_stepsize_t * l_step_size = 00; \
+ opj_tcd_precinct_t *l_current_precinct = 00; \
+ TYPE* l_code_block = 00; \
+ opj_image_t *l_image = 00; \
+ OPJ_UINT32 p,q; \
+ OPJ_UINT32 l_level_no; \
+ OPJ_UINT32 l_pdx, l_pdy; \
+ OPJ_UINT32 l_gain; \
+ OPJ_INT32 l_x0b, l_y0b; \
+ /* extent of precincts , top left, bottom right**/ \
+ OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
+ /* number of precinct for a resolution */ \
+ OPJ_UINT32 l_nb_precincts; \
+ /* room needed to store l_nb_precinct precinct for a resolution */ \
+ OPJ_UINT32 l_nb_precinct_size; \
+ /* number of code blocks for a precinct*/ \
+ OPJ_UINT32 l_nb_code_blocks; \
+ /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
+ OPJ_UINT32 l_nb_code_blocks_size; \
+ /* size of data for a tile */ \
+ OPJ_UINT32 l_data_size; \
+ \
+ l_cp = p_tcd->cp; \
+ l_tcp = &(l_cp->tcps[p_tile_no]); \
+ l_tile = p_tcd->tcd_image->tiles; \
+ l_tccp = l_tcp->tccps; \
+ l_tilec = l_tile->comps; \
+ l_image = p_tcd->image; \
+ l_image_comp = p_tcd->image->comps; \
+ \
+ p = p_tile_no % l_cp->tw; /* tile coordinates */ \
+ q = p_tile_no / l_cp->tw; \
+ /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
+ \
+ /* 4 borders of the tile rescale on the image if necessary */ \
+ l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0); \
+ l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0); \
+ l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1); \
+ l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1); \
+ /* testcase 1888.pdf.asan.35.988 */ \
+ if (l_tccp->numresolutions == 0) { \
+ fprintf(stderr, "tiles require at least one resolution\n"); \
+ return OPJ_FALSE; \
+ } \
+ /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
+ \
+ /*tile->numcomps = image->numcomps; */ \
+ for(compno = 0; compno < l_tile->numcomps; ++compno) { \
+ /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
+ \
+ /* border of each l_tile component (global) */ \
+ l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx); \
+ l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy); \
+ l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); \
+ l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); \
+ /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
+ if (l_tilec->x1 < 0 || l_tilec->x0 < 0 || l_tilec->y1 < 0 || l_tilec->y0 < 0) return OPJ_FALSE;\
+ l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) \
+ * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0) * (OPJ_UINT32)sizeof(OPJ_UINT32 );\
+ l_tilec->numresolutions = l_tccp->numresolutions; \
+ if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
+ l_tilec->minimum_num_resolutions = 1; \
+ } \
+ else { \
+ l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
+ - l_cp->m_specific_param.m_dec.m_reduce; \
+ } \
+ \
+ if (l_tilec->data == 00) { \
+ l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
+ if (! l_tilec->data ) { \
+ return OPJ_FALSE; \
+ } \
+ /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
+ \
+ l_tilec->data_size = l_data_size; \
+ } \
+ else if (l_data_size > l_tilec->data_size) { \
+ OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
+ fprintf(stderr, "Not enough memory to handle tile data\n"); \
+ if (! new_data) { \
+ opj_free(l_tilec->data); \
+ l_tilec->data = NULL; \
+ l_tilec->data_size = 0; \
+ return OPJ_FALSE; \
+ } \
+ l_tilec->data = new_data; \
+ /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
+ l_tilec->data_size = l_data_size; \
+ } \
+ \
+ l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t); \
+ \
+ if (l_tilec->resolutions == 00) { \
+ l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); \
+ if (! l_tilec->resolutions ) { \
+ return OPJ_FALSE; \
+ } \
+ /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ \
+ l_tilec->resolutions_size = l_data_size; \
+ memset(l_tilec->resolutions,0,l_data_size); \
+ } \
+ else if (l_data_size > l_tilec->resolutions_size) { \
+ opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
+ if (! new_resolutions) { \
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
+ fprintf(stderr, "Not enough memory to tile resolutions\n"); \
+ opj_free(l_tilec->resolutions); \
+ l_tilec->resolutions = NULL; \
+ l_tilec->resolutions_size = 0; \
+ return OPJ_FALSE; \
+ } \
+ l_tilec->resolutions = new_resolutions; \
+ /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
+ memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
+ l_tilec->resolutions_size = l_data_size; \
+ } \
+ \
+ l_level_no = l_tilec->numresolutions - 1; \
+ l_res = l_tilec->resolutions; \
+ l_step_size = l_tccp->stepsizes; \
+ if (l_tccp->qmfbid == 0) { \
+ l_gain_ptr = &opj_dwt_getgain_real; \
+ } \
+ else { \
+ l_gain_ptr = &opj_dwt_getgain; \
+ } \
+ /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
+ \
+ for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
+ /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
+ OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; \
+ OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
+ OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
+ \
+ /* border for each resolution level (global) */ \
+ l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
+ l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
+ l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
+ l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
+ /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/ \
+ /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
+ l_pdx = l_tccp->prcw[resno]; \
+ l_pdy = l_tccp->prch[resno]; \
+ /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
+ /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
+ l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx; \
+ l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy; \
+ l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx; \
+ l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy; \
+ /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/ \
+ \
+ l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
+ l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
+ /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
+ \
+ l_nb_precincts = l_res->pw * l_res->ph; \
+ l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t); \
+ if (resno == 0) { \
+ tlcbgxstart = l_tl_prc_x_start; \
+ tlcbgystart = l_tl_prc_y_start; \
+ /*brcbgxend = l_br_prc_x_end;*/ \
+ /* brcbgyend = l_br_prc_y_end;*/ \
+ cbgwidthexpn = l_pdx; \
+ cbgheightexpn = l_pdy; \
+ l_res->numbands = 1; \
+ } \
+ else { \
+ tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); \
+ tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); \
+ /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ \
+ /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ \
+ cbgwidthexpn = l_pdx - 1; \
+ cbgheightexpn = l_pdy - 1; \
+ l_res->numbands = 3; \
+ } \
+ \
+ cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); \
+ cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); \
+ l_band = l_res->bands; \
+ \
+ for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
+ OPJ_INT32 numbps; \
+ /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
+ \
+ if (resno == 0) { \
+ l_band->bandno = 0 ; \
+ l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
+ l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
+ l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
+ l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
+ } \
+ else { \
+ l_band->bandno = bandno + 1; \
+ /* x0b = 1 if bandno = 1 or 3 */ \
+ l_x0b = l_band->bandno&1; \
+ /* y0b = 1 if bandno = 2 or 3 */ \
+ l_y0b = (OPJ_INT32)((l_band->bandno)>>1); \
+ /* l_band border (global) */ \
+ l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
+ l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
+ l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
+ l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
+ } \
+ \
+ /** avoid an if with storing function pointer */ \
+ l_gain = (*l_gain_ptr) (l_band->bandno); \
+ numbps = (OPJ_INT32)(l_image_comp->prec + l_gain); \
+ l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
+ l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ \
+ \
+ if (! l_band->precincts) { \
+ l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
+ if (! l_band->precincts) { \
+ return OPJ_FALSE; \
+ } \
+ /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ \
+ memset(l_band->precincts,0,l_nb_precinct_size); \
+ l_band->precincts_data_size = l_nb_precinct_size; \
+ } \
+ else if (l_band->precincts_data_size < l_nb_precinct_size) { \
+ \
+ opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
+ if (! new_precincts) { \
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
+ fprintf(stderr, "Not enough memory to handle band precints\n"); \
+ opj_free(l_band->precincts); \
+ l_band->precincts = NULL; \
+ l_band->precincts_data_size = 0; \
+ return OPJ_FALSE; \
+ } \
+ l_band->precincts = new_precincts; \
+ /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/ \
+ memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
+ l_band->precincts_data_size = l_nb_precinct_size; \
+ } \
+ \
+ l_current_precinct = l_band->precincts; \
+ for (precno = 0; precno < l_nb_precincts; ++precno) { \
+ OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
+ OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn); \
+ OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn); \
+ OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
+ OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
+ /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
+ /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
+ \
+ /* precinct size (global) */ \
+ /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
+ \
+ l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); \
+ l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); \
+ l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); \
+ l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); \
+ /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ \
+ \
+ tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
+ /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
+ tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
+ /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
+ brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
+ /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
+ brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
+ /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
+ l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn); \
+ l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn); \
+ \
+ l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
+ /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
+ l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE); \
+ \
+ if (! l_current_precinct->cblks.ELEMENT) { \
+ l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
+ if (! l_current_precinct->cblks.ELEMENT ) { \
+ return OPJ_FALSE; \
+ } \
+ /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ \
+ \
+ memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
+ \
+ l_current_precinct->block_size = l_nb_code_blocks_size; \
+ } \
+ else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
+ TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
+ if (! new_ELEMENT) { \
+ opj_free(l_current_precinct->cblks.ELEMENT); \
+ l_current_precinct->cblks.ELEMENT = NULL; \
+ l_current_precinct->block_size = 0; \
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
+ fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
+ return OPJ_FALSE; \
+ } \
+ l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
+ /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */\
+ \
+ memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
+ ,0 \
+ ,l_nb_code_blocks_size - l_current_precinct->block_size); \
+ \
+ l_current_precinct->block_size = l_nb_code_blocks_size; \
+ } \
+ \
+ if (! l_current_precinct->incltree) { \
+ l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, \
+ l_current_precinct->ch); \
+ } \
+ else{ \
+ l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, \
+ l_current_precinct->cw, \
+ l_current_precinct->ch); \
+ } \
+ \
+ if (! l_current_precinct->incltree) { \
+ fprintf(stderr, "WARNING: No incltree created.\n"); \
+ /*return OPJ_FALSE;*/ \
+ } \
+ \
+ if (! l_current_precinct->imsbtree) { \
+ l_current_precinct->imsbtree = opj_tgt_create( \
+ l_current_precinct->cw, \
+ l_current_precinct->ch); \
+ } \
+ else { \
+ l_current_precinct->imsbtree = opj_tgt_init( \
+ l_current_precinct->imsbtree, \
+ l_current_precinct->cw, \
+ l_current_precinct->ch); \
+ } \
+ \
+ if (! l_current_precinct->imsbtree) { \
+ fprintf(stderr, "WARNING: No imsbtree created.\n"); \
+ /*return OPJ_FALSE;*/ \
+ } \
+ \
+ l_code_block = l_current_precinct->cblks.ELEMENT; \
+ \
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
+ OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
+ OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
+ OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
+ OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
+ \
+ /* code-block size (global) */ \
+ l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
+ l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
+ l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
+ l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
+ \
+ if (! FUNCTION_ELEMENT(l_code_block)) { \
+ return OPJ_FALSE; \
+ } \
+ ++l_code_block; \
+ } \
+ ++l_current_precinct; \
+ } /* precno */ \
+ ++l_band; \
+ ++l_step_size; \
+ } /* bandno */ \
+ ++l_res; \
+ --l_level_no; \
+ } /* resno */ \
+ ++l_tccp; \
+ ++l_tilec; \
+ ++l_image_comp; \
+ } /* compno */ \
+ return OPJ_TRUE; \
+} \
+
+
+OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
+OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
+
+#undef OPJ_MACRO_TCD_ALLOCATE
+
+/**
+ * Allocates memory for an encoding code block.
+ */
+OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
+{
+ if (! p_code_block->data) {
+
+ p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
+ if(! p_code_block->data) {
+ return OPJ_FALSE;
+ }
+
+ p_code_block->data[0] = 0;
+ p_code_block->data+=1;
+
+ /* no memset since data */
+ p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
+ if (! p_code_block->layers) {
+ return OPJ_FALSE;
+ }
+
+ p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
+ if (! p_code_block->passes) {
+ return OPJ_FALSE;
+ }
+ }
+
+ memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
+ memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Allocates memory for a decoding code block.
+ */
+OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
+{
+ OPJ_UINT32 l_seg_size;
+
+ if (! p_code_block->data) {
+
+ p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
+ if (! p_code_block->data) {
+ return OPJ_FALSE;
+ }
+ p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
+ /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
+
+ l_seg_size = OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
+ p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
+ if (! p_code_block->segs) {
+ return OPJ_FALSE;
+ }
+ memset(p_code_block->segs,0,l_seg_size);
+ /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
+
+ p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
+ /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
+ }
+ /* TODO */
+ /*p_code_block->numsegs = 0; */
+
+ return OPJ_TRUE;
+}
+
+OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 i;
+ OPJ_UINT32 l_data_size = 0;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_t * l_tile_comp = 00;
+ opj_tcd_resolution_t * l_res = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+
+ l_tile_comp = p_tcd->tcd_image->tiles->comps;
+ l_img_comp = p_tcd->image->comps;
+
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+ l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+
+ if(l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
+ l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
+ ++l_img_comp;
+ ++l_tile_comp;
+ }
+
+ return l_data_size;
+}
+
+OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
+ OPJ_UINT32 p_tile_no,
+ OPJ_BYTE *p_dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_max_length,
+ opj_codestream_info_t *p_cstr_info)
+{
+
+ if (p_tcd->cur_tp_num == 0) {
+
+ p_tcd->tcd_tileno = p_tile_no;
+ p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
+
+ /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
+ if(p_cstr_info) {
+ OPJ_UINT32 l_num_packs = 0;
+ OPJ_UINT32 i;
+ opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
+ opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
+
+ for (i = 0; i < l_tilec_idx->numresolutions; i++) {
+ opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
+
+ p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
+ p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
+
+ l_num_packs += l_res_idx->pw * l_res_idx->ph;
+ p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
+ p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
+ }
+ p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
+ }
+ /* << INDEX */
+
+ /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
+ /*---------------TILE-------------------*/
+ if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
+
+ /* FIXME _ProfStart(PGROUP_MCT); */
+ if (! opj_tcd_mct_encode(p_tcd)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_MCT); */
+
+ /* FIXME _ProfStart(PGROUP_DWT); */
+ if (! opj_tcd_dwt_encode(p_tcd)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_DWT); */
+
+ /* FIXME _ProfStart(PGROUP_T1); */
+ if (! opj_tcd_t1_encode(p_tcd)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_T1); */
+
+ /* FIXME _ProfStart(PGROUP_RATE); */
+ if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_RATE); */
+
+ }
+ /*--------------TIER2------------------*/
+
+ /* INDEX */
+ if (p_cstr_info) {
+ p_cstr_info->index_write = 1;
+ }
+ /* FIXME _ProfStart(PGROUP_T2); */
+
+ if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_T2); */
+
+ /*---------------CLEAN-------------------*/
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
+ OPJ_BYTE *p_src,
+ OPJ_UINT32 p_max_length,
+ OPJ_UINT32 p_tile_no,
+ opj_codestream_index_t *p_cstr_index
+ )
+{
+ OPJ_UINT32 l_data_read;
+ p_tcd->tcd_tileno = p_tile_no;
+ p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
+
+#ifdef TODO_MSD /* FIXME */
+ /* INDEX >> */
+ if(p_cstr_info) {
+ OPJ_UINT32 resno, compno, numprec = 0;
+ for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
+ opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
+ opj_tccp_t *tccp = &tcp->tccps[compno];
+ opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
+ for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
+ opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
+ p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
+ p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
+ numprec += res_idx->pw * res_idx->ph;
+ p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
+ p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
+ }
+ }
+ p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
+ p_cstr_info->packno = 0;
+ }
+ /* << INDEX */
+#endif
+
+ /*--------------TIER2------------------*/
+ /* FIXME _ProfStart(PGROUP_T2); */
+ l_data_read = 0;
+ if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
+ {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_T2); */
+
+ /*------------------TIER1-----------------*/
+
+ /* FIXME _ProfStart(PGROUP_T1); */
+ if
+ (! opj_tcd_t1_decode(p_tcd))
+ {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_T1); */
+
+ /*----------------DWT---------------------*/
+
+ /* FIXME _ProfStart(PGROUP_DWT); */
+ if
+ (! opj_tcd_dwt_decode(p_tcd))
+ {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_DWT); */
+
+ /*----------------MCT-------------------*/
+ /* FIXME _ProfStart(PGROUP_MCT); */
+ if
+ (! opj_tcd_mct_decode(p_tcd))
+ {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_MCT); */
+
+ /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
+ if
+ (! opj_tcd_dc_level_shift_decode(p_tcd))
+ {
+ return OPJ_FALSE;
+ }
+ /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
+
+
+ /*---------------TILE-------------------*/
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest,
+ OPJ_UINT32 p_dest_length
+ )
+{
+ OPJ_UINT32 i,j,k,l_data_size = 0;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_t * l_tilec = 00;
+ opj_tcd_resolution_t * l_res;
+ OPJ_UINT32 l_size_comp, l_remaining;
+ OPJ_UINT32 l_stride, l_width,l_height;
+
+ l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
+ if (l_data_size > p_dest_length) {
+ return OPJ_FALSE;
+ }
+
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_img_comp = p_tcd->image->comps;
+
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+ l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+ l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
+ l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
+ l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
+ l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
+
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ switch (l_size_comp)
+ {
+ case 1:
+ {
+ OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
+ const OPJ_INT32 * l_src_ptr = l_tilec->data;
+
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+
+ p_dest = (OPJ_BYTE *)l_dest_ptr;
+ }
+ break;
+ case 2:
+ {
+ const OPJ_INT32 * l_src_ptr = l_tilec->data;
+ OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
+
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
+ }
+ l_src_ptr += l_stride;
+ }
+ }
+
+ p_dest = (OPJ_BYTE*) l_dest_ptr;
+ }
+ break;
+ case 4:
+ {
+ OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
+ OPJ_INT32 * l_src_ptr = l_tilec->data;
+
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (*(l_src_ptr++));
+ }
+ l_src_ptr += l_stride;
+ }
+
+ p_dest = (OPJ_BYTE*) l_dest_ptr;
+ }
+ break;
+ }
+
+ ++l_img_comp;
+ ++l_tilec;
+ }
+
+ return OPJ_TRUE;
+}
+
+
+
+
+void opj_tcd_free_tile(opj_tcd_t *p_tcd)
+{
+ OPJ_UINT32 compno, resno, bandno, precno;
+ opj_tcd_tile_t *l_tile = 00;
+ opj_tcd_tilecomp_t *l_tile_comp = 00;
+ opj_tcd_resolution_t *l_res = 00;
+ opj_tcd_band_t *l_band = 00;
+ opj_tcd_precinct_t *l_precinct = 00;
+ OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
+ void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
+
+ if (! p_tcd) {
+ return;
+ }
+
+ if (! p_tcd->tcd_image) {
+ return;
+ }
+
+ if (p_tcd->m_is_decoder) {
+ l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
+ }
+ else {
+ l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
+ }
+
+ l_tile = p_tcd->tcd_image->tiles;
+ if (! l_tile) {
+ return;
+ }
+
+ l_tile_comp = l_tile->comps;
+
+ for (compno = 0; compno < l_tile->numcomps; ++compno) {
+ l_res = l_tile_comp->resolutions;
+ if (l_res) {
+
+ l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
+ for (resno = 0; resno < l_nb_resolutions; ++resno) {
+ l_band = l_res->bands;
+ for (bandno = 0; bandno < 3; ++bandno) {
+ l_precinct = l_band->precincts;
+ if (l_precinct) {
+
+ l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
+ for (precno = 0; precno < l_nb_precincts; ++precno) {
+ opj_tgt_destroy(l_precinct->incltree);
+ l_precinct->incltree = 00;
+ opj_tgt_destroy(l_precinct->imsbtree);
+ l_precinct->imsbtree = 00;
+ (*l_tcd_code_block_deallocate) (l_precinct);
+ ++l_precinct;
+ }
+
+ opj_free(l_band->precincts);
+ l_band->precincts = 00;
+ }
+ ++l_band;
+ } /* for (resno */
+ ++l_res;
+ }
+
+ opj_free(l_tile_comp->resolutions);
+ l_tile_comp->resolutions = 00;
+ }
+
+ if (l_tile_comp->data) {
+ opj_free(l_tile_comp->data);
+ l_tile_comp->data = 00;
+ }
+ ++l_tile_comp;
+ }
+
+ opj_free(l_tile->comps);
+ l_tile->comps = 00;
+ opj_free(p_tcd->tcd_image->tiles);
+ p_tcd->tcd_image->tiles = 00;
+}
+
+
+OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_src_data,
+ OPJ_UINT32 * p_data_read,
+ OPJ_UINT32 p_max_src_size,
+ opj_codestream_index_t *p_cstr_index
+ )
+{
+ opj_t2_t * l_t2;
+
+ l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
+ if (l_t2 == 00) {
+ return OPJ_FALSE;
+ }
+
+ if (! opj_t2_decode_packets(
+ l_t2,
+ p_tcd->tcd_tileno,
+ p_tcd->tcd_image->tiles,
+ p_src_data,
+ p_data_read,
+ p_max_src_size,
+ p_cstr_index)) {
+ opj_t2_destroy(l_t2);
+ return OPJ_FALSE;
+ }
+
+ opj_t2_destroy(l_t2);
+
+ /*---------------CLEAN-------------------*/
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 compno;
+ opj_t1_t * l_t1;
+ opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
+ opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
+ opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
+
+
+ l_t1 = opj_t1_create();
+ if (l_t1 == 00) {
+ return OPJ_FALSE;
+ }
+
+ for (compno = 0; compno < l_tile->numcomps; ++compno) {
+ /* The +3 is headroom required by the vectorized DWT */
+ if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
+ opj_t1_destroy(l_t1);
+ return OPJ_FALSE;
+ }
+ ++l_tile_comp;
+ ++l_tccp;
+ }
+
+ opj_t1_destroy(l_t1);
+
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 compno;
+ opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
+ opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
+ opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
+ opj_image_comp_t * l_img_comp = p_tcd->image->comps;
+
+ for (compno = 0; compno < l_tile->numcomps; compno++) {
+ /*
+ if (tcd->cp->reduce != 0) {
+ tcd->image->comps[compno].resno_decoded =
+ tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
+ if (tcd->image->comps[compno].resno_decoded < 0)
+ {
+ return false;
+ }
+ }
+ numres2decode = tcd->image->comps[compno].resno_decoded + 1;
+ if(numres2decode > 0){
+ */
+
+ if (l_tccp->qmfbid == 1) {
+ if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ ++l_tile_comp;
+ ++l_img_comp;
+ ++l_tccp;
+ }
+
+ return OPJ_TRUE;
+}
+OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
+{
+ opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
+ opj_tcp_t * l_tcp = p_tcd->tcp;
+ opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
+ OPJ_UINT32 l_samples,i;
+
+ if (! l_tcp->mct) {
+ return OPJ_TRUE;
+ }
+
+ l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
+
+ if (l_tile->numcomps >= 3 ){
+ /* testcase 1336.pdf.asan.47.376 */
+ if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 - l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
+ (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
+ (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
+ fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
+ return OPJ_FALSE;
+ }
+ else if (l_tcp->mct == 2) {
+ OPJ_BYTE ** l_data;
+
+ if (! l_tcp->m_mct_decoding_matrix) {
+ return OPJ_TRUE;
+ }
+
+ l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
+ if (! l_data) {
+ return OPJ_FALSE;
+ }
+
+ for (i=0;i<l_tile->numcomps;++i) {
+ l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
+ ++l_tile_comp;
+ }
+
+ if (! opj_mct_decode_custom(/* MCT data */
+ (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
+ /* size of components */
+ l_samples,
+ /* components */
+ l_data,
+ /* nb of components (i.e. size of pData) */
+ l_tile->numcomps,
+ /* tells if the data is signed */
+ p_tcd->image->comps->sgnd)) {
+ opj_free(l_data);
+ return OPJ_FALSE;
+ }
+
+ opj_free(l_data);
+ }
+ else {
+ if (l_tcp->tccps->qmfbid == 1) {
+ opj_mct_decode( l_tile->comps[0].data,
+ l_tile->comps[1].data,
+ l_tile->comps[2].data,
+ l_samples);
+ }
+ else {
+ opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
+ (OPJ_FLOAT32*)l_tile->comps[1].data,
+ (OPJ_FLOAT32*)l_tile->comps[2].data,
+ l_samples);
+ }
+ }
+ }
+ else {
+ /* FIXME need to use opj_event_msg function */
+ fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
+ }
+
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 compno;
+ opj_tcd_tilecomp_t * l_tile_comp = 00;
+ opj_tccp_t * l_tccp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_resolution_t* l_res = 00;
+ opj_tcd_tile_t * l_tile;
+ OPJ_UINT32 l_width,l_height,i,j;
+ OPJ_INT32 * l_current_ptr;
+ OPJ_INT32 l_min, l_max;
+ OPJ_UINT32 l_stride;
+
+ l_tile = p_tcd->tcd_image->tiles;
+ l_tile_comp = l_tile->comps;
+ l_tccp = p_tcd->tcp->tccps;
+ l_img_comp = p_tcd->image->comps;
+
+ for (compno = 0; compno < l_tile->numcomps; compno++) {
+ l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
+ l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
+ l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
+ l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
+
+ assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
+
+ if (l_img_comp->sgnd) {
+ l_min = -(1 << (l_img_comp->prec - 1));
+ l_max = (1 << (l_img_comp->prec - 1)) - 1;
+ }
+ else {
+ l_min = 0;
+ l_max = (1 << l_img_comp->prec) - 1;
+ }
+
+ l_current_ptr = l_tile_comp->data;
+
+ if (l_tccp->qmfbid == 1) {
+ for (j=0;j<l_height;++j) {
+ for (i = 0; i < l_width; ++i) {
+ *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
+ ++l_current_ptr;
+ }
+ l_current_ptr += l_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (i = 0; i < l_width; ++i) {
+ OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
+ *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
+ ++l_current_ptr;
+ }
+ l_current_ptr += l_stride;
+ }
+ }
+
+ ++l_img_comp;
+ ++l_tccp;
+ ++l_tile_comp;
+ }
+
+ return OPJ_TRUE;
+}
+
+
+
+/**
+ * Deallocates the encoding data of the given precinct.
+ */
+void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
+{
+ OPJ_UINT32 cblkno , l_nb_code_blocks;
+
+ opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
+ if (l_code_block) {
+ /*fprintf(stderr,"deallocate codeblock:{\n");*/
+ /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
+ /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
+ l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
+
+
+ l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
+ /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
+
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
+
+ if (l_code_block->data) {
+ opj_free(l_code_block->data);
+ l_code_block->data = 00;
+ }
+
+ if (l_code_block->segs) {
+ opj_free(l_code_block->segs );
+ l_code_block->segs = 00;
+ }
+
+ ++l_code_block;
+ }
+
+ opj_free(p_precinct->cblks.dec);
+ p_precinct->cblks.dec = 00;
+ }
+}
+
+/**
+ * Deallocates the encoding data of the given precinct.
+ */
+void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
+{
+ OPJ_UINT32 cblkno , l_nb_code_blocks;
+
+ opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
+ if (l_code_block) {
+ l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
+
+ for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
+ if (l_code_block->data) {
+ opj_free(l_code_block->data - 1);
+ l_code_block->data = 00;
+ }
+
+ if (l_code_block->layers) {
+ opj_free(l_code_block->layers );
+ l_code_block->layers = 00;
+ }
+
+ if (l_code_block->passes) {
+ opj_free(l_code_block->passes );
+ l_code_block->passes = 00;
+ }
+ ++l_code_block;
+ }
+
+ opj_free(p_precinct->cblks.enc);
+
+ p_precinct->cblks.enc = 00;
+ }
+}
+
+OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 i,l_data_size = 0;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_t * l_tilec = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_img_comp = p_tcd->image->comps;
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+ l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
+ ++l_img_comp;
+ ++l_tilec;
+ }
+
+ return l_data_size;
+}
+
+OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
+{
+ OPJ_UINT32 compno;
+ opj_tcd_tilecomp_t * l_tile_comp = 00;
+ opj_tccp_t * l_tccp = 00;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tile_t * l_tile;
+ OPJ_UINT32 l_nb_elem,i;
+ OPJ_INT32 * l_current_ptr;
+
+ l_tile = p_tcd->tcd_image->tiles;
+ l_tile_comp = l_tile->comps;
+ l_tccp = p_tcd->tcp->tccps;
+ l_img_comp = p_tcd->image->comps;
+
+ for (compno = 0; compno < l_tile->numcomps; compno++) {
+ l_current_ptr = l_tile_comp->data;
+ l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
+
+ if (l_tccp->qmfbid == 1) {
+ for (i = 0; i < l_nb_elem; ++i) {
+ *l_current_ptr -= l_tccp->m_dc_level_shift ;
+ ++l_current_ptr;
+ }
+ }
+ else {
+ for (i = 0; i < l_nb_elem; ++i) {
+ *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
+ ++l_current_ptr;
+ }
+ }
+
+ ++l_img_comp;
+ ++l_tccp;
+ ++l_tile_comp;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
+{
+ opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
+ opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
+ OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
+ OPJ_UINT32 i;
+ OPJ_BYTE ** l_data = 00;
+ opj_tcp_t * l_tcp = p_tcd->tcp;
+
+ if(!p_tcd->tcp->mct) {
+ return OPJ_TRUE;
+ }
+
+ if (p_tcd->tcp->mct == 2) {
+ if (! p_tcd->tcp->m_mct_coding_matrix) {
+ return OPJ_TRUE;
+ }
+
+ l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
+ if (! l_data) {
+ return OPJ_FALSE;
+ }
+
+ for (i=0;i<l_tile->numcomps;++i) {
+ l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
+ ++l_tile_comp;
+ }
+
+ if (! opj_mct_encode_custom(/* MCT data */
+ (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
+ /* size of components */
+ samples,
+ /* components */
+ l_data,
+ /* nb of components (i.e. size of pData) */
+ l_tile->numcomps,
+ /* tells if the data is signed */
+ p_tcd->image->comps->sgnd) )
+ {
+ opj_free(l_data);
+ return OPJ_FALSE;
+ }
+
+ opj_free(l_data);
+ }
+ else if (l_tcp->tccps->qmfbid == 0) {
+ opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
+ }
+ else {
+ opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
+{
+ opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
+ opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
+ opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
+ OPJ_UINT32 compno;
+
+ for (compno = 0; compno < l_tile->numcomps; ++compno) {
+ if (l_tccp->qmfbid == 1) {
+ if (! opj_dwt_encode(l_tile_comp)) {
+ return OPJ_FALSE;
+ }
+ }
+ else if (l_tccp->qmfbid == 0) {
+ if (! opj_dwt_encode_real(l_tile_comp)) {
+ return OPJ_FALSE;
+ }
+ }
+
+ ++l_tile_comp;
+ ++l_tccp;
+ }
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
+{
+ opj_t1_t * l_t1;
+ const OPJ_FLOAT64 * l_mct_norms;
+ opj_tcp_t * l_tcp = p_tcd->tcp;
+
+ l_t1 = opj_t1_create();
+ if (l_t1 == 00) {
+ return OPJ_FALSE;
+ }
+
+ if (l_tcp->mct == 1) {
+ /* irreversible encoding */
+ if (l_tcp->tccps->qmfbid == 0) {
+ l_mct_norms = opj_mct_get_mct_norms_real();
+ }
+ else {
+ l_mct_norms = opj_mct_get_mct_norms();
+ }
+ }
+ else {
+ l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
+ }
+
+ if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
+ opj_t1_destroy(l_t1);
+ return OPJ_FALSE;
+ }
+
+ opj_t1_destroy(l_t1);
+
+ return OPJ_TRUE;
+}
+
+OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest_data,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_max_dest_size,
+ opj_codestream_info_t *p_cstr_info )
+{
+ opj_t2_t * l_t2;
+
+ l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
+ if (l_t2 == 00) {
+ return OPJ_FALSE;
+ }
+
+ if (! opj_t2_encode_packets(
+ l_t2,
+ p_tcd->tcd_tileno,
+ p_tcd->tcd_image->tiles,
+ p_tcd->tcp->numlayers,
+ p_dest_data,
+ p_data_written,
+ p_max_dest_size,
+ p_cstr_info,
+ p_tcd->tp_num,
+ p_tcd->tp_pos,
+ p_tcd->cur_pino,
+ FINAL_PASS))
+ {
+ opj_t2_destroy(l_t2);
+ return OPJ_FALSE;
+ }
+
+ opj_t2_destroy(l_t2);
+
+ /*---------------CLEAN-------------------*/
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest_data,
+ OPJ_UINT32 p_max_dest_size,
+ opj_codestream_info_t *p_cstr_info )
+{
+ opj_cp_t * l_cp = p_tcd->cp;
+ OPJ_UINT32 l_nb_written = 0;
+
+ if (p_cstr_info) {
+ p_cstr_info->index_write = 0;
+ }
+
+ if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
+ /* fixed_quality */
+ /* Normal Rate/distortion allocation */
+ if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ /* Fixed layer allocation */
+ opj_tcd_rateallocate_fixed(p_tcd);
+ }
+
+ return OPJ_TRUE;
+}
+
+
+OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_src,
+ OPJ_UINT32 p_src_length )
+{
+ OPJ_UINT32 i,j,l_data_size = 0;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_t * l_tilec = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+ OPJ_UINT32 l_nb_elem;
+
+ l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
+ if (l_data_size != p_src_length) {
+ return OPJ_FALSE;
+ }
+
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_img_comp = p_tcd->image->comps;
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+ l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+ l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
+
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ switch (l_size_comp) {
+ case 1:
+ {
+ OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
+ OPJ_INT32 * l_dest_ptr = l_tilec->data;
+
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_nb_elem;++j) {
+ *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
+ }
+ }
+ else {
+ for (j=0;j<l_nb_elem;++j) {
+ *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
+ }
+ }
+
+ p_src = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ case 2:
+ {
+ OPJ_INT32 * l_dest_ptr = l_tilec->data;
+ OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
+
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_nb_elem;++j) {
+ *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
+ }
+ }
+ else {
+ for (j=0;j<l_nb_elem;++j) {
+ *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
+ }
+ }
+
+ p_src = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ case 4:
+ {
+ OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
+ OPJ_INT32 * l_dest_ptr = l_tilec->data;
+
+ for (j=0;j<l_nb_elem;++j) {
+ *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
+ }
+
+ p_src = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ }
+
+ ++l_img_comp;
+ ++l_tilec;
+ }
+
+ return OPJ_TRUE;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.h
new file mode 100644
index 0000000000..360923b151
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tcd.h
@@ -0,0 +1,354 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __TCD_H
+#define __TCD_H
+/**
+@file tcd.h
+@brief Implementation of a tile coder/decoder (TCD)
+
+The functions in TCD.C encode or decode each tile independently from
+each other. The functions in TCD.C are used by other functions in J2K.C.
+*/
+
+/** @defgroup TCD TCD - Implementation of a tile coder/decoder */
+/*@{*/
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_seg {
+ OPJ_BYTE ** data;
+ OPJ_UINT32 dataindex;
+ OPJ_UINT32 numpasses;
+ OPJ_UINT32 real_num_passes;
+ OPJ_UINT32 len;
+ OPJ_UINT32 maxpasses;
+ OPJ_UINT32 numnewpasses;
+ OPJ_UINT32 newlen;
+} opj_tcd_seg_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_pass {
+ OPJ_UINT32 rate;
+ OPJ_FLOAT64 distortiondec;
+ OPJ_UINT32 len;
+ OPJ_UINT32 term : 1;
+} opj_tcd_pass_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_layer {
+ OPJ_UINT32 numpasses; /* Number of passes in the layer */
+ OPJ_UINT32 len; /* len of information */
+ OPJ_FLOAT64 disto; /* add for index (Cfr. Marcela) */
+ OPJ_BYTE *data; /* data */
+} opj_tcd_layer_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_cblk_enc {
+ OPJ_BYTE* data; /* Data */
+ opj_tcd_layer_t* layers; /* layer information */
+ opj_tcd_pass_t* passes; /* information about the passes */
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 numbps;
+ OPJ_UINT32 numlenbits;
+ OPJ_UINT32 numpasses; /* number of pass already done for the code-blocks */
+ OPJ_UINT32 numpassesinlayers; /* number of passes in the layer */
+ OPJ_UINT32 totalpasses; /* total number of passes */
+} opj_tcd_cblk_enc_t;
+
+
+typedef struct opj_tcd_cblk_dec {
+ OPJ_BYTE * data; /* Data */
+ opj_tcd_seg_t* segs; /* segments information */
+ OPJ_INT32 x0, y0, x1, y1; /* position of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 numbps;
+ OPJ_UINT32 numlenbits;
+ OPJ_UINT32 data_max_size; /* Size of allocated data buffer */
+ OPJ_UINT32 data_current_size; /* Size of used data buffer */
+ OPJ_UINT32 numnewpasses; /* number of pass added to the code-blocks */
+ OPJ_UINT32 numsegs; /* number of segments */
+ OPJ_UINT32 real_num_segs;
+ OPJ_UINT32 m_current_max_segs;
+} opj_tcd_cblk_dec_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_precinct {
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 cw, ch; /* number of precinct in width and height */
+ union{ /* code-blocks information */
+ opj_tcd_cblk_enc_t* enc;
+ opj_tcd_cblk_dec_t* dec;
+ } cblks;
+ OPJ_UINT32 block_size; /* size taken by cblks (in bytes) */
+ opj_tgt_tree_t *incltree; /* inclusion tree */
+ opj_tgt_tree_t *imsbtree; /* IMSB tree */
+} opj_tcd_precinct_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_band {
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 bandno;
+ opj_tcd_precinct_t *precincts; /* precinct information */
+ OPJ_UINT32 precincts_data_size; /* size of data taken by precincts */
+ OPJ_INT32 numbps;
+ OPJ_FLOAT32 stepsize;
+} opj_tcd_band_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_resolution {
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 pw, ph;
+ OPJ_UINT32 numbands; /* number sub-band for the resolution level */
+ opj_tcd_band_t bands[3]; /* subband information */
+} opj_tcd_resolution_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_tilecomp
+{
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 numresolutions; /* number of resolutions level */
+ OPJ_UINT32 minimum_num_resolutions; /* number of resolutions level to decode (at max)*/
+ opj_tcd_resolution_t *resolutions; /* resolutions information */
+ OPJ_UINT32 resolutions_size; /* size of data for resolutions (in bytes) */
+ OPJ_INT32 *data; /* data of the component */
+ OPJ_UINT32 data_size; /* size of the data of the component */
+ OPJ_INT32 numpix; /* add fixed_quality */
+} opj_tcd_tilecomp_t;
+
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_tile {
+ OPJ_INT32 x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
+ OPJ_UINT32 numcomps; /* number of components in tile */
+ opj_tcd_tilecomp_t *comps; /* Components information */
+ OPJ_INT32 numpix; /* add fixed_quality */
+ OPJ_FLOAT64 distotile; /* add fixed_quality */
+ OPJ_FLOAT64 distolayer[100]; /* add fixed_quality */
+ OPJ_UINT32 packno; /* packet number */
+} opj_tcd_tile_t;
+
+/**
+FIXME DOC
+*/
+typedef struct opj_tcd_image
+{
+ opj_tcd_tile_t *tiles; /* Tiles information */
+}
+opj_tcd_image_t;
+
+
+/**
+Tile coder/decoder
+*/
+typedef struct opj_tcd
+{
+ /** Position of the tilepart flag in Progression order*/
+ OPJ_INT32 tp_pos;
+ /** Tile part number*/
+ OPJ_UINT32 tp_num;
+ /** Current tile part number*/
+ OPJ_UINT32 cur_tp_num;
+ /** Total number of tileparts of the current tile*/
+ OPJ_UINT32 cur_totnum_tp;
+ /** Current Packet iterator number */
+ OPJ_UINT32 cur_pino;
+ /** info on each image tile */
+ opj_tcd_image_t *tcd_image;
+ /** image header */
+ opj_image_t *image;
+ /** coding parameters */
+ opj_cp_t *cp;
+ /** coding/decoding parameters common to all tiles */
+ opj_tcp_t *tcp;
+ /** current encoded/decoded tile */
+ OPJ_UINT32 tcd_tileno;
+ /** tell if the tcd is a decoder. */
+ OPJ_UINT32 m_is_decoder : 1;
+} opj_tcd_t;
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+
+/**
+Dump the content of a tcd structure
+*/
+/*void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);*/ /* TODO MSD shoul use the new v2 structures */
+
+/**
+Create a new TCD handle
+@param p_is_decoder FIXME DOC
+@return Returns a new TCD handle if successful returns NULL otherwise
+*/
+opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder);
+
+/**
+Destroy a previously created TCD handle
+@param tcd TCD handle to destroy
+*/
+void opj_tcd_destroy(opj_tcd_t *tcd);
+
+/**
+ * Initialize the tile coder and may reuse some memory.
+ * @param p_tcd TCD handle.
+ * @param p_image raw image.
+ * @param p_cp coding parameters.
+ *
+ * @return true if the encoding values could be set (false otherwise).
+*/
+OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
+ opj_image_t * p_image,
+ opj_cp_t * p_cp );
+
+/**
+ * Allocates memory for decoding a specific tile.
+ *
+ * @param p_tcd the tile decoder.
+ * @param p_tile_no the index of the tile received in sequence. This not necessarily lead to the
+ * tile at index p_tile_no.
+ *
+ * @return true if the remaining data is sufficient.
+ */
+OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no);
+
+void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final);
+
+void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd);
+
+void opj_tcd_makelayer( opj_tcd_t *tcd,
+ OPJ_UINT32 layno,
+ OPJ_FLOAT64 thresh,
+ OPJ_UINT32 final);
+
+OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
+ OPJ_BYTE *dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 len,
+ opj_codestream_info_t *cstr_info);
+
+/**
+ * Gets the maximum tile size that will be taken by the tile once decoded.
+ */
+OPJ_UINT32 opj_tcd_get_decoded_tile_size (opj_tcd_t *p_tcd );
+
+/**
+ * Encodes a tile from the raw image into the given buffer.
+ * @param p_tcd Tile Coder handle
+ * @param p_tile_no Index of the tile to encode.
+ * @param p_dest Destination buffer
+ * @param p_data_written pointer to an int that is incremented by the number of bytes really written on p_dest
+ * @param p_len Maximum length of the destination buffer
+ * @param p_cstr_info Codestream information structure
+ * @return true if the coding is successfull.
+*/
+OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
+ OPJ_UINT32 p_tile_no,
+ OPJ_BYTE *p_dest,
+ OPJ_UINT32 * p_data_written,
+ OPJ_UINT32 p_len,
+ struct opj_codestream_info *p_cstr_info);
+
+
+/**
+Decode a tile from a buffer into a raw image
+@param tcd TCD handle
+@param src Source buffer
+@param len Length of source buffer
+@param tileno Number that identifies one of the tiles to be decoded
+@param cstr_info FIXME DOC
+*/
+OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *tcd,
+ OPJ_BYTE *src,
+ OPJ_UINT32 len,
+ OPJ_UINT32 tileno,
+ opj_codestream_index_t *cstr_info);
+
+
+/**
+ * Copies tile data from the system onto the given memory block.
+ */
+OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_dest,
+ OPJ_UINT32 p_dest_length );
+
+/**
+ *
+ */
+OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd );
+
+/**
+ * Initialize the tile coder and may reuse some meory.
+ *
+ * @param p_tcd TCD handle.
+ * @param p_tile_no current tile index to encode.
+ *
+ * @return true if the encoding values could be set (false otherwise).
+*/
+OPJ_BOOL opj_tcd_init_encode_tile ( opj_tcd_t *p_tcd,
+ OPJ_UINT32 p_tile_no );
+
+/**
+ * Copies tile data from the given memory block onto the system.
+ */
+OPJ_BOOL opj_tcd_copy_tile_data (opj_tcd_t *p_tcd,
+ OPJ_BYTE * p_src,
+ OPJ_UINT32 p_src_length );
+
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __TCD_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.c b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.c
new file mode 100644
index 0000000000..e77adb3bb8
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.c
@@ -0,0 +1,337 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/*
+==========================================================
+ Tag-tree coder interface
+==========================================================
+*/
+
+opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv) {
+ OPJ_INT32 nplh[32];
+ OPJ_INT32 nplv[32];
+ opj_tgt_node_t *node = 00;
+ opj_tgt_node_t *l_parent_node = 00;
+ opj_tgt_node_t *l_parent_node0 = 00;
+ opj_tgt_tree_t *tree = 00;
+ OPJ_UINT32 i;
+ OPJ_INT32 j,k;
+ OPJ_UINT32 numlvls;
+ OPJ_UINT32 n;
+
+ tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
+ if(!tree) {
+ fprintf(stderr, "ERROR in tgt_create while allocating tree\n");
+ return 00;
+ }
+ memset(tree,0,sizeof(opj_tgt_tree_t));
+
+ tree->numleafsh = numleafsh;
+ tree->numleafsv = numleafsv;
+
+ numlvls = 0;
+ nplh[0] = (OPJ_INT32)numleafsh;
+ nplv[0] = (OPJ_INT32)numleafsv;
+ tree->numnodes = 0;
+ do {
+ n = (OPJ_UINT32)(nplh[numlvls] * nplv[numlvls]);
+ nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
+ nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
+ tree->numnodes += n;
+ ++numlvls;
+ } while (n > 1);
+
+ /* ADD */
+ if (tree->numnodes == 0) {
+ opj_free(tree);
+ fprintf(stderr, "WARNING in tgt_create tree->numnodes == 0, no tree created.\n");
+ return 00;
+ }
+
+ tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t));
+ if(!tree->nodes) {
+ fprintf(stderr, "ERROR in tgt_create while allocating node of the tree\n");
+ opj_free(tree);
+ return 00;
+ }
+ memset(tree->nodes,0,tree->numnodes * sizeof(opj_tgt_node_t));
+ tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
+
+ node = tree->nodes;
+ l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv];
+ l_parent_node0 = l_parent_node;
+
+ for (i = 0; i < numlvls - 1; ++i) {
+ for (j = 0; j < nplv[i]; ++j) {
+ k = nplh[i];
+ while (--k >= 0) {
+ node->parent = l_parent_node;
+ ++node;
+ if (--k >= 0) {
+ node->parent = l_parent_node;
+ ++node;
+ }
+ ++l_parent_node;
+ }
+ if ((j & 1) || j == nplv[i] - 1) {
+ l_parent_node0 = l_parent_node;
+ } else {
+ l_parent_node = l_parent_node0;
+ l_parent_node0 += nplh[i];
+ }
+ }
+ }
+ node->parent = 0;
+ opj_tgt_reset(tree);
+ return tree;
+}
+
+/**
+ * Reinitialises a tag-tree from an existing one.
+ *
+ * @param p_tree the tree to reinitialize.
+ * @param p_num_leafs_h the width of the array of leafs of the tree
+ * @param p_num_leafs_v the height of the array of leafs of the tree
+ * @return a new tag-tree if successful, NULL otherwise
+*/
+opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v)
+{
+ OPJ_INT32 l_nplh[32];
+ OPJ_INT32 l_nplv[32];
+ opj_tgt_node_t *l_node = 00;
+ opj_tgt_node_t *l_parent_node = 00;
+ opj_tgt_node_t *l_parent_node0 = 00;
+ OPJ_UINT32 i;
+ OPJ_INT32 j,k;
+ OPJ_UINT32 l_num_levels;
+ OPJ_UINT32 n;
+ OPJ_UINT32 l_node_size;
+
+ if (! p_tree){
+ return 00;
+ }
+
+ if ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v)) {
+ p_tree->numleafsh = p_num_leafs_h;
+ p_tree->numleafsv = p_num_leafs_v;
+
+ l_num_levels = 0;
+ l_nplh[0] = (OPJ_INT32)p_num_leafs_h;
+ l_nplv[0] = (OPJ_INT32)p_num_leafs_v;
+ p_tree->numnodes = 0;
+ do
+ {
+ n = (OPJ_UINT32)(l_nplh[l_num_levels] * l_nplv[l_num_levels]);
+ l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
+ l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
+ p_tree->numnodes += n;
+ ++l_num_levels;
+ }
+ while (n > 1);
+
+ /* ADD */
+ if (p_tree->numnodes == 0) {
+ opj_tgt_destroy(p_tree);
+ return 00;
+ }
+ l_node_size = p_tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
+
+ if (l_node_size > p_tree->nodes_size) {
+ opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size);
+ if (! new_nodes) {
+ fprintf(stderr, "ERROR Not enough memory to reinitialize the tag tree\n");
+ opj_tgt_destroy(p_tree);
+ return 00;
+ }
+ p_tree->nodes = new_nodes;
+ memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size);
+ p_tree->nodes_size = l_node_size;
+ }
+ l_node = p_tree->nodes;
+ l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
+ l_parent_node0 = l_parent_node;
+
+ for (i = 0; i < l_num_levels - 1; ++i) {
+ for (j = 0; j < l_nplv[i]; ++j) {
+ k = l_nplh[i];
+ while (--k >= 0) {
+ l_node->parent = l_parent_node;
+ ++l_node;
+ if (--k >= 0) {
+ l_node->parent = l_parent_node;
+ ++l_node;
+ }
+ ++l_parent_node;
+ }
+ if ((j & 1) || j == l_nplv[i] - 1)
+ {
+ l_parent_node0 = l_parent_node;
+ }
+ else
+ {
+ l_parent_node = l_parent_node0;
+ l_parent_node0 += l_nplh[i];
+ }
+ }
+ }
+ l_node->parent = 0;
+ }
+ opj_tgt_reset(p_tree);
+
+ return p_tree;
+}
+
+void opj_tgt_destroy(opj_tgt_tree_t *p_tree)
+{
+ if (! p_tree) {
+ return;
+ }
+
+ if (p_tree->nodes) {
+ opj_free(p_tree->nodes);
+ p_tree->nodes = 00;
+ }
+ opj_free(p_tree);
+}
+
+void opj_tgt_reset(opj_tgt_tree_t *p_tree) {
+ OPJ_UINT32 i;
+ opj_tgt_node_t * l_current_node = 00;;
+
+ if (! p_tree) {
+ return;
+ }
+
+ l_current_node = p_tree->nodes;
+ for (i = 0; i < p_tree->numnodes; ++i)
+ {
+ l_current_node->value = 999;
+ l_current_node->low = 0;
+ l_current_node->known = 0;
+ ++l_current_node;
+ }
+}
+
+void opj_tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value) {
+ opj_tgt_node_t *node;
+ node = &tree->nodes[leafno];
+ while (node && node->value > value) {
+ node->value = value;
+ node = node->parent;
+ }
+}
+
+void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) {
+ opj_tgt_node_t *stk[31];
+ opj_tgt_node_t **stkptr;
+ opj_tgt_node_t *node;
+ OPJ_INT32 low;
+
+ stkptr = stk;
+ node = &tree->nodes[leafno];
+ while (node->parent) {
+ *stkptr++ = node;
+ node = node->parent;
+ }
+
+ low = 0;
+ for (;;) {
+ if (low > node->low) {
+ node->low = low;
+ } else {
+ low = node->low;
+ }
+
+ while (low < threshold) {
+ if (low >= node->value) {
+ if (!node->known) {
+ opj_bio_write(bio, 1, 1);
+ node->known = 1;
+ }
+ break;
+ }
+ opj_bio_write(bio, 0, 1);
+ ++low;
+ }
+
+ node->low = low;
+ if (stkptr == stk)
+ break;
+ node = *--stkptr;
+ }
+}
+
+OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) {
+ opj_tgt_node_t *stk[31];
+ opj_tgt_node_t **stkptr;
+ opj_tgt_node_t *node;
+ OPJ_INT32 low;
+
+ stkptr = stk;
+ node = &tree->nodes[leafno];
+ while (node->parent) {
+ *stkptr++ = node;
+ node = node->parent;
+ }
+
+ low = 0;
+ for (;;) {
+ if (low > node->low) {
+ node->low = low;
+ } else {
+ low = node->low;
+ }
+ while (low < threshold && low < node->value) {
+ if (opj_bio_read(bio, 1)) {
+ node->value = low;
+ } else {
+ ++low;
+ }
+ }
+ node->low = low;
+ if (stkptr == stk) {
+ break;
+ }
+ node = *--stkptr;
+ }
+
+ return (node->value < threshold) ? 1 : 0;
+}
diff --git a/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.h b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.h
new file mode 100644
index 0000000000..3d152f8a90
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/tgt.h
@@ -0,0 +1,146 @@
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2012, CS Systemes d'Information, France
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __TGT_H
+#define __TGT_H
+/**
+@file tgt.h
+@brief Implementation of a tag-tree coder (TGT)
+
+The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C
+are used by some function in T2.C.
+*/
+
+/** @defgroup TGT TGT - Implementation of a tag-tree coder */
+/*@{*/
+
+/**
+Tag node
+*/
+typedef struct opj_tgt_node {
+ struct opj_tgt_node *parent;
+ OPJ_INT32 value;
+ OPJ_INT32 low;
+ OPJ_UINT32 known;
+} opj_tgt_node_t;
+
+/**
+Tag tree
+*/
+typedef struct opj_tgt_tree
+{
+ OPJ_UINT32 numleafsh;
+ OPJ_UINT32 numleafsv;
+ OPJ_UINT32 numnodes;
+ opj_tgt_node_t *nodes;
+ OPJ_UINT32 nodes_size; /* maximum size taken by nodes */
+} opj_tgt_tree_t;
+
+
+/** @name Exported functions */
+/*@{*/
+/* ----------------------------------------------------------------------- */
+/**
+Create a tag-tree
+@param numleafsh Width of the array of leafs of the tree
+@param numleafsv Height of the array of leafs of the tree
+@return Returns a new tag-tree if successful, returns NULL otherwise
+*/
+opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv);
+
+/**
+ * Reinitialises a tag-tree from an exixting one.
+ *
+ * @param p_tree the tree to reinitialize.
+ * @param p_num_leafs_h the width of the array of leafs of the tree
+ * @param p_num_leafs_v the height of the array of leafs of the tree
+ * @return a new tag-tree if successful, NULL otherwise
+*/
+opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree,
+ OPJ_UINT32 p_num_leafs_h,
+ OPJ_UINT32 p_num_leafs_v);
+/**
+Destroy a tag-tree, liberating memory
+@param tree Tag-tree to destroy
+*/
+void opj_tgt_destroy(opj_tgt_tree_t *tree);
+/**
+Reset a tag-tree (set all leaves to 0)
+@param tree Tag-tree to reset
+*/
+void opj_tgt_reset(opj_tgt_tree_t *tree);
+/**
+Set the value of a leaf of a tag-tree
+@param tree Tag-tree to modify
+@param leafno Number that identifies the leaf to modify
+@param value New value of the leaf
+*/
+void opj_tgt_setvalue(opj_tgt_tree_t *tree,
+ OPJ_UINT32 leafno,
+ OPJ_INT32 value);
+/**
+Encode the value of a leaf of the tag-tree up to a given threshold
+@param bio Pointer to a BIO handle
+@param tree Tag-tree to modify
+@param leafno Number that identifies the leaf to encode
+@param threshold Threshold to use when encoding value of the leaf
+*/
+void opj_tgt_encode(opj_bio_t *bio,
+ opj_tgt_tree_t *tree,
+ OPJ_UINT32 leafno,
+ OPJ_INT32 threshold);
+/**
+Decode the value of a leaf of the tag-tree up to a given threshold
+@param bio Pointer to a BIO handle
+@param tree Tag-tree to decode
+@param leafno Number that identifies the leaf to decode
+@param threshold Threshold to use when decoding value of the leaf
+@return Returns 1 if the node's value < threshold, returns 0 otherwise
+*/
+OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio,
+ opj_tgt_tree_t *tree,
+ OPJ_UINT32 leafno,
+ OPJ_INT32 threshold);
+/* ----------------------------------------------------------------------- */
+/*@}*/
+
+/*@}*/
+
+#endif /* __TGT_H */
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_bio.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_bio.c
new file mode 100644
index 0000000000..57ed6f83a5
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_bio.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/bio.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_cio.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_cio.c
new file mode 100644
index 0000000000..5f70ee540c
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_cio.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/cio.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_dwt.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_dwt.c
new file mode 100644
index 0000000000..2dd428f1b7
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_dwt.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/dwt.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_event.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_event.c
new file mode 100644
index 0000000000..a7d9bcae10
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_event.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/event.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_function_list.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_function_list.c
new file mode 100644
index 0000000000..499b1419dd
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_function_list.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/function_list.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_image.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_image.c
new file mode 100644
index 0000000000..46b0faab35
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_image.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/image.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_invert.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_invert.c
new file mode 100644
index 0000000000..daa89ab2c3
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_invert.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/invert.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k.c
new file mode 100644
index 0000000000..c3ae6d42d6
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/j2k.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k_lib.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k_lib.c
new file mode 100644
index 0000000000..0aa62a05ba
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_j2k_lib.c
@@ -0,0 +1,9 @@
+// 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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_jpt.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_jpt.c
new file mode 100644
index 0000000000..0aa62a05ba
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_jpt.c
@@ -0,0 +1,9 @@
+// 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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_mct.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_mct.c
new file mode 100644
index 0000000000..d2a3861a98
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_mct.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/mct.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_mqc.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_mqc.c
new file mode 100644
index 0000000000..bda851de93
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_mqc.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/mqc.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg.c
new file mode 100644
index 0000000000..8addb90bb9
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/openjpeg.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg_jp2.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg_jp2.c
new file mode 100644
index 0000000000..4271dd0b86
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_openjpeg_jp2.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/jp2.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_opj_clock.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_opj_clock.c
new file mode 100644
index 0000000000..1aed471673
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_opj_clock.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/opj_clock.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_pi.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_pi.c
new file mode 100644
index 0000000000..034a1208bb
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_pi.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/pi.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_raw.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_raw.c
new file mode 100644
index 0000000000..c475df2b65
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_raw.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/raw.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_t1.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_t1.c
new file mode 100644
index 0000000000..ada8ae22ac
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_t1.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/t1.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_t1_generate_luts.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_t1_generate_luts.c
new file mode 100644
index 0000000000..a4b8890154
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_t1_generate_luts.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/t1_generate_luts.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_t2.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_t2.c
new file mode 100644
index 0000000000..4e5ca50783
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_t2.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/t2.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_tcd.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_tcd.c
new file mode 100644
index 0000000000..8d575701ec
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_tcd.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/tcd.c"
diff --git a/core/src/fxcodec/fx_libopenjpeg/src/fx_tgt.c b/core/src/fxcodec/fx_libopenjpeg/src/fx_tgt.c
new file mode 100644
index 0000000000..283dc54b79
--- /dev/null
+++ b/core/src/fxcodec/fx_libopenjpeg/src/fx_tgt.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../libopenjpeg20/tgt.c"
diff --git a/core/src/fxcodec/fx_zlib/include/fx_zlib.h b/core/src/fxcodec/fx_zlib/include/fx_zlib.h
new file mode 100644
index 0000000000..955481ce7b
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/include/fx_zlib.h
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/zlib.h"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_adler32.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_adler32.c
new file mode 100644
index 0000000000..38a35a03e7
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_adler32.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_adler32.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_compress.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_compress.c
new file mode 100644
index 0000000000..c88a675922
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_compress.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_compress.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_crc32.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_crc32.c
new file mode 100644
index 0000000000..e7f7453519
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_crc32.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_crc32.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_deflate.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_deflate.c
new file mode 100644
index 0000000000..377779aedc
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_deflate.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_deflate.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_gzclose.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzclose.c
new file mode 100644
index 0000000000..726cbc2390
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzclose.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/gzclose.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_gzlib.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzlib.c
new file mode 100644
index 0000000000..3e95454682
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzlib.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/gzlib.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_gzread.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzread.c
new file mode 100644
index 0000000000..2c14ff7468
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzread.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/gzread.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_gzwrite.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzwrite.c
new file mode 100644
index 0000000000..2a37fed390
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_gzwrite.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/gzwrite.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_infback.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_infback.c
new file mode 100644
index 0000000000..17bc781d06
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_infback.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_infback.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_inffast.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_inffast.c
new file mode 100644
index 0000000000..a6828cc424
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_inffast.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_inffast.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_inflate.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_inflate.c
new file mode 100644
index 0000000000..f28a389c07
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_inflate.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_inflate.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_inftrees.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_inftrees.c
new file mode 100644
index 0000000000..9abe3f0570
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_inftrees.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_inftrees.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_trees.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_trees.c
new file mode 100644
index 0000000000..05e175b33f
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_trees.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_trees.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_uncompr.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_uncompr.c
new file mode 100644
index 0000000000..451cb6fa85
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_uncompr.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_uncompr.c"
diff --git a/core/src/fxcodec/fx_zlib/src/fx_zlib_zutil.c b/core/src/fxcodec/fx_zlib/src/fx_zlib_zutil.c
new file mode 100644
index 0000000000..f97edd50fd
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/src/fx_zlib_zutil.c
@@ -0,0 +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
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include "../zlib_v128/fx_zlib_zutil.c"
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/crc32.h b/core/src/fxcodec/fx_zlib/zlib_v128/crc32.h
new file mode 100644
index 0000000000..9e0c778102
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/crc32.h
@@ -0,0 +1,441 @@
+/* crc32.h -- tables for rapid CRC calculation
+ * Generated automatically by crc32.c
+ */
+
+local const z_crc_t FAR crc_table[TBLS][256] =
+{
+ {
+ 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
+ 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
+ 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
+ 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
+ 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
+ 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
+ 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
+ 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
+ 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
+ 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
+ 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
+ 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
+ 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
+ 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
+ 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
+ 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
+ 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
+ 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
+ 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
+ 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
+ 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
+ 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
+ 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
+ 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
+ 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
+ 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
+ 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
+ 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
+ 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
+ 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
+ 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
+ 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
+ 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
+ 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
+ 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
+ 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
+ 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
+ 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
+ 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
+ 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
+ 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
+ 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
+ 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
+ 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
+ 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
+ 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
+ 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
+ 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
+ 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
+ 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
+ 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
+ 0x2d02ef8dUL
+#ifdef BYFOUR
+ },
+ {
+ 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
+ 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
+ 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
+ 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
+ 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
+ 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
+ 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
+ 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
+ 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
+ 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
+ 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
+ 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
+ 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
+ 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
+ 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
+ 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
+ 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
+ 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
+ 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
+ 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
+ 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
+ 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
+ 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
+ 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
+ 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
+ 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
+ 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
+ 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
+ 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
+ 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
+ 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
+ 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
+ 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
+ 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
+ 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
+ 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
+ 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
+ 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
+ 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
+ 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
+ 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
+ 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
+ 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
+ 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
+ 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
+ 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
+ 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
+ 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
+ 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
+ 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
+ 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
+ 0x9324fd72UL
+ },
+ {
+ 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
+ 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
+ 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
+ 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
+ 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
+ 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
+ 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
+ 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
+ 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
+ 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
+ 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
+ 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
+ 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
+ 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
+ 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
+ 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
+ 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
+ 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
+ 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
+ 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
+ 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
+ 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
+ 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
+ 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
+ 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
+ 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
+ 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
+ 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
+ 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
+ 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
+ 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
+ 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
+ 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
+ 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
+ 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
+ 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
+ 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
+ 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
+ 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
+ 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
+ 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
+ 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
+ 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
+ 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
+ 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
+ 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
+ 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
+ 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
+ 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
+ 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
+ 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
+ 0xbe9834edUL
+ },
+ {
+ 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
+ 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
+ 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
+ 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
+ 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
+ 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
+ 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
+ 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
+ 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
+ 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
+ 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
+ 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
+ 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
+ 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
+ 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
+ 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
+ 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
+ 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
+ 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
+ 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
+ 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
+ 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
+ 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
+ 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
+ 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
+ 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
+ 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
+ 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
+ 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
+ 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
+ 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
+ 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
+ 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
+ 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
+ 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
+ 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
+ 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
+ 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
+ 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
+ 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
+ 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
+ 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
+ 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
+ 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
+ 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
+ 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
+ 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
+ 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
+ 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
+ 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
+ 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
+ 0xde0506f1UL
+ },
+ {
+ 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
+ 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
+ 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
+ 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
+ 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
+ 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
+ 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
+ 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
+ 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
+ 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
+ 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
+ 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
+ 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
+ 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
+ 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
+ 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
+ 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
+ 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
+ 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
+ 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
+ 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
+ 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
+ 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
+ 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
+ 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
+ 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
+ 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
+ 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
+ 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
+ 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
+ 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
+ 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
+ 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
+ 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
+ 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
+ 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
+ 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
+ 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
+ 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
+ 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
+ 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
+ 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
+ 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
+ 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
+ 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
+ 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
+ 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
+ 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
+ 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
+ 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
+ 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
+ 0x8def022dUL
+ },
+ {
+ 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
+ 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
+ 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
+ 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
+ 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
+ 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
+ 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
+ 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
+ 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
+ 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
+ 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
+ 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
+ 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
+ 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
+ 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
+ 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
+ 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
+ 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
+ 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
+ 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
+ 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
+ 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
+ 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
+ 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
+ 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
+ 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
+ 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
+ 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
+ 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
+ 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
+ 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
+ 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
+ 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
+ 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
+ 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
+ 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
+ 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
+ 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
+ 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
+ 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
+ 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
+ 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
+ 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
+ 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
+ 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
+ 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
+ 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
+ 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
+ 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
+ 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
+ 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
+ 0x72fd2493UL
+ },
+ {
+ 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
+ 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
+ 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
+ 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
+ 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
+ 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
+ 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
+ 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
+ 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
+ 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
+ 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
+ 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
+ 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
+ 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
+ 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
+ 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
+ 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
+ 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
+ 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
+ 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
+ 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
+ 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
+ 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
+ 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
+ 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
+ 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
+ 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
+ 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
+ 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
+ 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
+ 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
+ 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
+ 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
+ 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
+ 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
+ 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
+ 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
+ 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
+ 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
+ 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
+ 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
+ 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
+ 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
+ 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
+ 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
+ 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
+ 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
+ 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
+ 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
+ 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
+ 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
+ 0xed3498beUL
+ },
+ {
+ 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
+ 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
+ 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
+ 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
+ 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
+ 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
+ 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
+ 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
+ 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
+ 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
+ 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
+ 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
+ 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
+ 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
+ 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
+ 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
+ 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
+ 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
+ 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
+ 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
+ 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
+ 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
+ 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
+ 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
+ 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
+ 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
+ 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
+ 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
+ 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
+ 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
+ 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
+ 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
+ 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
+ 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
+ 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
+ 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
+ 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
+ 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
+ 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
+ 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
+ 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
+ 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
+ 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
+ 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
+ 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
+ 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
+ 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
+ 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
+ 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
+ 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
+ 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
+ 0xf10605deUL
+#endif
+ }
+};
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/deflate.h b/core/src/fxcodec/fx_zlib/zlib_v128/deflate.h
new file mode 100644
index 0000000000..ce0299edd1
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/deflate.h
@@ -0,0 +1,346 @@
+/* deflate.h -- internal compression state
+ * Copyright (C) 1995-2012 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* @(#) $Id$ */
+
+#ifndef DEFLATE_H
+#define DEFLATE_H
+
+#include "zutil.h"
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer creation by deflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip encoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GZIP
+#endif
+
+/* ===========================================================================
+ * Internal compression state.
+ */
+
+#define LENGTH_CODES 29
+/* number of length codes, not counting the special END_BLOCK code */
+
+#define LITERALS 256
+/* number of literal bytes 0..255 */
+
+#define L_CODES (LITERALS+1+LENGTH_CODES)
+/* number of Literal or Length codes, including the END_BLOCK code */
+
+#define D_CODES 30
+/* number of distance codes */
+
+#define BL_CODES 19
+/* number of codes used to transfer the bit lengths */
+
+#define HEAP_SIZE (2*L_CODES+1)
+/* maximum heap size */
+
+#define MAX_BITS 15
+/* All codes must not exceed MAX_BITS bits */
+
+#define Buf_size 16
+/* size of bit buffer in bi_buf */
+
+#define INIT_STATE 42
+#define EXTRA_STATE 69
+#define NAME_STATE 73
+#define COMMENT_STATE 91
+#define HCRC_STATE 103
+#define BUSY_STATE 113
+#define FINISH_STATE 666
+/* Stream status */
+
+
+/* Data structure describing a single value and its code string. */
+typedef struct ct_data_s {
+ union {
+ ush freq; /* frequency count */
+ ush code; /* bit string */
+ } fc;
+ union {
+ ush dad; /* father node in Huffman tree */
+ ush len; /* length of bit string */
+ } dl;
+} FAR ct_data;
+
+#define Freq fc.freq
+#define Code fc.code
+#define Dad dl.dad
+#define Len dl.len
+
+typedef struct static_tree_desc_s static_tree_desc;
+
+typedef struct tree_desc_s {
+ ct_data *dyn_tree; /* the dynamic tree */
+ int max_code; /* largest code with non zero frequency */
+ static_tree_desc *stat_desc; /* the corresponding static tree */
+} FAR tree_desc;
+
+typedef ush Pos;
+typedef Pos FAR Posf;
+typedef unsigned IPos;
+
+/* A Pos is an index in the character window. We use short instead of int to
+ * save space in the various tables. IPos is used only for parameter passing.
+ */
+
+typedef struct internal_state {
+ z_streamp strm; /* pointer back to this zlib stream */
+ int status; /* as the name implies */
+ Bytef *pending_buf; /* output still pending */
+ ulg pending_buf_size; /* size of pending_buf */
+ Bytef *pending_out; /* next pending byte to output to the stream */
+ uInt pending; /* nb of bytes in the pending buffer */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
+ gz_headerp gzhead; /* gzip header information to write */
+ uInt gzindex; /* where in extra, name, or comment */
+ Byte method; /* can only be DEFLATED */
+ int last_flush; /* value of flush param for previous deflate call */
+
+ /* used by deflate.c: */
+
+ uInt w_size; /* LZ77 window size (32K by default) */
+ uInt w_bits; /* log2(w_size) (8..16) */
+ uInt w_mask; /* w_size - 1 */
+
+ Bytef *window;
+ /* Sliding window. Input bytes are read into the second half of the window,
+ * and move to the first half later to keep a dictionary of at least wSize
+ * bytes. With this organization, matches are limited to a distance of
+ * wSize-MAX_MATCH bytes, but this ensures that IO is always
+ * performed with a length multiple of the block size. Also, it limits
+ * the window size to 64K, which is quite useful on MSDOS.
+ * To do: use the user input buffer as sliding window.
+ */
+
+ ulg window_size;
+ /* Actual size of window: 2*wSize, except when the user input buffer
+ * is directly used as sliding window.
+ */
+
+ Posf *prev;
+ /* Link to older string with same hash index. To limit the size of this
+ * array to 64K, this link is maintained only for the last 32K strings.
+ * An index in this array is thus a window index modulo 32K.
+ */
+
+ Posf *head; /* Heads of the hash chains or NIL. */
+
+ uInt ins_h; /* hash index of string to be inserted */
+ uInt hash_size; /* number of elements in hash table */
+ uInt hash_bits; /* log2(hash_size) */
+ uInt hash_mask; /* hash_size-1 */
+
+ uInt hash_shift;
+ /* Number of bits by which ins_h must be shifted at each input
+ * step. It must be such that after MIN_MATCH steps, the oldest
+ * byte no longer takes part in the hash key, that is:
+ * hash_shift * MIN_MATCH >= hash_bits
+ */
+
+ long block_start;
+ /* Window position at the beginning of the current output block. Gets
+ * negative when the window is moved backwards.
+ */
+
+ uInt match_length; /* length of best match */
+ IPos prev_match; /* previous match */
+ int match_available; /* set if previous match exists */
+ uInt strstart; /* start of string to insert */
+ uInt match_start; /* start of matching string */
+ uInt lookahead; /* number of valid bytes ahead in window */
+
+ uInt prev_length;
+ /* Length of the best match at previous step. Matches not greater than this
+ * are discarded. This is used in the lazy match evaluation.
+ */
+
+ uInt max_chain_length;
+ /* To speed up deflation, hash chains are never searched beyond this
+ * length. A higher limit improves compression ratio but degrades the
+ * speed.
+ */
+
+ uInt max_lazy_match;
+ /* Attempt to find a better match only when the current match is strictly
+ * smaller than this value. This mechanism is used only for compression
+ * levels >= 4.
+ */
+# define max_insert_length max_lazy_match
+ /* Insert new strings in the hash table only if the match length is not
+ * greater than this length. This saves time but degrades compression.
+ * max_insert_length is used only for compression levels <= 3.
+ */
+
+ int level; /* compression level (1..9) */
+ int strategy; /* favor or force Huffman coding*/
+
+ uInt good_match;
+ /* Use a faster search when the previous match is longer than this */
+
+ int nice_match; /* Stop searching when current match exceeds this */
+
+ /* used by trees.c: */
+ /* Didn't use ct_data typedef below to suppress compiler warning */
+ struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
+ struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
+ struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
+
+ struct tree_desc_s l_desc; /* desc. for literal tree */
+ struct tree_desc_s d_desc; /* desc. for distance tree */
+ struct tree_desc_s bl_desc; /* desc. for bit length tree */
+
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
+ int heap_len; /* number of elements in the heap */
+ int heap_max; /* element of largest frequency */
+ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
+ * The same heap array is used to build all trees.
+ */
+
+ uch depth[2*L_CODES+1];
+ /* Depth of each subtree used as tie breaker for trees of equal frequency
+ */
+
+ uchf *l_buf; /* buffer for literals or lengths */
+
+ uInt lit_bufsize;
+ /* Size of match buffer for literals/lengths. There are 4 reasons for
+ * limiting lit_bufsize to 64K:
+ * - frequencies can be kept in 16 bit counters
+ * - if compression is not successful for the first block, all input
+ * data is still in the window so we can still emit a stored block even
+ * when input comes from standard input. (This can also be done for
+ * all blocks if lit_bufsize is not greater than 32K.)
+ * - if compression is not successful for a file smaller than 64K, we can
+ * even emit a stored file instead of a stored block (saving 5 bytes).
+ * This is applicable only for zip (not gzip or zlib).
+ * - creating new Huffman trees less frequently may not provide fast
+ * adaptation to changes in the input data statistics. (Take for
+ * example a binary file with poorly compressible code followed by
+ * a highly compressible string table.) Smaller buffer sizes give
+ * fast adaptation but have of course the overhead of transmitting
+ * trees more frequently.
+ * - I can't count above 4
+ */
+
+ uInt last_lit; /* running index in l_buf */
+
+ ushf *d_buf;
+ /* Buffer for distances. To simplify the code, d_buf and l_buf have
+ * the same number of elements. To use different lengths, an extra flag
+ * array would be necessary.
+ */
+
+ ulg opt_len; /* bit length of current block with optimal trees */
+ ulg static_len; /* bit length of current block with static trees */
+ uInt matches; /* number of string matches in current block */
+ uInt insert; /* bytes at end of window left to insert */
+
+#ifdef DEBUG
+ ulg compressed_len; /* total bit length of compressed file mod 2^32 */
+ ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
+#endif
+
+ ush bi_buf;
+ /* Output buffer. bits are inserted starting at the bottom (least
+ * significant bits).
+ */
+ int bi_valid;
+ /* Number of valid bits in bi_buf. All bits above the last valid bit
+ * are always zero.
+ */
+
+ ulg high_water;
+ /* High water mark offset in window for initialized bytes -- bytes above
+ * this are set to zero in order to avoid memory check warnings when
+ * longest match routines access bytes past the input. This is then
+ * updated to the new high water mark.
+ */
+
+} FAR deflate_state;
+
+/* Output a byte on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
+
+
+#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
+/* Minimum amount of lookahead, except at the end of the input file.
+ * See deflate.c for comments about the MIN_MATCH+1.
+ */
+
+#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
+/* In order to simplify the code, particularly on 16 bit machines, match
+ * distances are limited to MAX_DIST instead of WSIZE.
+ */
+
+#define WIN_INIT MAX_MATCH
+/* Number of bytes after end of data in window to initialize in order to avoid
+ memory checker errors from longest match routines */
+
+ /* in trees.c */
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
+ ulg stored_len, int last));
+
+#define d_code(dist) \
+ ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
+/* Mapping from a distance to a distance code. dist is the distance - 1 and
+ * must not have side effects. _dist_code[256] and _dist_code[257] are never
+ * used.
+ */
+
+#ifndef DEBUG
+/* Inline versions of _tr_tally for speed: */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+ extern uch ZLIB_INTERNAL _length_code[];
+ extern uch ZLIB_INTERNAL _dist_code[];
+#else
+ extern const uch ZLIB_INTERNAL _length_code[];
+ extern const uch ZLIB_INTERNAL _dist_code[];
+#endif
+
+# define _tr_tally_lit(s, c, flush) \
+ { uch cc = (c); \
+ s->d_buf[s->last_lit] = 0; \
+ s->l_buf[s->last_lit++] = cc; \
+ s->dyn_ltree[cc].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+# define _tr_tally_dist(s, distance, length, flush) \
+ { uch len = (length); \
+ ush dist = (distance); \
+ s->d_buf[s->last_lit] = dist; \
+ s->l_buf[s->last_lit++] = len; \
+ dist--; \
+ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
+ s->dyn_dtree[d_code(dist)].Freq++; \
+ flush = (s->last_lit == s->lit_bufsize-1); \
+ }
+#else
+# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
+# define _tr_tally_dist(s, distance, length, flush) \
+ flush = _tr_tally(s, distance, length)
+#endif
+
+#endif /* DEFLATE_H */
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_adler32.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_adler32.c
new file mode 100644
index 0000000000..82a84ceed2
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_adler32.c
@@ -0,0 +1,179 @@
+/* adler32.c -- compute the Adler-32 checksum of a data stream
+ * Copyright (C) 1995-2011 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+
+#define local static
+
+local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
+
+#define BASE 65521 /* largest prime smaller than 65536 */
+#define NMAX 5552
+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
+
+#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
+#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf) DO8(buf,0); DO8(buf,8);
+
+/* use NO_DIVIDE if your processor does not do division in hardware --
+ try it both ways to see which is faster */
+#ifdef NO_DIVIDE
+/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
+ (thank you to John Reiser for pointing this out) */
+# define CHOP(a) \
+ do { \
+ unsigned long tmp = a >> 16; \
+ a &= 0xffffUL; \
+ a += (tmp << 4) - tmp; \
+ } while (0)
+# define MOD28(a) \
+ do { \
+ CHOP(a); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+# define MOD(a) \
+ do { \
+ CHOP(a); \
+ MOD28(a); \
+ } while (0)
+# define MOD63(a) \
+ do { /* this assumes a is not negative */ \
+ z_off64_t tmp = a >> 32; \
+ a &= 0xffffffffL; \
+ a += (tmp << 8) - (tmp << 5) + tmp; \
+ tmp = a >> 16; \
+ a &= 0xffffL; \
+ a += (tmp << 4) - tmp; \
+ tmp = a >> 16; \
+ a &= 0xffffL; \
+ a += (tmp << 4) - tmp; \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+#else
+# define MOD(a) a %= BASE
+# define MOD28(a) a %= BASE
+# define MOD63(a) a %= BASE
+#endif
+
+/* ========================================================================= */
+uLong ZEXPORT adler32(
+ uLong adler,
+ const Bytef *buf,
+ uInt len)
+{
+ unsigned long sum2;
+ unsigned n;
+
+ /* split Adler-32 into component sums */
+ sum2 = (adler >> 16) & 0xffff;
+ adler &= 0xffff;
+
+ /* in case user likes doing a byte at a time, keep it fast */
+ if (len == 1) {
+ adler += buf[0];
+ if (adler >= BASE)
+ adler -= BASE;
+ sum2 += adler;
+ if (sum2 >= BASE)
+ sum2 -= BASE;
+ return adler | (sum2 << 16);
+ }
+
+ /* initial Adler-32 value (deferred check for len == 1 speed) */
+ if (buf == Z_NULL)
+ return 1L;
+
+ /* in case short lengths are provided, keep it somewhat fast */
+ if (len < 16) {
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ if (adler >= BASE)
+ adler -= BASE;
+ MOD28(sum2); /* only added so many BASE's */
+ return adler | (sum2 << 16);
+ }
+
+ /* do length NMAX blocks -- requires just one modulo operation */
+ while (len >= NMAX) {
+ len -= NMAX;
+ n = NMAX / 16; /* NMAX is divisible by 16 */
+ do {
+ DO16(buf); /* 16 sums unrolled */
+ buf += 16;
+ } while (--n);
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* do remaining bytes (less than NMAX, still just one modulo) */
+ if (len) { /* avoid modulos if none remaining */
+ while (len >= 16) {
+ len -= 16;
+ DO16(buf);
+ buf += 16;
+ }
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* return recombined sums */
+ return adler | (sum2 << 16);
+}
+
+/* ========================================================================= */
+local uLong adler32_combine_(
+ uLong adler1,
+ uLong adler2,
+ z_off64_t len2)
+{
+ unsigned long sum1;
+ unsigned long sum2;
+ unsigned rem;
+
+ /* for negative len, return invalid adler32 as a clue for debugging */
+ if (len2 < 0)
+ return 0xffffffffUL;
+
+ /* the derivation of this formula is left as an exercise for the reader */
+ MOD63(len2); /* assumes len2 >= 0 */
+ rem = (unsigned)len2;
+ sum1 = adler1 & 0xffff;
+ sum2 = rem * sum1;
+ MOD(sum2);
+ sum1 += (adler2 & 0xffff) + BASE - 1;
+ sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum1 >= BASE) sum1 -= BASE;
+ if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
+ if (sum2 >= BASE) sum2 -= BASE;
+ return sum1 | (sum2 << 16);
+}
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(
+ uLong adler1,
+ uLong adler2,
+ z_off_t len2)
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
+
+uLong ZEXPORT adler32_combine64(
+ uLong adler1,
+ uLong adler2,
+ z_off64_t len2)
+{
+ return adler32_combine_(adler1, adler2, len2);
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_compress.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_compress.c
new file mode 100644
index 0000000000..0314fbc61a
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_compress.c
@@ -0,0 +1,80 @@
+/* compress.c -- compress a memory buffer
+ * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least 0.1% larger than sourceLen plus
+ 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+int ZEXPORT compress2 (
+ Bytef *dest,
+ uLongf *destLen,
+ const Bytef *source,
+ uLong sourceLen,
+ int level)
+{
+ z_stream stream;
+ int err;
+
+ stream.next_in = (z_const Bytef *)source;
+ stream.avail_in = (uInt)sourceLen;
+#ifdef MAXSEG_64K
+ /* Check for source > 64K on 16-bit machine: */
+ if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+#endif
+ stream.next_out = dest;
+ stream.avail_out = (uInt)*destLen;
+ if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+ stream.opaque = (voidpf)0;
+
+ err = deflateInit(&stream, level);
+ if (err != Z_OK) return err;
+
+ err = deflate(&stream, Z_FINISH);
+ if (err != Z_STREAM_END) {
+ deflateEnd(&stream);
+ return err == Z_OK ? Z_BUF_ERROR : err;
+ }
+ *destLen = stream.total_out;
+
+ err = deflateEnd(&stream);
+ return err;
+}
+
+/* ===========================================================================
+ */
+int ZEXPORT compress (
+ Bytef *dest,
+ uLongf *destLen,
+ const Bytef *source,
+ uLong sourceLen)
+{
+ return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
+}
+
+/* ===========================================================================
+ If the default memLevel or windowBits for deflateInit() is changed, then
+ this function needs to be updated.
+ */
+uLong ZEXPORT compressBound (
+ uLong sourceLen)
+{
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_crc32.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_crc32.c
new file mode 100644
index 0000000000..f0072c94bf
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_crc32.c
@@ -0,0 +1,425 @@
+/* crc32.c -- compute the CRC-32 of a data stream
+ * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ *
+ * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
+ * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
+ * tables for updating the shift register in one step with three exclusive-ors
+ * instead of four steps with four exclusive-ors. This results in about a
+ * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
+ */
+
+/* @(#) $Id$ */
+
+/*
+ Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
+ protection on the static variables used to control the first-use generation
+ of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
+ first call get_crc_table() to initialize the tables before allowing more than
+ one thread to use crc32().
+
+ DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
+ */
+
+#ifdef MAKECRCH
+# include <stdio.h>
+# ifndef DYNAMIC_CRC_TABLE
+# define DYNAMIC_CRC_TABLE
+# endif /* !DYNAMIC_CRC_TABLE */
+#endif /* MAKECRCH */
+
+#include "zutil.h" /* for STDC and FAR definitions */
+
+#define local static
+
+/* Definitions for doing the crc four data bytes at a time. */
+#if !defined(NOBYFOUR) && defined(Z_U4)
+# define BYFOUR
+#endif
+#ifdef BYFOUR
+ local unsigned long crc32_little OF((unsigned long,
+ const unsigned char FAR *, unsigned));
+ local unsigned long crc32_big OF((unsigned long,
+ const unsigned char FAR *, unsigned));
+# define TBLS 8
+#else
+# define TBLS 1
+#endif /* BYFOUR */
+
+/* Local functions for crc concatenation */
+local unsigned long gf2_matrix_times OF((unsigned long *mat,
+ unsigned long vec));
+local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
+local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
+
+
+#ifdef DYNAMIC_CRC_TABLE
+
+local volatile int crc_table_empty = 1;
+local z_crc_t FAR crc_table[TBLS][256];
+local void make_crc_table OF((void));
+#ifdef MAKECRCH
+ local void write_table OF((FILE *, const z_crc_t FAR *));
+#endif /* MAKECRCH */
+/*
+ Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
+ x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
+
+ Polynomials over GF(2) are represented in binary, one bit per coefficient,
+ with the lowest powers in the most significant bit. Then adding polynomials
+ is just exclusive-or, and multiplying a polynomial by x is a right shift by
+ one. If we call the above polynomial p, and represent a byte as the
+ polynomial q, also with the lowest power in the most significant bit (so the
+ byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
+ where a mod b means the remainder after dividing a by b.
+
+ This calculation is done using the shift-register method of multiplying and
+ taking the remainder. The register is initialized to zero, and for each
+ incoming bit, x^32 is added mod p to the register if the bit is a one (where
+ x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
+ x (which is shifting right by one and adding x^32 mod p if the bit shifted
+ out is a one). We start with the highest power (least significant bit) of
+ q and repeat for all eight bits of q.
+
+ The first table is simply the CRC of all possible eight bit values. This is
+ all the information needed to generate CRCs on data a byte at a time for all
+ combinations of CRC register values and incoming bytes. The remaining tables
+ allow for word-at-a-time CRC calculation for both big-endian and little-
+ endian machines, where a word is four bytes.
+*/
+local void make_crc_table()
+{
+ z_crc_t c;
+ int n, k;
+ z_crc_t poly; /* polynomial exclusive-or pattern */
+ /* terms of polynomial defining this crc (except x^32): */
+ static volatile int first = 1; /* flag to limit concurrent making */
+ static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+ /* See if another task is already doing this (not thread-safe, but better
+ than nothing -- significantly reduces duration of vulnerability in
+ case the advice about DYNAMIC_CRC_TABLE is ignored) */
+ if (first) {
+ first = 0;
+
+ /* make exclusive-or pattern from polynomial (0xedb88320UL) */
+ poly = 0;
+ for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
+ poly |= (z_crc_t)1 << (31 - p[n]);
+
+ /* generate a crc for every 8-bit value */
+ for (n = 0; n < 256; n++) {
+ c = (z_crc_t)n;
+ for (k = 0; k < 8; k++)
+ c = c & 1 ? poly ^ (c >> 1) : c >> 1;
+ crc_table[0][n] = c;
+ }
+
+#ifdef BYFOUR
+ /* generate crc for each value followed by one, two, and three zeros,
+ and then the byte reversal of those as well as the first table */
+ for (n = 0; n < 256; n++) {
+ c = crc_table[0][n];
+ crc_table[4][n] = ZSWAP32(c);
+ for (k = 1; k < 4; k++) {
+ c = crc_table[0][c & 0xff] ^ (c >> 8);
+ crc_table[k][n] = c;
+ crc_table[k + 4][n] = ZSWAP32(c);
+ }
+ }
+#endif /* BYFOUR */
+
+ crc_table_empty = 0;
+ }
+ else { /* not first */
+ /* wait for the other guy to finish (not efficient, but rare) */
+ while (crc_table_empty)
+ ;
+ }
+
+#ifdef MAKECRCH
+ /* write out CRC tables to crc32.h */
+ {
+ FILE *out;
+
+ out = fopen("crc32.h", "w");
+ if (out == NULL) return;
+ fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
+ fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
+ fprintf(out, "local const z_crc_t FAR ");
+ fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
+ write_table(out, crc_table[0]);
+# ifdef BYFOUR
+ fprintf(out, "#ifdef BYFOUR\n");
+ for (k = 1; k < 8; k++) {
+ fprintf(out, " },\n {\n");
+ write_table(out, crc_table[k]);
+ }
+ fprintf(out, "#endif\n");
+# endif /* BYFOUR */
+ fprintf(out, " }\n};\n");
+ fclose(out);
+ }
+#endif /* MAKECRCH */
+}
+
+#ifdef MAKECRCH
+local void write_table(
+ FILE *out,
+ const z_crc_t FAR *table)
+{
+ int n;
+
+ for (n = 0; n < 256; n++)
+ fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
+ (unsigned long)(table[n]),
+ n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
+}
+#endif /* MAKECRCH */
+
+#else /* !DYNAMIC_CRC_TABLE */
+/* ========================================================================
+ * Tables of CRC-32s of all single-byte values, made by make_crc_table().
+ */
+#include "crc32.h"
+#endif /* DYNAMIC_CRC_TABLE */
+
+/* =========================================================================
+ * This function can be used by asm versions of crc32()
+ */
+const z_crc_t FAR * ZEXPORT get_crc_table()
+{
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+ return (const z_crc_t FAR *)crc_table;
+}
+
+/* ========================================================================= */
+#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
+#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
+
+/* ========================================================================= */
+unsigned long ZEXPORT crc32(
+ unsigned long crc,
+ const unsigned char FAR *buf,
+ uInt len)
+{
+ if (buf == Z_NULL) return 0UL;
+
+#ifdef DYNAMIC_CRC_TABLE
+ if (crc_table_empty)
+ make_crc_table();
+#endif /* DYNAMIC_CRC_TABLE */
+
+#ifdef BYFOUR
+ if (sizeof(void *) == sizeof(ptrdiff_t)) {
+ z_crc_t endian;
+
+ endian = 1;
+ if (*((unsigned char *)(&endian)))
+ return crc32_little(crc, buf, len);
+ else
+ return crc32_big(crc, buf, len);
+ }
+#endif /* BYFOUR */
+ crc = crc ^ 0xffffffffUL;
+ while (len >= 8) {
+ DO8;
+ len -= 8;
+ }
+ if (len) do {
+ DO1;
+ } while (--len);
+ return crc ^ 0xffffffffUL;
+}
+
+#ifdef BYFOUR
+
+/* ========================================================================= */
+#define DOLIT4 c ^= *buf4++; \
+ c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
+ crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
+#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
+
+/* ========================================================================= */
+local unsigned long crc32_little(
+ unsigned long crc,
+ const unsigned char FAR *buf,
+ unsigned len)
+{
+ register z_crc_t c;
+ register const z_crc_t FAR *buf4;
+
+ c = (z_crc_t)crc;
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ len--;
+ }
+
+ buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
+ while (len >= 32) {
+ DOLIT32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOLIT4;
+ len -= 4;
+ }
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)c;
+}
+
+/* ========================================================================= */
+#define DOBIG4 c ^= *++buf4; \
+ c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
+ crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
+#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
+
+/* ========================================================================= */
+local unsigned long crc32_big(
+ unsigned long crc,
+ const unsigned char FAR *buf,
+ unsigned len)
+{
+ register z_crc_t c;
+ register const z_crc_t FAR *buf4;
+
+ c = ZSWAP32((z_crc_t)crc);
+ c = ~c;
+ while (len && ((ptrdiff_t)buf & 3)) {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ len--;
+ }
+
+ buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
+ buf4--;
+ while (len >= 32) {
+ DOBIG32;
+ len -= 32;
+ }
+ while (len >= 4) {
+ DOBIG4;
+ len -= 4;
+ }
+ buf4++;
+ buf = (const unsigned char FAR *)buf4;
+
+ if (len) do {
+ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
+ } while (--len);
+ c = ~c;
+ return (unsigned long)(ZSWAP32(c));
+}
+
+#endif /* BYFOUR */
+
+#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
+
+/* ========================================================================= */
+local unsigned long gf2_matrix_times(
+ unsigned long *mat,
+ unsigned long vec)
+{
+ unsigned long sum;
+
+ sum = 0;
+ while (vec) {
+ if (vec & 1)
+ sum ^= *mat;
+ vec >>= 1;
+ mat++;
+ }
+ return sum;
+}
+
+/* ========================================================================= */
+local void gf2_matrix_square(
+ unsigned long *square,
+ unsigned long *mat)
+{
+ int n;
+
+ for (n = 0; n < GF2_DIM; n++)
+ square[n] = gf2_matrix_times(mat, mat[n]);
+}
+
+/* ========================================================================= */
+local uLong crc32_combine_(
+ uLong crc1,
+ uLong crc2,
+ z_off64_t len2)
+{
+ int n;
+ unsigned long row;
+ unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
+ unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
+
+ /* degenerate case (also disallow negative lengths) */
+ if (len2 <= 0)
+ return crc1;
+
+ /* put operator for one zero bit in odd */
+ odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
+ row = 1;
+ for (n = 1; n < GF2_DIM; n++) {
+ odd[n] = row;
+ row <<= 1;
+ }
+
+ /* put operator for two zero bits in even */
+ gf2_matrix_square(even, odd);
+
+ /* put operator for four zero bits in odd */
+ gf2_matrix_square(odd, even);
+
+ /* apply len2 zeros to crc1 (first square will put the operator for one
+ zero byte, eight zero bits, in even) */
+ do {
+ /* apply zeros operator for this bit of len2 */
+ gf2_matrix_square(even, odd);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(even, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ if (len2 == 0)
+ break;
+
+ /* another iteration of the loop with odd and even swapped */
+ gf2_matrix_square(odd, even);
+ if (len2 & 1)
+ crc1 = gf2_matrix_times(odd, crc1);
+ len2 >>= 1;
+
+ /* if no more bits set, then done */
+ } while (len2 != 0);
+
+ /* return combined crc */
+ crc1 ^= crc2;
+ return crc1;
+}
+
+/* ========================================================================= */
+uLong ZEXPORT crc32_combine(
+ uLong crc1,
+ uLong crc2,
+ z_off_t len2)
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
+
+uLong ZEXPORT crc32_combine64(
+ uLong crc1,
+ uLong crc2,
+ z_off64_t len2)
+{
+ return crc32_combine_(crc1, crc2, len2);
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_deflate.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_deflate.c
new file mode 100644
index 0000000000..1afe233c9a
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_deflate.c
@@ -0,0 +1,1966 @@
+/* deflate.c -- compress data using the deflation algorithm
+ * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process depends on being able to identify portions
+ * of the input text which are identical to earlier input (within a
+ * sliding window trailing behind the input currently being processed).
+ *
+ * The most straightforward technique turns out to be the fastest for
+ * most input files: try all possible matches and select the longest.
+ * The key feature of this algorithm is that insertions into the string
+ * dictionary are very simple and thus fast, and deletions are avoided
+ * completely. Insertions are performed at each input character, whereas
+ * string matches are performed only when the previous match ends. So it
+ * is preferable to spend more time in matches to allow very fast string
+ * insertions and avoid deletions. The matching algorithm for small
+ * strings is inspired from that of Rabin & Karp. A brute force approach
+ * is used to find longer strings when a small match has been found.
+ * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
+ * (by Leonid Broukhis).
+ * A previous version of this file used a more sophisticated algorithm
+ * (by Fiala and Greene) which is guaranteed to run in linear amortized
+ * time, but has a larger average cost, uses more memory and is patented.
+ * However the F&G algorithm may be faster for some highly redundant
+ * files if the parameter max_chain_length (described below) is too large.
+ *
+ * ACKNOWLEDGEMENTS
+ *
+ * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
+ * I found it in 'freeze' written by Leonid Broukhis.
+ * Thanks to many people for bug reports and testing.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
+ * Available in http://tools.ietf.org/html/rfc1951
+ *
+ * A description of the Rabin and Karp algorithm is given in the book
+ * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
+ *
+ * Fiala,E.R., and Greene,D.H.
+ * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
+ *
+ */
+
+/* @(#) $Id$ */
+
+#include "deflate.h"
+
+const char deflate_copyright[] =
+ " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/* ===========================================================================
+ * Function prototypes.
+ */
+typedef enum {
+ need_more, /* block not completed, need more input or more output */
+ block_done, /* block flush performed */
+ finish_started, /* finish started, need only more output at next deflate */
+ finish_done /* finish done, accept no more input or output */
+} block_state;
+
+typedef block_state (*compress_func) OF((deflate_state *s, int flush));
+/* Compression function. Returns the block state after the call. */
+
+local void fill_window OF((deflate_state *s));
+local block_state deflate_stored OF((deflate_state *s, int flush));
+local block_state deflate_fast OF((deflate_state *s, int flush));
+#ifndef FASTEST
+local block_state deflate_slow OF((deflate_state *s, int flush));
+#endif
+local block_state deflate_rle OF((deflate_state *s, int flush));
+local block_state deflate_huff OF((deflate_state *s, int flush));
+local void lm_init OF((deflate_state *s));
+local void putShortMSB OF((deflate_state *s, uInt b));
+local void flush_pending OF((z_streamp strm));
+local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
+#ifdef ASMV
+ void match_init OF((void)); /* asm code initialization */
+ uInt longest_match OF((deflate_state *s, IPos cur_match));
+#else
+local uInt longest_match OF((deflate_state *s, IPos cur_match));
+#endif
+
+#ifdef DEBUG
+local void check_match OF((deflate_state *s, IPos start, IPos match,
+ int length));
+#endif
+
+/* ===========================================================================
+ * Local data
+ */
+
+#define NIL 0
+/* Tail of hash chains */
+
+#ifndef TOO_FAR
+# define TOO_FAR 4096
+#endif
+/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
+
+/* Values for max_lazy_match, good_match and max_chain_length, depending on
+ * the desired pack level (0..9). The values given below have been tuned to
+ * exclude worst case performance for pathological files. Better values may be
+ * found for specific files.
+ */
+typedef struct config_s {
+ ush good_length; /* reduce lazy search above this match length */
+ ush max_lazy; /* do not perform lazy search above this match length */
+ ush nice_length; /* quit search above this match length */
+ ush max_chain;
+ compress_func func;
+} config;
+
+#ifdef FASTEST
+local const config configuration_table[2] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
+#else
+local const config configuration_table[10] = {
+/* good lazy nice chain */
+/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
+/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
+/* 2 */ {4, 5, 16, 8, deflate_fast},
+/* 3 */ {4, 6, 32, 32, deflate_fast},
+
+/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
+/* 5 */ {8, 16, 32, 32, deflate_slow},
+/* 6 */ {8, 16, 128, 128, deflate_slow},
+/* 7 */ {8, 32, 128, 256, deflate_slow},
+/* 8 */ {32, 128, 258, 1024, deflate_slow},
+/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
+#endif
+
+/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
+ * meaning.
+ */
+
+#define EQUAL 0
+/* result of memcmp for equal strings */
+
+#ifndef NO_DUMMY_DECL
+struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
+#endif
+
+/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
+#define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
+
+/* ===========================================================================
+ * Update a hash value with the given input byte
+ * IN assertion: all calls to to UPDATE_HASH are made with consecutive
+ * input characters, so that a running hash key can be computed from the
+ * previous key instead of complete recalculation each time.
+ */
+#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
+
+
+/* ===========================================================================
+ * Insert string str in the dictionary and set match_head to the previous head
+ * of the hash chain (the most recent string with same hash key). Return
+ * the previous length of the hash chain.
+ * If this file is compiled with -DFASTEST, the compression level is forced
+ * to 1, and no hash chains are maintained.
+ * IN assertion: all calls to to INSERT_STRING are made with consecutive
+ * input characters and the first MIN_MATCH bytes of str are valid
+ * (except for the last MIN_MATCH-1 bytes of the input file).
+ */
+#ifdef FASTEST
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#else
+#define INSERT_STRING(s, str, match_head) \
+ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+ match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
+ s->head[s->ins_h] = (Pos)(str))
+#endif
+
+/* ===========================================================================
+ * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
+ * prev[] will be initialized on the fly.
+ */
+#define CLEAR_HASH(s) \
+ s->head[s->hash_size-1] = NIL; \
+ zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
+
+/* ========================================================================= */
+int ZEXPORT deflateInit_(
+ z_streamp strm,
+ int level,
+ const char *version,
+ int stream_size)
+{
+ return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
+ Z_DEFAULT_STRATEGY, version, stream_size);
+ /* To do: ignore strm->next_in if we use it as window */
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateInit2_(
+ z_streamp strm,
+ int level,
+ int method,
+ int windowBits,
+ int memLevel,
+ int strategy,
+ const char *version,
+ int stream_size)
+{
+ deflate_state *s;
+ int wrap = 1;
+ static const char my_version[] = ZLIB_VERSION;
+
+ ushf *overlay;
+ /* We overlay pending_buf and d_buf+l_buf. This works since the average
+ * output size for (length,distance) codes is <= 24 bits.
+ */
+
+ if (version == Z_NULL || version[0] != my_version[0] ||
+ stream_size != sizeof(z_stream)) {
+ return Z_VERSION_ERROR;
+ }
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+
+ strm->msg = Z_NULL;
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+
+ if (windowBits < 0) { /* suppress zlib wrapper */
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+#ifdef GZIP
+ else if (windowBits > 15) {
+ wrap = 2; /* write gzip wrapper instead */
+ windowBits -= 16;
+ }
+#endif
+ if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
+ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
+ strategy < 0 || strategy > Z_FIXED) {
+ return Z_STREAM_ERROR;
+ }
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
+ s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
+ if (s == Z_NULL) return Z_MEM_ERROR;
+ strm->state = (struct internal_state FAR *)s;
+ s->strm = strm;
+
+ s->wrap = wrap;
+ s->gzhead = Z_NULL;
+ s->w_bits = windowBits;
+ s->w_size = 1 << s->w_bits;
+ s->w_mask = s->w_size - 1;
+
+ s->hash_bits = memLevel + 7;
+ s->hash_size = 1 << s->hash_bits;
+ s->hash_mask = s->hash_size - 1;
+ s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
+
+ s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
+ s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
+ s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
+
+ s->high_water = 0; /* nothing written to s->window yet */
+
+ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
+
+ overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
+ s->pending_buf = (uchf *) overlay;
+ s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
+
+ if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
+ s->pending_buf == Z_NULL) {
+ s->status = FINISH_STATE;
+ strm->msg = ERR_MSG(Z_MEM_ERROR);
+ deflateEnd (strm);
+ return Z_MEM_ERROR;
+ }
+ s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
+ s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
+
+ s->level = level;
+ s->strategy = strategy;
+ s->method = (Byte)method;
+
+ return deflateReset(strm);
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetDictionary (
+ z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength)
+{
+ deflate_state *s;
+ uInt str, n;
+ int wrap;
+ unsigned avail;
+ z_const unsigned char *next;
+
+ if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
+ return Z_STREAM_ERROR;
+ s = strm->state;
+ wrap = s->wrap;
+ if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
+ return Z_STREAM_ERROR;
+
+ /* when using zlib wrappers, compute Adler-32 for provided dictionary */
+ if (wrap == 1)
+ strm->adler = adler32(strm->adler, dictionary, dictLength);
+ s->wrap = 0; /* avoid computing Adler-32 in read_buf */
+
+ /* if dictionary would fill window, just replace the history */
+ if (dictLength >= s->w_size) {
+ if (wrap == 0) { /* already empty otherwise */
+ CLEAR_HASH(s);
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->insert = 0;
+ }
+ dictionary += dictLength - s->w_size; /* use the tail */
+ dictLength = s->w_size;
+ }
+
+ /* insert dictionary into window and hash */
+ avail = strm->avail_in;
+ next = strm->next_in;
+ strm->avail_in = dictLength;
+ strm->next_in = (z_const Bytef *)dictionary;
+ fill_window(s);
+ while (s->lookahead >= MIN_MATCH) {
+ str = s->strstart;
+ n = s->lookahead - (MIN_MATCH-1);
+ do {
+ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+ s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+ s->head[s->ins_h] = (Pos)str;
+ str++;
+ } while (--n);
+ s->strstart = str;
+ s->lookahead = MIN_MATCH-1;
+ fill_window(s);
+ }
+ s->strstart += s->lookahead;
+ s->block_start = (long)s->strstart;
+ s->insert = s->lookahead;
+ s->lookahead = 0;
+ s->match_length = s->prev_length = MIN_MATCH-1;
+ s->match_available = 0;
+ strm->next_in = next;
+ strm->avail_in = avail;
+ s->wrap = wrap;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateResetKeep (
+ z_streamp strm)
+{
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL ||
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
+ return Z_STREAM_ERROR;
+ }
+
+ strm->total_in = strm->total_out = 0;
+ strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
+ strm->data_type = Z_UNKNOWN;
+
+ s = (deflate_state *)strm->state;
+ s->pending = 0;
+ s->pending_out = s->pending_buf;
+
+ if (s->wrap < 0) {
+ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
+ }
+ s->status = s->wrap ? INIT_STATE : BUSY_STATE;
+ strm->adler =
+#ifdef GZIP
+ s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
+#endif
+ adler32(0L, Z_NULL, 0);
+ s->last_flush = Z_NO_FLUSH;
+
+ _tr_init(s);
+
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateReset (
+ z_streamp strm)
+{
+ int ret;
+
+ ret = deflateResetKeep(strm);
+ if (ret == Z_OK)
+ lm_init(strm->state);
+ return ret;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateSetHeader (strm, head)
+ z_streamp strm;
+ gz_headerp head;
+{
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ if (strm->state->wrap != 2) return Z_STREAM_ERROR;
+ strm->state->gzhead = head;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflatePending (
+ z_streamp strm,
+ unsigned *pending,
+ int *bits)
+{
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ if (pending != Z_NULL)
+ *pending = strm->state->pending;
+ if (bits != Z_NULL)
+ *bits = strm->state->bi_valid;
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflatePrime (strm, bits, value)
+ z_streamp strm;
+ int bits;
+ int value;
+{
+ deflate_state *s;
+ int put;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ s = strm->state;
+ if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
+ return Z_BUF_ERROR;
+ do {
+ put = Buf_size - s->bi_valid;
+ if (put > bits)
+ put = bits;
+ s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
+ s->bi_valid += put;
+ _tr_flush_bits(s);
+ value >>= put;
+ bits -= put;
+ } while (bits);
+ return Z_OK;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateParams(
+ z_streamp strm,
+ int level,
+ int strategy)
+{
+ deflate_state *s;
+ compress_func func;
+ int err = Z_OK;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ s = strm->state;
+
+#ifdef FASTEST
+ if (level != 0) level = 1;
+#else
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
+#endif
+ if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
+ return Z_STREAM_ERROR;
+ }
+ func = configuration_table[s->level].func;
+
+ if ((strategy != s->strategy || func != configuration_table[level].func) &&
+ strm->total_in != 0) {
+ /* Flush the last buffer: */
+ err = deflate(strm, Z_BLOCK);
+ if (err == Z_BUF_ERROR && s->pending == 0)
+ err = Z_OK;
+ }
+ if (s->level != level) {
+ s->level = level;
+ s->max_lazy_match = configuration_table[level].max_lazy;
+ s->good_match = configuration_table[level].good_length;
+ s->nice_match = configuration_table[level].nice_length;
+ s->max_chain_length = configuration_table[level].max_chain;
+ }
+ s->strategy = strategy;
+ return err;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateTune(
+ z_streamp strm,
+ int good_length,
+ int max_lazy,
+ int nice_length,
+ int max_chain)
+{
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ s = strm->state;
+ s->good_match = good_length;
+ s->max_lazy_match = max_lazy;
+ s->nice_match = nice_length;
+ s->max_chain_length = max_chain;
+ return Z_OK;
+}
+
+/* =========================================================================
+ * For the default windowBits of 15 and memLevel of 8, this function returns
+ * a close to exact, as well as small, upper bound on the compressed size.
+ * They are coded as constants here for a reason--if the #define's are
+ * changed, then this function needs to be changed as well. The return
+ * value for 15 and 8 only works for those exact settings.
+ *
+ * For any setting other than those defaults for windowBits and memLevel,
+ * the value returned is a conservative worst case for the maximum expansion
+ * resulting from using fixed blocks instead of stored blocks, which deflate
+ * can emit on compressed data for some combinations of the parameters.
+ *
+ * This function could be more sophisticated to provide closer upper bounds for
+ * every combination of windowBits and memLevel. But even the conservative
+ * upper bound of about 14% expansion does not seem onerous for output buffer
+ * allocation.
+ */
+uLong ZEXPORT deflateBound(
+ z_streamp strm,
+ uLong sourceLen)
+{
+ deflate_state *s;
+ uLong complen, wraplen;
+ Bytef *str;
+
+ /* conservative upper bound for compressed data */
+ complen = sourceLen +
+ ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
+
+ /* if can't get parameters, return conservative bound plus zlib wrapper */
+ if (strm == Z_NULL || strm->state == Z_NULL)
+ return complen + 6;
+
+ /* compute wrapper length */
+ s = strm->state;
+ switch (s->wrap) {
+ case 0: /* raw deflate */
+ wraplen = 0;
+ break;
+ case 1: /* zlib wrapper */
+ wraplen = 6 + (s->strstart ? 4 : 0);
+ break;
+ case 2: /* gzip wrapper */
+ wraplen = 18;
+ if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
+ if (s->gzhead->extra != Z_NULL)
+ wraplen += 2 + s->gzhead->extra_len;
+ str = s->gzhead->name;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ str = s->gzhead->comment;
+ if (str != Z_NULL)
+ do {
+ wraplen++;
+ } while (*str++);
+ if (s->gzhead->hcrc)
+ wraplen += 2;
+ }
+ break;
+ default: /* for compiler happiness */
+ wraplen = 6;
+ }
+
+ /* if not default parameters, return conservative bound */
+ if (s->w_bits != 15 || s->hash_bits != 8 + 7)
+ return complen + wraplen;
+
+ /* default settings: return tight bound for that case */
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+ (sourceLen >> 25) + 13 - 6 + wraplen;
+}
+
+/* =========================================================================
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
+ * IN assertion: the stream state is correct and there is enough room in
+ * pending_buf.
+ */
+local void putShortMSB (
+ deflate_state *s,
+ uInt b)
+{
+ put_byte(s, (Byte)(b >> 8));
+ put_byte(s, (Byte)(b & 0xff));
+}
+
+/* =========================================================================
+ * Flush as much pending output as possible. All deflate() output goes
+ * through this function so some applications may wish to modify it
+ * to avoid allocating a large strm->next_out buffer and copying into it.
+ * (See also read_buf()).
+ */
+local void flush_pending(
+ z_streamp strm)
+{
+ unsigned len;
+ deflate_state *s = strm->state;
+
+ _tr_flush_bits(s);
+ len = s->pending;
+ if (len > strm->avail_out) len = strm->avail_out;
+ if (len == 0) return;
+
+ zmemcpy(strm->next_out, s->pending_out, len);
+ strm->next_out += len;
+ s->pending_out += len;
+ strm->total_out += len;
+ strm->avail_out -= len;
+ s->pending -= len;
+ if (s->pending == 0) {
+ s->pending_out = s->pending_buf;
+ }
+}
+
+/* ========================================================================= */
+int ZEXPORT deflate (
+ z_streamp strm,
+ int flush)
+{
+ int old_flush; /* value of flush param for previous deflate call */
+ deflate_state *s;
+
+ if (strm == Z_NULL || strm->state == Z_NULL ||
+ flush > Z_BLOCK || flush < 0) {
+ return Z_STREAM_ERROR;
+ }
+ s = strm->state;
+
+ if (strm->next_out == Z_NULL ||
+ (strm->next_in == Z_NULL && strm->avail_in != 0) ||
+ (s->status == FINISH_STATE && flush != Z_FINISH)) {
+ ERR_RETURN(strm, Z_STREAM_ERROR);
+ }
+ if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
+
+ s->strm = strm; /* just in case */
+ old_flush = s->last_flush;
+ s->last_flush = flush;
+
+ /* Write the header */
+ if (s->status == INIT_STATE) {
+#ifdef GZIP
+ if (s->wrap == 2) {
+ strm->adler = crc32(0L, Z_NULL, 0);
+ put_byte(s, 31);
+ put_byte(s, 139);
+ put_byte(s, 8);
+ if (s->gzhead == Z_NULL) {
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, 0);
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, OS_CODE);
+ s->status = BUSY_STATE;
+ }
+ else {
+ put_byte(s, (s->gzhead->text ? 1 : 0) +
+ (s->gzhead->hcrc ? 2 : 0) +
+ (s->gzhead->extra == Z_NULL ? 0 : 4) +
+ (s->gzhead->name == Z_NULL ? 0 : 8) +
+ (s->gzhead->comment == Z_NULL ? 0 : 16)
+ );
+ put_byte(s, (Byte)(s->gzhead->time & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
+ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
+ put_byte(s, s->level == 9 ? 2 :
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
+ 4 : 0));
+ put_byte(s, s->gzhead->os & 0xff);
+ if (s->gzhead->extra != Z_NULL) {
+ put_byte(s, s->gzhead->extra_len & 0xff);
+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
+ }
+ if (s->gzhead->hcrc)
+ strm->adler = crc32(strm->adler, s->pending_buf,
+ s->pending);
+ s->gzindex = 0;
+ s->status = EXTRA_STATE;
+ }
+ }
+ else
+#endif
+ {
+ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
+ uInt level_flags;
+
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
+ level_flags = 0;
+ else if (s->level < 6)
+ level_flags = 1;
+ else if (s->level == 6)
+ level_flags = 2;
+ else
+ level_flags = 3;
+ header |= (level_flags << 6);
+ if (s->strstart != 0) header |= PRESET_DICT;
+ header += 31 - (header % 31);
+
+ s->status = BUSY_STATE;
+ putShortMSB(s, header);
+
+ /* Save the adler32 of the preset dictionary: */
+ if (s->strstart != 0) {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ strm->adler = adler32(0L, Z_NULL, 0);
+ }
+ }
+#ifdef GZIP
+ if (s->status == EXTRA_STATE) {
+ if (s->gzhead->extra != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+
+ while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size)
+ break;
+ }
+ put_byte(s, s->gzhead->extra[s->gzindex]);
+ s->gzindex++;
+ }
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (s->gzindex == s->gzhead->extra_len) {
+ s->gzindex = 0;
+ s->status = NAME_STATE;
+ }
+ }
+ else
+ s->status = NAME_STATE;
+ }
+ if (s->status == NAME_STATE) {
+ if (s->gzhead->name != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+ int val;
+
+ do {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size) {
+ val = 1;
+ break;
+ }
+ }
+ val = s->gzhead->name[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (val == 0) {
+ s->gzindex = 0;
+ s->status = COMMENT_STATE;
+ }
+ }
+ else
+ s->status = COMMENT_STATE;
+ }
+ if (s->status == COMMENT_STATE) {
+ if (s->gzhead->comment != Z_NULL) {
+ uInt beg = s->pending; /* start of bytes to update crc */
+ int val;
+
+ do {
+ if (s->pending == s->pending_buf_size) {
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ flush_pending(strm);
+ beg = s->pending;
+ if (s->pending == s->pending_buf_size) {
+ val = 1;
+ break;
+ }
+ }
+ val = s->gzhead->comment[s->gzindex++];
+ put_byte(s, val);
+ } while (val != 0);
+ if (s->gzhead->hcrc && s->pending > beg)
+ strm->adler = crc32(strm->adler, s->pending_buf + beg,
+ s->pending - beg);
+ if (val == 0)
+ s->status = HCRC_STATE;
+ }
+ else
+ s->status = HCRC_STATE;
+ }
+ if (s->status == HCRC_STATE) {
+ if (s->gzhead->hcrc) {
+ if (s->pending + 2 > s->pending_buf_size)
+ flush_pending(strm);
+ if (s->pending + 2 <= s->pending_buf_size) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ strm->adler = crc32(0L, Z_NULL, 0);
+ s->status = BUSY_STATE;
+ }
+ }
+ else
+ s->status = BUSY_STATE;
+ }
+#endif
+
+ /* Flush as much pending output as possible */
+ if (s->pending != 0) {
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ /* Since avail_out is 0, deflate will be called again with
+ * more output space, but possibly with both pending and
+ * avail_in equal to zero. There won't be anything to do,
+ * but this is not an error situation so make sure we
+ * return OK instead of BUF_ERROR at next call of deflate:
+ */
+ s->last_flush = -1;
+ return Z_OK;
+ }
+
+ /* Make sure there is something to do and avoid duplicate consecutive
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
+ */
+ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
+ flush != Z_FINISH) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* User must not provide more input after the first FINISH: */
+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
+ ERR_RETURN(strm, Z_BUF_ERROR);
+ }
+
+ /* Start a new block or continue the current one.
+ */
+ if (strm->avail_in != 0 || s->lookahead != 0 ||
+ (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
+ block_state bstate;
+
+ bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
+ (s->strategy == Z_RLE ? deflate_rle(s, flush) :
+ (*(configuration_table[s->level].func))(s, flush));
+
+ if (bstate == finish_started || bstate == finish_done) {
+ s->status = FINISH_STATE;
+ }
+ if (bstate == need_more || bstate == finish_started) {
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
+ }
+ return Z_OK;
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
+ * of deflate should use the same flush parameter to make sure
+ * that the flush is complete. So we don't have to output an
+ * empty block here, this will be done at next call. This also
+ * ensures that for a very small output buffer, we emit at most
+ * one empty block.
+ */
+ }
+ if (bstate == block_done) {
+ if (flush == Z_PARTIAL_FLUSH) {
+ _tr_align(s);
+ } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
+ _tr_stored_block(s, (char*)0, 0L, 0);
+ /* For a full flush, this empty block will be recognized
+ * as a special marker by inflate_sync().
+ */
+ if (flush == Z_FULL_FLUSH) {
+ CLEAR_HASH(s); /* forget history */
+ if (s->lookahead == 0) {
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->insert = 0;
+ }
+ }
+ }
+ flush_pending(strm);
+ if (strm->avail_out == 0) {
+ s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
+ return Z_OK;
+ }
+ }
+ }
+ Assert(strm->avail_out > 0, "bug2");
+
+ if (flush != Z_FINISH) return Z_OK;
+ if (s->wrap <= 0) return Z_STREAM_END;
+
+ /* Write the trailer */
+#ifdef GZIP
+ if (s->wrap == 2) {
+ put_byte(s, (Byte)(strm->adler & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
+ put_byte(s, (Byte)(strm->total_in & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
+ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
+ }
+ else
+#endif
+ {
+ putShortMSB(s, (uInt)(strm->adler >> 16));
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
+ }
+ flush_pending(strm);
+ /* If avail_out is zero, the application will call deflate again
+ * to flush the rest.
+ */
+ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
+ return s->pending != 0 ? Z_OK : Z_STREAM_END;
+}
+
+/* ========================================================================= */
+int ZEXPORT deflateEnd (
+ z_streamp strm)
+{
+ int status;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+
+ status = strm->state->status;
+ if (status != INIT_STATE &&
+ status != EXTRA_STATE &&
+ status != NAME_STATE &&
+ status != COMMENT_STATE &&
+ status != HCRC_STATE &&
+ status != BUSY_STATE &&
+ status != FINISH_STATE) {
+ return Z_STREAM_ERROR;
+ }
+
+ /* Deallocate in reverse order of allocations: */
+ TRY_FREE(strm, strm->state->pending_buf);
+ TRY_FREE(strm, strm->state->head);
+ TRY_FREE(strm, strm->state->prev);
+ TRY_FREE(strm, strm->state->window);
+
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+
+ return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
+}
+
+/* =========================================================================
+ * Copy the source state to the destination state.
+ * To simplify the source, this is not supported for 16-bit MSDOS (which
+ * doesn't have enough memory anyway to duplicate compression states).
+ */
+int ZEXPORT deflateCopy (
+ z_streamp dest,
+ z_streamp source)
+{
+#ifdef MAXSEG_64K
+ return Z_STREAM_ERROR;
+#else
+ deflate_state *ds;
+ deflate_state *ss;
+ ushf *overlay;
+
+
+ if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
+ return Z_STREAM_ERROR;
+ }
+
+ ss = source->state;
+
+ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+
+ ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
+ if (ds == Z_NULL) return Z_MEM_ERROR;
+ dest->state = (struct internal_state FAR *) ds;
+ zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
+ ds->strm = dest;
+
+ ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
+ ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
+ ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
+ overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
+ ds->pending_buf = (uchf *) overlay;
+
+ if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
+ ds->pending_buf == Z_NULL) {
+ deflateEnd (dest);
+ return Z_MEM_ERROR;
+ }
+ /* following zmemcpy do not work for 16-bit MSDOS */
+ zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
+ zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
+ zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
+ zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
+
+ ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
+ ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
+ ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
+
+ ds->l_desc.dyn_tree = ds->dyn_ltree;
+ ds->d_desc.dyn_tree = ds->dyn_dtree;
+ ds->bl_desc.dyn_tree = ds->bl_tree;
+
+ return Z_OK;
+#endif /* MAXSEG_64K */
+}
+
+/* ===========================================================================
+ * Read a new buffer from the current input stream, update the adler32
+ * and total number of bytes read. All deflate() input goes through
+ * this function so some applications may wish to modify it to avoid
+ * allocating a large strm->next_in buffer and copying from it.
+ * (See also flush_pending()).
+ */
+local int read_buf(
+ z_streamp strm,
+ Bytef *buf,
+ unsigned size)
+{
+ unsigned len = strm->avail_in;
+
+ if (len > size) len = size;
+ if (len == 0) return 0;
+
+ strm->avail_in -= len;
+
+ zmemcpy(buf, strm->next_in, len);
+ if (strm->state->wrap == 1) {
+ strm->adler = adler32(strm->adler, buf, len);
+ }
+#ifdef GZIP
+ else if (strm->state->wrap == 2) {
+ strm->adler = crc32(strm->adler, buf, len);
+ }
+#endif
+ strm->next_in += len;
+ strm->total_in += len;
+
+ return (int)len;
+}
+
+/* ===========================================================================
+ * Initialize the "longest match" routines for a new zlib stream
+ */
+local void lm_init (
+ deflate_state *s)
+{
+ s->window_size = (ulg)2L*s->w_size;
+
+ CLEAR_HASH(s);
+
+ /* Set the default configuration parameters:
+ */
+ s->max_lazy_match = configuration_table[s->level].max_lazy;
+ s->good_match = configuration_table[s->level].good_length;
+ s->nice_match = configuration_table[s->level].nice_length;
+ s->max_chain_length = configuration_table[s->level].max_chain;
+
+ s->strstart = 0;
+ s->block_start = 0L;
+ s->lookahead = 0;
+ s->insert = 0;
+ s->match_length = s->prev_length = MIN_MATCH-1;
+ s->match_available = 0;
+ s->ins_h = 0;
+#ifndef FASTEST
+#ifdef ASMV
+ match_init(); /* initialize the asm code */
+#endif
+#endif
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Set match_start to the longest match starting at the given string and
+ * return its length. Matches shorter or equal to prev_length are discarded,
+ * in which case the result is equal to prev_length and match_start is
+ * garbage.
+ * IN assertions: cur_match is the head of the hash chain for the current
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
+ * OUT assertion: the match length is not greater than s->lookahead.
+ */
+#ifndef ASMV
+/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
+ * match.S. The code will be functionally equivalent.
+ */
+local uInt longest_match(
+ deflate_state *s,
+ IPos cur_match) /* current match */
+{
+ unsigned chain_length = s->max_chain_length;/* max hash chain length */
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ int best_len = s->prev_length; /* best match length so far */
+ int nice_match = s->nice_match; /* stop if match long enough */
+ IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
+ s->strstart - (IPos)MAX_DIST(s) : NIL;
+ /* Stop when cur_match becomes <= limit. To simplify the code,
+ * we prevent matches with the string of window index 0.
+ */
+ Posf *prev = s->prev;
+ uInt wmask = s->w_mask;
+
+#ifdef UNALIGNED_OK
+ /* Compare two bytes at a time. Note: this is not always beneficial.
+ * Try with and without -DUNALIGNED_OK to check.
+ */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
+ register ush scan_start = *(ushf*)scan;
+ register ush scan_end = *(ushf*)(scan+best_len-1);
+#else
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+ register Byte scan_end1 = scan[best_len-1];
+ register Byte scan_end = scan[best_len];
+#endif
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ /* Do not waste too much time if we already have a good match: */
+ if (s->prev_length >= s->good_match) {
+ chain_length >>= 2;
+ }
+ /* Do not look for matches beyond the end of the input. This is necessary
+ * to make deflate deterministic.
+ */
+ if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ do {
+ Assert(cur_match < s->strstart, "no future");
+ match = s->window + cur_match;
+
+ /* Skip to next match if the match length cannot increase
+ * or if the match length is less than 2. Note that the checks below
+ * for insufficient lookahead only occur occasionally for performance
+ * reasons. Therefore uninitialized memory will be accessed, and
+ * conditional jumps will be made that depend on those values.
+ * However the length of the match is limited to the lookahead, so
+ * the output of deflate is not affected by the uninitialized values.
+ */
+#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
+ /* This code assumes sizeof(unsigned short) == 2. Do not use
+ * UNALIGNED_OK if your compiler uses a different size.
+ */
+ if (*(ushf*)(match+best_len-1) != scan_end ||
+ *(ushf*)match != scan_start) continue;
+
+ /* It is not necessary to compare scan[2] and match[2] since they are
+ * always equal when the other bytes match, given that the hash keys
+ * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
+ * strstart+3, +5, ... up to strstart+257. We check for insufficient
+ * lookahead only every 4th comparison; the 128th check will be made
+ * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
+ * necessary to put more guard bytes at the end of the window, or
+ * to check more often for insufficient lookahead.
+ */
+ Assert(scan[2] == match[2], "scan[2]?");
+ scan++, match++;
+ do {
+ } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+ scan < strend);
+ /* The funny "do {}" generates better code on most compilers */
+
+ /* Here, scan <= window+strstart+257 */
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+ if (*scan == *match) scan++;
+
+ len = (MAX_MATCH - 1) - (int)(strend-scan);
+ scan = strend - (MAX_MATCH-1);
+
+#else /* UNALIGNED_OK */
+
+ if (match[best_len] != scan_end ||
+ match[best_len-1] != scan_end1 ||
+ *match != *scan ||
+ *++match != scan[1]) continue;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match++;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+ scan = strend - MAX_MATCH;
+
+#endif /* UNALIGNED_OK */
+
+ if (len > best_len) {
+ s->match_start = cur_match;
+ best_len = len;
+ if (len >= nice_match) break;
+#ifdef UNALIGNED_OK
+ scan_end = *(ushf*)(scan+best_len-1);
+#else
+ scan_end1 = scan[best_len-1];
+ scan_end = scan[best_len];
+#endif
+ }
+ } while ((cur_match = prev[cur_match & wmask]) > limit
+ && --chain_length != 0);
+
+ if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
+ return s->lookahead;
+}
+#endif /* ASMV */
+
+#else /* FASTEST */
+
+/* ---------------------------------------------------------------------------
+ * Optimized version for FASTEST only
+ */
+local uInt longest_match(
+ deflate_state *s,
+ IPos cur_match) /* current match */
+{
+ register Bytef *scan = s->window + s->strstart; /* current string */
+ register Bytef *match; /* matched string */
+ register int len; /* length of current match */
+ register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+ Assert(cur_match < s->strstart, "no future");
+
+ match = s->window + cur_match;
+
+ /* Return failure if the match length is less than 2:
+ */
+ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
+
+ /* The check at best_len-1 can be removed because it will be made
+ * again later. (This heuristic is not always a win.)
+ * It is not necessary to compare scan[2] and match[2] since they
+ * are always equal when the other bytes match, given that
+ * the hash keys are equal and that HASH_BITS >= 8.
+ */
+ scan += 2, match += 2;
+ Assert(*scan == *match, "match[2]?");
+
+ /* We check for insufficient lookahead only every 8th comparison;
+ * the 256th check will be made at strstart+258.
+ */
+ do {
+ } while (*++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ *++scan == *++match && *++scan == *++match &&
+ scan < strend);
+
+ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+ len = MAX_MATCH - (int)(strend - scan);
+
+ if (len < MIN_MATCH) return MIN_MATCH - 1;
+
+ s->match_start = cur_match;
+ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
+}
+
+#endif /* FASTEST */
+
+#ifdef DEBUG
+/* ===========================================================================
+ * Check that the match at match_start is indeed a match.
+ */
+local void check_match(
+ deflate_state *s,
+ IPos start, IPos match,
+ int length)
+{
+ /* check that the match is indeed a match */
+ if (zmemcmp(s->window + match,
+ s->window + start, length) != EQUAL) {
+ fprintf(stderr, " start %u, match %u, length %d\n",
+ start, match, length);
+ do {
+ fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
+ } while (--length != 0);
+ z_error("invalid match");
+ }
+ if (z_verbose > 1) {
+ fprintf(stderr,"\\[%d,%d]", start-match, length);
+ do { putc(s->window[start++], stderr); } while (--length != 0);
+ }
+}
+#else
+# define check_match(s, start, match, length)
+#endif /* DEBUG */
+
+/* ===========================================================================
+ * Fill the window when the lookahead becomes insufficient.
+ * Updates strstart and lookahead.
+ *
+ * IN assertion: lookahead < MIN_LOOKAHEAD
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
+ * At least one byte has been read, or avail_in == 0; reads are
+ * performed for at least two bytes (required for the zip translate_eol
+ * option -- not supported here).
+ */
+local void fill_window(
+ deflate_state *s)
+{
+ register unsigned n, m;
+ register Posf *p;
+ unsigned more; /* Amount of free space at the end of the window. */
+ uInt wsize = s->w_size;
+
+ Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
+
+ do {
+ more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
+
+ /* Deal with !@#$% 64K limit: */
+ if (sizeof(int) <= 2) {
+ if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
+ more = wsize;
+
+ } else if (more == (unsigned)(-1)) {
+ /* Very unlikely, but possible on 16 bit machine if
+ * strstart == 0 && lookahead == 1 (input done a byte at time)
+ */
+ more--;
+ }
+ }
+
+ /* If the window is almost full and there is insufficient lookahead,
+ * move the upper half to the lower one to make room in the upper half.
+ */
+ if (s->strstart >= wsize+MAX_DIST(s)) {
+
+ zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
+ s->match_start -= wsize;
+ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
+ s->block_start -= (long) wsize;
+
+ /* Slide the hash table (could be avoided with 32 bit values
+ at the expense of memory usage). We slide even when level == 0
+ to keep the hash table consistent if we switch back to level > 0
+ later. (Using level 0 permanently is not an optimal usage of
+ zlib, so we don't care about this pathological case.)
+ */
+ n = s->hash_size;
+ p = &s->head[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m-wsize : NIL);
+ } while (--n);
+
+ n = wsize;
+#ifndef FASTEST
+ p = &s->prev[n];
+ do {
+ m = *--p;
+ *p = (Pos)(m >= wsize ? m-wsize : NIL);
+ /* If n is not on any hash chain, prev[n] is garbage but
+ * its value will never be used.
+ */
+ } while (--n);
+#endif
+ more += wsize;
+ }
+ if (s->strm->avail_in == 0) break;
+
+ /* If there was no sliding:
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
+ * more == window_size - lookahead - strstart
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
+ * => more >= window_size - 2*WSIZE + 2
+ * In the BIG_MEM or MMAP case (not yet supported),
+ * window_size == input_size + MIN_LOOKAHEAD &&
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
+ */
+ Assert(more >= 2, "more < 2");
+
+ n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
+ s->lookahead += n;
+
+ /* Initialize the hash value now that we have some input: */
+ if (s->lookahead + s->insert >= MIN_MATCH) {
+ uInt str = s->strstart - s->insert;
+ s->ins_h = s->window[str];
+ UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ while (s->insert) {
+ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
+#ifndef FASTEST
+ s->prev[str & s->w_mask] = s->head[s->ins_h];
+#endif
+ s->head[s->ins_h] = (Pos)str;
+ str++;
+ s->insert--;
+ if (s->lookahead + s->insert < MIN_MATCH)
+ break;
+ }
+ }
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
+ * but this is not important since only literal bytes will be emitted.
+ */
+
+ } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+
+ /* If the WIN_INIT bytes after the end of the current data have never been
+ * written, then zero those bytes in order to avoid memory check reports of
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
+ * the longest match routines. Update the high water mark for the next
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
+ */
+ if (s->high_water < s->window_size) {
+ ulg curr = s->strstart + (ulg)(s->lookahead);
+ ulg init;
+
+ if (s->high_water < curr) {
+ /* Previous high water mark below current data -- zero WIN_INIT
+ * bytes or up to end of window, whichever is less.
+ */
+ init = s->window_size - curr;
+ if (init > WIN_INIT)
+ init = WIN_INIT;
+ zmemzero(s->window + curr, (unsigned)init);
+ s->high_water = curr + init;
+ }
+ else if (s->high_water < (ulg)curr + WIN_INIT) {
+ /* High water mark at or above current data, but below current data
+ * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
+ * to end of window, whichever is less.
+ */
+ init = (ulg)curr + WIN_INIT - s->high_water;
+ if (init > s->window_size - s->high_water)
+ init = s->window_size - s->high_water;
+ zmemzero(s->window + s->high_water, (unsigned)init);
+ s->high_water += init;
+ }
+ }
+
+ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
+ "not enough room for search");
+}
+
+/* ===========================================================================
+ * Flush the current block, with given end-of-file flag.
+ * IN assertion: strstart is set to the end of the current match.
+ */
+#define FLUSH_BLOCK_ONLY(s, last) { \
+ _tr_flush_block(s, (s->block_start >= 0L ? \
+ (charf *)&s->window[(unsigned)s->block_start] : \
+ (charf *)Z_NULL), \
+ (ulg)((long)s->strstart - s->block_start), \
+ (last)); \
+ s->block_start = s->strstart; \
+ flush_pending(s->strm); \
+ Tracev((stderr,"[FLUSH]")); \
+}
+
+/* Same but force premature exit if necessary. */
+#define FLUSH_BLOCK(s, last) { \
+ FLUSH_BLOCK_ONLY(s, last); \
+ if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
+}
+
+/* ===========================================================================
+ * Copy without compression as much as possible from the input stream, return
+ * the current block state.
+ * This function does not insert new strings in the dictionary since
+ * uncompressible data is probably not useful. This function is used
+ * only for the level=0 compression option.
+ * NOTE: this function should be optimized to avoid extra copying from
+ * window to pending_buf.
+ */
+local block_state deflate_stored(
+ deflate_state *s,
+ int flush)
+{
+ /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
+ * to pending_buf_size, and each stored block has a 5 byte header:
+ */
+ ulg max_block_size = 0xffff;
+ ulg max_start;
+
+ if (max_block_size > s->pending_buf_size - 5) {
+ max_block_size = s->pending_buf_size - 5;
+ }
+
+ /* Copy as much as possible from input to output: */
+ for (;;) {
+ /* Fill the window as much as possible: */
+ if (s->lookahead <= 1) {
+
+ Assert(s->strstart < s->w_size+MAX_DIST(s) ||
+ s->block_start >= (long)s->w_size, "slide too late");
+
+ fill_window(s);
+ if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
+
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+ Assert(s->block_start >= 0L, "block gone");
+
+ s->strstart += s->lookahead;
+ s->lookahead = 0;
+
+ /* Emit a stored block if pending_buf will be full: */
+ max_start = s->block_start + max_block_size;
+ if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
+ /* strstart == 0 is possible when wraparound on 16-bit machine */
+ s->lookahead = (uInt)(s->strstart - max_start);
+ s->strstart = (uInt)max_start;
+ FLUSH_BLOCK(s, 0);
+ }
+ /* Flush if we may have to slide, otherwise block_start may become
+ * negative and the data will be gone:
+ */
+ if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
+ FLUSH_BLOCK(s, 0);
+ }
+ }
+ s->insert = 0;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if ((long)s->strstart > s->block_start)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+
+/* ===========================================================================
+ * Compress as much as possible from the input stream, return the current
+ * block state.
+ * This function does not perform lazy evaluation of matches and inserts
+ * new strings in the dictionary only for unmatched strings or for short
+ * matches. It is used only for the fast compression options.
+ */
+local block_state deflate_fast(
+ deflate_state *s,
+ int flush)
+{
+ IPos hash_head; /* head of the hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ * At this point we have always match_length < MIN_MATCH
+ */
+ if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+ }
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->match_start, s->match_length);
+
+ _tr_tally_dist(s, s->strstart - s->match_start,
+ s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+
+ /* Insert new strings in the hash table only if the match length
+ * is not too large. This saves time but degrades compression.
+ */
+#ifndef FASTEST
+ if (s->match_length <= s->max_insert_length &&
+ s->lookahead >= MIN_MATCH) {
+ s->match_length--; /* string at strstart already in table */
+ do {
+ s->strstart++;
+ INSERT_STRING(s, s->strstart, hash_head);
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+ * always MIN_MATCH bytes ahead.
+ */
+ } while (--s->match_length != 0);
+ s->strstart++;
+ } else
+#endif
+ {
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ s->ins_h = s->window[s->strstart];
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+ Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
+ * matter since it will be recomputed at next deflate call.
+ */
+ }
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+
+#ifndef FASTEST
+/* ===========================================================================
+ * Same as above, but achieves better compression. We use a lazy
+ * evaluation for matches: a match is finally adopted only if there is
+ * no better match at the next window position.
+ */
+local block_state deflate_slow(
+ deflate_state *s,
+ int flush)
+{
+ IPos hash_head; /* head of hash chain */
+ int bflush; /* set if current block must be flushed */
+
+ /* Process the input block. */
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the next match, plus MIN_MATCH bytes to insert the
+ * string following the next match.
+ */
+ if (s->lookahead < MIN_LOOKAHEAD) {
+ fill_window(s);
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* Insert the string window[strstart .. strstart+2] in the
+ * dictionary, and set hash_head to the head of the hash chain:
+ */
+ hash_head = NIL;
+ if (s->lookahead >= MIN_MATCH) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+
+ /* Find the longest match, discarding those <= prev_length.
+ */
+ s->prev_length = s->match_length, s->prev_match = s->match_start;
+ s->match_length = MIN_MATCH-1;
+
+ if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
+ s->strstart - hash_head <= MAX_DIST(s)) {
+ /* To simplify the code, we prevent matches with the string
+ * of window index 0 (in particular we have to avoid a match
+ * of the string with itself at the start of the input file).
+ */
+ s->match_length = longest_match (s, hash_head);
+ /* longest_match() sets match_start */
+
+ if (s->match_length <= 5 && (s->strategy == Z_FILTERED
+#if TOO_FAR <= 32767
+ || (s->match_length == MIN_MATCH &&
+ s->strstart - s->match_start > TOO_FAR)
+#endif
+ )) {
+
+ /* If prev_match is also MIN_MATCH, match_start is garbage
+ * but we will ignore the current match anyway.
+ */
+ s->match_length = MIN_MATCH-1;
+ }
+ }
+ /* If there was a match at the previous step and the current
+ * match is not better, output the previous match:
+ */
+ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
+ uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
+ /* Do not insert strings in hash table beyond this. */
+
+ check_match(s, s->strstart-1, s->prev_match, s->prev_length);
+
+ _tr_tally_dist(s, s->strstart -1 - s->prev_match,
+ s->prev_length - MIN_MATCH, bflush);
+
+ /* Insert in hash table all strings up to the end of the match.
+ * strstart-1 and strstart are already inserted. If there is not
+ * enough lookahead, the last two strings are not inserted in
+ * the hash table.
+ */
+ s->lookahead -= s->prev_length-1;
+ s->prev_length -= 2;
+ do {
+ if (++s->strstart <= max_insert) {
+ INSERT_STRING(s, s->strstart, hash_head);
+ }
+ } while (--s->prev_length != 0);
+ s->match_available = 0;
+ s->match_length = MIN_MATCH-1;
+ s->strstart++;
+
+ if (bflush) FLUSH_BLOCK(s, 0);
+
+ } else if (s->match_available) {
+ /* If there was no match at the previous position, output a
+ * single literal. If there was a match but the current match
+ * is longer, truncate the previous match to a single literal.
+ */
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ if (bflush) {
+ FLUSH_BLOCK_ONLY(s, 0);
+ }
+ s->strstart++;
+ s->lookahead--;
+ if (s->strm->avail_out == 0) return need_more;
+ } else {
+ /* There is no previous match to compare with, wait for
+ * the next step to decide.
+ */
+ s->match_available = 1;
+ s->strstart++;
+ s->lookahead--;
+ }
+ }
+ Assert (flush != Z_NO_FLUSH, "no flush?");
+ if (s->match_available) {
+ Tracevv((stderr,"%c", s->window[s->strstart-1]));
+ _tr_tally_lit(s, s->window[s->strstart-1], bflush);
+ s->match_available = 0;
+ }
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+#endif /* FASTEST */
+
+/* ===========================================================================
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
+ * deflate switches away from Z_RLE.)
+ */
+local block_state deflate_rle(
+ deflate_state *s,
+ int flush)
+{
+ int bflush; /* set if current block must be flushed */
+ uInt prev; /* byte at distance one to match */
+ Bytef *scan, *strend; /* scan goes up to strend for length of run */
+
+ for (;;) {
+ /* Make sure that we always have enough lookahead, except
+ * at the end of the input file. We need MAX_MATCH bytes
+ * for the longest run, plus one for the unrolled loop.
+ */
+ if (s->lookahead <= MAX_MATCH) {
+ fill_window(s);
+ if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
+ return need_more;
+ }
+ if (s->lookahead == 0) break; /* flush the current block */
+ }
+
+ /* See how many times the previous byte repeats */
+ s->match_length = 0;
+ if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
+ scan = s->window + s->strstart - 1;
+ prev = *scan;
+ if (prev == *++scan && prev == *++scan && prev == *++scan) {
+ strend = s->window + s->strstart + MAX_MATCH;
+ do {
+ } while (prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ prev == *++scan && prev == *++scan &&
+ scan < strend);
+ s->match_length = MAX_MATCH - (int)(strend - scan);
+ if (s->match_length > s->lookahead)
+ s->match_length = s->lookahead;
+ }
+ Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
+ }
+
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
+ if (s->match_length >= MIN_MATCH) {
+ check_match(s, s->strstart, s->strstart - 1, s->match_length);
+
+ _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
+
+ s->lookahead -= s->match_length;
+ s->strstart += s->match_length;
+ s->match_length = 0;
+ } else {
+ /* No match, output a literal byte */
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ }
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = 0;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
+
+/* ===========================================================================
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
+ */
+local block_state deflate_huff(
+ deflate_state *s,
+ int flush)
+{
+ int bflush; /* set if current block must be flushed */
+
+ for (;;) {
+ /* Make sure that we have a literal to write. */
+ if (s->lookahead == 0) {
+ fill_window(s);
+ if (s->lookahead == 0) {
+ if (flush == Z_NO_FLUSH)
+ return need_more;
+ break; /* flush the current block */
+ }
+ }
+
+ /* Output a literal byte */
+ s->match_length = 0;
+ Tracevv((stderr,"%c", s->window[s->strstart]));
+ _tr_tally_lit (s, s->window[s->strstart], bflush);
+ s->lookahead--;
+ s->strstart++;
+ if (bflush) FLUSH_BLOCK(s, 0);
+ }
+ s->insert = 0;
+ if (flush == Z_FINISH) {
+ FLUSH_BLOCK(s, 1);
+ return finish_done;
+ }
+ if (s->last_lit)
+ FLUSH_BLOCK(s, 0);
+ return block_done;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_infback.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_infback.c
new file mode 100644
index 0000000000..157bbd64e9
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_infback.c
@@ -0,0 +1,640 @@
+/* infback.c -- inflate using a call-back interface
+ * Copyright (C) 1995-2011 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ This code is largely copied from inflate.c. Normally either infback.o or
+ inflate.o would be linked into an application--not both. The interface
+ with inffast.c is retained so that optimized assembler-coded versions of
+ inflate_fast() can be used with either inflate.c or infback.c.
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+/* function prototypes */
+local void fixedtables OF((struct inflate_state FAR *state));
+
+/*
+ strm provides memory allocation functions in zalloc and zfree, or
+ Z_NULL to use the library memory allocation functions.
+
+ windowBits is in the range 8..15, and window is a user-supplied
+ window and output buffer that is 2**windowBits bytes.
+ */
+int ZEXPORT inflateBackInit_(
+z_streamp strm,
+int windowBits,
+unsigned char FAR *window,
+const char *version,
+int stream_size)
+{
+ struct inflate_state FAR *state;
+
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+ stream_size != (int)(sizeof(z_stream)))
+ return Z_VERSION_ERROR;
+ if (strm == Z_NULL || window == Z_NULL ||
+ windowBits < 8 || windowBits > 15)
+ return Z_STREAM_ERROR;
+ strm->msg = Z_NULL; /* in case we return an error */
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+ state = (struct inflate_state FAR *)ZALLOC(strm, 1,
+ sizeof(struct inflate_state));
+ if (state == Z_NULL) return Z_MEM_ERROR;
+ Tracev((stderr, "inflate: allocated\n"));
+ strm->state = (struct internal_state FAR *)state;
+ state->dmax = 32768U;
+ state->wbits = windowBits;
+ state->wsize = 1U << windowBits;
+ state->window = window;
+ state->wnext = 0;
+ state->whave = 0;
+ return Z_OK;
+}
+
+/*
+ Return state with length and distance decoding tables and index sizes set to
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
+ If BUILDFIXED is defined, then instead this routine builds the tables the
+ first time it's called, and returns those tables the first time and
+ thereafter. This reduces the size of the code by about 2K bytes, in
+ exchange for a little execution time. However, BUILDFIXED should not be
+ used for threaded applications, since the rewriting of the tables and virgin
+ may not be thread-safe.
+ */
+local void fixedtables(
+struct inflate_state FAR *state)
+{
+#ifdef BUILDFIXED
+ static int virgin = 1;
+ static code *lenfix, *distfix;
+ static code fixed[544];
+
+ /* build fixed huffman tables if first call (may not be thread safe) */
+ if (virgin) {
+ unsigned sym, bits;
+ static code *next;
+
+ /* literal/length table */
+ sym = 0;
+ while (sym < 144) state->lens[sym++] = 8;
+ while (sym < 256) state->lens[sym++] = 9;
+ while (sym < 280) state->lens[sym++] = 7;
+ while (sym < 288) state->lens[sym++] = 8;
+ next = fixed;
+ lenfix = next;
+ bits = 9;
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
+
+ /* distance table */
+ sym = 0;
+ while (sym < 32) state->lens[sym++] = 5;
+ distfix = next;
+ bits = 5;
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
+
+ /* do this just once */
+ virgin = 0;
+ }
+#else /* !BUILDFIXED */
+# include "inffixed.h"
+#endif /* BUILDFIXED */
+ state->lencode = lenfix;
+ state->lenbits = 9;
+ state->distcode = distfix;
+ state->distbits = 5;
+}
+
+/* Macros for inflateBack(): */
+
+/* Load returned state from inflate_fast() */
+#define LOAD() \
+ do { \
+ put = strm->next_out; \
+ left = strm->avail_out; \
+ next = strm->next_in; \
+ have = strm->avail_in; \
+ hold = state->hold; \
+ bits = state->bits; \
+ } while (0)
+
+/* Set state from registers for inflate_fast() */
+#define RESTORE() \
+ do { \
+ strm->next_out = put; \
+ strm->avail_out = left; \
+ strm->next_in = next; \
+ strm->avail_in = have; \
+ state->hold = hold; \
+ state->bits = bits; \
+ } while (0)
+
+/* Clear the input bit accumulator */
+#define INITBITS() \
+ do { \
+ hold = 0; \
+ bits = 0; \
+ } while (0)
+
+/* Assure that some input is available. If input is requested, but denied,
+ then return a Z_BUF_ERROR from inflateBack(). */
+#define PULL() \
+ do { \
+ if (have == 0) { \
+ have = in(in_desc, &next); \
+ if (have == 0) { \
+ next = Z_NULL; \
+ ret = Z_BUF_ERROR; \
+ goto inf_leave; \
+ } \
+ } \
+ } while (0)
+
+/* Get a byte of input into the bit accumulator, or return from inflateBack()
+ with an error if there is no input available. */
+#define PULLBYTE() \
+ do { \
+ PULL(); \
+ have--; \
+ hold += (unsigned long)(*next++) << bits; \
+ bits += 8; \
+ } while (0)
+
+/* Assure that there are at least n bits in the bit accumulator. If there is
+ not enough available input to do that, then return from inflateBack() with
+ an error. */
+#define NEEDBITS(n) \
+ do { \
+ while (bits < (unsigned)(n)) \
+ PULLBYTE(); \
+ } while (0)
+
+/* Return the low n bits of the bit accumulator (n < 16) */
+#define BITS(n) \
+ ((unsigned)hold & ((1U << (n)) - 1))
+
+/* Remove n bits from the bit accumulator */
+#define DROPBITS(n) \
+ do { \
+ hold >>= (n); \
+ bits -= (unsigned)(n); \
+ } while (0)
+
+/* Remove zero to seven bits as needed to go to a byte boundary */
+#define BYTEBITS() \
+ do { \
+ hold >>= bits & 7; \
+ bits -= bits & 7; \
+ } while (0)
+
+/* Assure that some output space is available, by writing out the window
+ if it's full. If the write fails, return from inflateBack() with a
+ Z_BUF_ERROR. */
+#define ROOM() \
+ do { \
+ if (left == 0) { \
+ put = state->window; \
+ left = state->wsize; \
+ state->whave = left; \
+ if (out(out_desc, put, left)) { \
+ ret = Z_BUF_ERROR; \
+ goto inf_leave; \
+ } \
+ } \
+ } while (0)
+
+/*
+ strm provides the memory allocation functions and window buffer on input,
+ and provides information on the unused input on return. For Z_DATA_ERROR
+ returns, strm will also provide an error message.
+
+ in() and out() are the call-back input and output functions. When
+ inflateBack() needs more input, it calls in(). When inflateBack() has
+ filled the window with output, or when it completes with data in the
+ window, it calls out() to write out the data. The application must not
+ change the provided input until in() is called again or inflateBack()
+ returns. The application must not change the window/output buffer until
+ inflateBack() returns.
+
+ in() and out() are called with a descriptor parameter provided in the
+ inflateBack() call. This parameter can be a structure that provides the
+ information required to do the read or write, as well as accumulated
+ information on the input and output such as totals and check values.
+
+ in() should return zero on failure. out() should return non-zero on
+ failure. If either in() or out() fails, than inflateBack() returns a
+ Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
+ was in() or out() that caused in the error. Otherwise, inflateBack()
+ returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
+ error, or Z_MEM_ERROR if it could not allocate memory for the state.
+ inflateBack() can also return Z_STREAM_ERROR if the input parameters
+ are not correct, i.e. strm is Z_NULL or the state was not initialized.
+ */
+int ZEXPORT inflateBack(
+z_streamp strm,
+in_func in,
+void FAR *in_desc,
+out_func out,
+void FAR *out_desc)
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *next; /* next input */
+ unsigned char FAR *put; /* next output */
+ unsigned have, left; /* available input and output */
+ unsigned long hold; /* bit buffer */
+ unsigned bits; /* bits in bit buffer */
+ unsigned copy; /* number of stored or match bytes to copy */
+ unsigned char FAR *from; /* where to copy match bytes from */
+ code here; /* current decoding table entry */
+ code last; /* parent table entry */
+ unsigned len; /* length to copy for repeats, bits to drop */
+ int ret; /* return code */
+ static const unsigned short order[19] = /* permutation of code lengths */
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ /* Check that the strm exists and that the state was initialized */
+ if (strm == Z_NULL || strm->state == Z_NULL)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* Reset the state */
+ strm->msg = Z_NULL;
+ state->mode = TYPE;
+ state->last = 0;
+ state->whave = 0;
+ next = strm->next_in;
+ have = next != Z_NULL ? strm->avail_in : 0;
+ hold = 0;
+ bits = 0;
+ put = state->window;
+ left = state->wsize;
+
+ /* Inflate until end of block marked as last */
+ for (;;)
+ switch (state->mode) {
+ case TYPE:
+ /* determine and dispatch block type */
+ if (state->last) {
+ BYTEBITS();
+ state->mode = DONE;
+ break;
+ }
+ NEEDBITS(3);
+ state->last = BITS(1);
+ DROPBITS(1);
+ switch (BITS(2)) {
+ case 0: /* stored block */
+ Tracev((stderr, "inflate: stored block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = STORED;
+ break;
+ case 1: /* fixed block */
+ fixedtables(state);
+ Tracev((stderr, "inflate: fixed codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = LEN; /* decode codes */
+ break;
+ case 2: /* dynamic block */
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = TABLE;
+ break;
+ case 3:
+ strm->msg = (char *)"invalid block type";
+ state->mode = BAD;
+ }
+ DROPBITS(2);
+ break;
+
+ case STORED:
+ /* get and verify stored block length */
+ BYTEBITS(); /* go to byte boundary */
+ NEEDBITS(32);
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
+ strm->msg = (char *)"invalid stored block lengths";
+ state->mode = BAD;
+ break;
+ }
+ state->length = (unsigned)hold & 0xffff;
+ Tracev((stderr, "inflate: stored length %u\n",
+ state->length));
+ INITBITS();
+
+ /* copy stored block from input to output */
+ while (state->length != 0) {
+ copy = state->length;
+ PULL();
+ ROOM();
+ if (copy > have) copy = have;
+ if (copy > left) copy = left;
+ zmemcpy(put, next, copy);
+ have -= copy;
+ next += copy;
+ left -= copy;
+ put += copy;
+ state->length -= copy;
+ }
+ Tracev((stderr, "inflate: stored end\n"));
+ state->mode = TYPE;
+ break;
+
+ case TABLE:
+ /* get dynamic table entries descriptor */
+ NEEDBITS(14);
+ state->nlen = BITS(5) + 257;
+ DROPBITS(5);
+ state->ndist = BITS(5) + 1;
+ DROPBITS(5);
+ state->ncode = BITS(4) + 4;
+ DROPBITS(4);
+#ifndef PKZIP_BUG_WORKAROUND
+ if (state->nlen > 286 || state->ndist > 30) {
+ strm->msg = (char *)"too many length or distance symbols";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracev((stderr, "inflate: table sizes ok\n"));
+
+ /* get code length code lengths (not a typo) */
+ state->have = 0;
+ while (state->have < state->ncode) {
+ NEEDBITS(3);
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
+ DROPBITS(3);
+ }
+ while (state->have < 19)
+ state->lens[order[state->have++]] = 0;
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 7;
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid code lengths set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: code lengths ok\n"));
+
+ /* get length and distance code code lengths */
+ state->have = 0;
+ while (state->have < state->nlen + state->ndist) {
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.val < 16) {
+ DROPBITS(here.bits);
+ state->lens[state->have++] = here.val;
+ }
+ else {
+ if (here.val == 16) {
+ NEEDBITS(here.bits + 2);
+ DROPBITS(here.bits);
+ if (state->have == 0) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ len = (unsigned)(state->lens[state->have - 1]);
+ copy = 3 + BITS(2);
+ DROPBITS(2);
+ }
+ else if (here.val == 17) {
+ NEEDBITS(here.bits + 3);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 3 + BITS(3);
+ DROPBITS(3);
+ }
+ else {
+ NEEDBITS(here.bits + 7);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 11 + BITS(7);
+ DROPBITS(7);
+ }
+ if (state->have + copy > state->nlen + state->ndist) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ while (copy--)
+ state->lens[state->have++] = (unsigned short)len;
+ }
+ }
+
+ /* handle error breaks in while */
+ if (state->mode == BAD) break;
+
+ /* check for end-of-block code (better have one) */
+ if (state->lens[256] == 0) {
+ strm->msg = (char *)"invalid code -- missing end-of-block";
+ state->mode = BAD;
+ break;
+ }
+
+ /* build code tables -- note: do not change the lenbits or distbits
+ values here (9 and 6) without reading the comments in inftrees.h
+ concerning the ENOUGH constants, which depend on those values */
+ state->next = state->codes;
+ state->lencode = (code const FAR *)(state->next);
+ state->lenbits = 9;
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid literal/lengths set";
+ state->mode = BAD;
+ break;
+ }
+ state->distcode = (code const FAR *)(state->next);
+ state->distbits = 6;
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
+ &(state->next), &(state->distbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid distances set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: codes ok\n"));
+ state->mode = LEN;
+
+ case LEN:
+ /* use inflate_fast() if we have enough input and output */
+ if (have >= 6 && left >= 258) {
+ RESTORE();
+ if (state->whave < state->wsize)
+ state->whave = state->wsize - left;
+ inflate_fast(strm, state->wsize);
+ LOAD();
+ break;
+ }
+
+ /* get a literal, length, or end-of-block code */
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.op && (here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->lencode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ }
+ DROPBITS(here.bits);
+ state->length = (unsigned)here.val;
+
+ /* process literal */
+ if (here.op == 0) {
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ ROOM();
+ *put++ = (unsigned char)(state->length);
+ left--;
+ state->mode = LEN;
+ break;
+ }
+
+ /* process end of block */
+ if (here.op & 32) {
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->mode = TYPE;
+ break;
+ }
+
+ /* invalid code */
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+
+ /* length code -- get extra bits, if any */
+ state->extra = (unsigned)(here.op) & 15;
+ if (state->extra != 0) {
+ NEEDBITS(state->extra);
+ state->length += BITS(state->extra);
+ DROPBITS(state->extra);
+ }
+ Tracevv((stderr, "inflate: length %u\n", state->length));
+
+ /* get distance code */
+ for (;;) {
+ here = state->distcode[BITS(state->distbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if ((here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->distcode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ }
+ DROPBITS(here.bits);
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ state->offset = (unsigned)here.val;
+
+ /* get distance extra bits, if any */
+ state->extra = (unsigned)(here.op) & 15;
+ if (state->extra != 0) {
+ NEEDBITS(state->extra);
+ state->offset += BITS(state->extra);
+ DROPBITS(state->extra);
+ }
+ if (state->offset > state->wsize - (state->whave < state->wsize ?
+ left : 0)) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
+
+ /* copy match from window to output */
+ do {
+ ROOM();
+ copy = state->wsize - state->offset;
+ if (copy < left) {
+ from = put + copy;
+ copy = left - copy;
+ }
+ else {
+ from = put - state->offset;
+ copy = left;
+ }
+ if (copy > state->length) copy = state->length;
+ state->length -= copy;
+ left -= copy;
+ do {
+ *put++ = *from++;
+ } while (--copy);
+ } while (state->length != 0);
+ break;
+
+ case DONE:
+ /* inflate stream terminated properly -- write leftover output */
+ ret = Z_STREAM_END;
+ if (left < state->wsize) {
+ if (out(out_desc, state->window, state->wsize - left))
+ ret = Z_BUF_ERROR;
+ }
+ goto inf_leave;
+
+ case BAD:
+ ret = Z_DATA_ERROR;
+ goto inf_leave;
+
+ default: /* can't happen, but makes compilers happy */
+ ret = Z_STREAM_ERROR;
+ goto inf_leave;
+ }
+
+ /* Return unused input */
+ inf_leave:
+ strm->next_in = next;
+ strm->avail_in = have;
+ return ret;
+}
+
+int ZEXPORT inflateBackEnd(
+z_streamp strm)
+{
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+ Tracev((stderr, "inflate: end\n"));
+ return Z_OK;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inffast.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inffast.c
new file mode 100644
index 0000000000..3fcffae498
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inffast.c
@@ -0,0 +1,340 @@
+/* inffast.c -- fast decoding
+ * Copyright (C) 1995-2008, 2010, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifndef ASMINF
+
+/* Allow machine dependent optimization for post-increment or pre-increment.
+ Based on testing to date,
+ Pre-increment preferred for:
+ - PowerPC G3 (Adler)
+ - MIPS R5000 (Randers-Pehrson)
+ Post-increment preferred for:
+ - none
+ No measurable difference:
+ - Pentium III (Anderson)
+ - M68060 (Nikl)
+ */
+#ifdef POSTINC
+# define OFF 0
+# define PUP(a) *(a)++
+#else
+# define OFF 1
+# define PUP(a) *++(a)
+#endif
+
+/*
+ Decode literal, length, and distance codes and write out the resulting
+ literal and match bytes until either not enough input or output is
+ available, an end-of-block is encountered, or a data error is encountered.
+ When large enough input and output buffers are supplied to inflate(), for
+ example, a 16K input buffer and a 64K output buffer, more than 95% of the
+ inflate execution time is spent in this routine.
+
+ Entry assumptions:
+
+ state->mode == LEN
+ strm->avail_in >= 6
+ strm->avail_out >= 258
+ start >= strm->avail_out
+ state->bits < 8
+
+ On return, state->mode is one of:
+
+ LEN -- ran out of enough output space or enough available input
+ TYPE -- reached end of block code, inflate() to interpret next block
+ BAD -- error in block data
+
+ Notes:
+
+ - The maximum input bits used by a length/distance pair is 15 bits for the
+ length code, 5 bits for the length extra, 15 bits for the distance code,
+ and 13 bits for the distance extra. This totals 48 bits, or six bytes.
+ Therefore if strm->avail_in >= 6, then there is enough input to avoid
+ checking for available input while decoding.
+
+ - The maximum bytes that a single length/distance pair can output is 258
+ bytes, which is the maximum length that can be coded. inflate_fast()
+ requires strm->avail_out >= 258 for each loop to avoid checking for
+ output space.
+ */
+void ZLIB_INTERNAL inflate_fast(
+z_streamp strm,
+unsigned start) /* inflate()'s starting value for strm->avail_out */
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *in; /* local strm->next_in */
+ z_const unsigned char FAR *last; /* have enough input while in < last */
+ unsigned char FAR *out; /* local strm->next_out */
+ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
+ unsigned char FAR *end; /* while out < end, enough space available */
+#ifdef INFLATE_STRICT
+ unsigned dmax; /* maximum distance from zlib header */
+#endif
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
+ unsigned long hold; /* local strm->hold */
+ unsigned bits; /* local strm->bits */
+ code const FAR *lcode; /* local strm->lencode */
+ code const FAR *dcode; /* local strm->distcode */
+ unsigned lmask; /* mask for first level of length codes */
+ unsigned dmask; /* mask for first level of distance codes */
+ code here; /* retrieved table entry */
+ unsigned op; /* code bits, operation, extra bits, or */
+ /* window position, window bytes to copy */
+ unsigned len; /* match length, unused bytes */
+ unsigned dist; /* match distance */
+ unsigned char FAR *from; /* where to copy match from */
+
+ /* copy state to local variables */
+ state = (struct inflate_state FAR *)strm->state;
+ in = strm->next_in - OFF;
+ last = in + (strm->avail_in - 5);
+ out = strm->next_out - OFF;
+ beg = out - (start - strm->avail_out);
+ end = out + (strm->avail_out - 257);
+#ifdef INFLATE_STRICT
+ dmax = state->dmax;
+#endif
+ wsize = state->wsize;
+ whave = state->whave;
+ wnext = state->wnext;
+ window = state->window;
+ hold = state->hold;
+ bits = state->bits;
+ lcode = state->lencode;
+ dcode = state->distcode;
+ lmask = (1U << state->lenbits) - 1;
+ dmask = (1U << state->distbits) - 1;
+
+ /* decode literals and length/distances until end-of-block or not enough
+ input data or output space */
+ do {
+ if (bits < 15) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ here = lcode[hold & lmask];
+ dolen:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op == 0) { /* literal */
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ PUP(out) = (unsigned char)(here.val);
+ }
+ else if (op & 16) { /* length base */
+ len = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (op) {
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ len += (unsigned)hold & ((1U << op) - 1);
+ hold >>= op;
+ bits -= op;
+ }
+ Tracevv((stderr, "inflate: length %u\n", len));
+ if (bits < 15) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ here = dcode[hold & dmask];
+ dodist:
+ op = (unsigned)(here.bits);
+ hold >>= op;
+ bits -= op;
+ op = (unsigned)(here.op);
+ if (op & 16) { /* distance base */
+ dist = (unsigned)(here.val);
+ op &= 15; /* number of extra bits */
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ if (bits < op) {
+ hold += (unsigned long)(PUP(in)) << bits;
+ bits += 8;
+ }
+ }
+ dist += (unsigned)hold & ((1U << op) - 1);
+#ifdef INFLATE_STRICT
+ if (dist > dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ hold >>= op;
+ bits -= op;
+ Tracevv((stderr, "inflate: distance %u\n", dist));
+ op = (unsigned)(out - beg); /* max distance in output */
+ if (dist > op) { /* see if copy from window */
+ op = dist - op; /* distance back in window */
+ if (op > whave) {
+ if (state->sane) {
+ strm->msg =
+ (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ if (len <= op - whave) {
+ do {
+ PUP(out) = 0;
+ } while (--len);
+ continue;
+ }
+ len -= op - whave;
+ do {
+ PUP(out) = 0;
+ } while (--op > whave);
+ if (op == 0) {
+ from = out - dist;
+ do {
+ PUP(out) = PUP(from);
+ } while (--len);
+ continue;
+ }
+#endif
+ }
+ from = window - OFF;
+ if (wnext == 0) { /* very common case */
+ from += wsize - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ else if (wnext < op) { /* wrap around window */
+ from += wsize + wnext - op;
+ op -= wnext;
+ if (op < len) { /* some from end of window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = window - OFF;
+ if (wnext < len) { /* some from start of window */
+ op = wnext;
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ }
+ else { /* contiguous in window */
+ from += wnext - op;
+ if (op < len) { /* some from window */
+ len -= op;
+ do {
+ PUP(out) = PUP(from);
+ } while (--op);
+ from = out - dist; /* rest from output */
+ }
+ }
+ while (len > 2) {
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ len -= 3;
+ }
+ if (len) {
+ PUP(out) = PUP(from);
+ if (len > 1)
+ PUP(out) = PUP(from);
+ }
+ }
+ else {
+ from = out - dist; /* copy direct from output */
+ do { /* minimum length is three */
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ PUP(out) = PUP(from);
+ len -= 3;
+ } while (len > 2);
+ if (len) {
+ PUP(out) = PUP(from);
+ if (len > 1)
+ PUP(out) = PUP(from);
+ }
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level distance code */
+ here = dcode[here.val + (hold & ((1U << op) - 1))];
+ goto dodist;
+ }
+ else {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ }
+ else if ((op & 64) == 0) { /* 2nd level length code */
+ here = lcode[here.val + (hold & ((1U << op) - 1))];
+ goto dolen;
+ }
+ else if (op & 32) { /* end-of-block */
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->mode = TYPE;
+ break;
+ }
+ else {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ } while (in < last && out < end);
+
+ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
+ len = bits >> 3;
+ in -= len;
+ bits -= len << 3;
+ hold &= (1U << bits) - 1;
+
+ /* update state and return */
+ strm->next_in = in + OFF;
+ strm->next_out = out + OFF;
+ strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
+ strm->avail_out = (unsigned)(out < end ?
+ 257 + (end - out) : 257 - (out - end));
+ state->hold = hold;
+ state->bits = bits;
+ return;
+}
+
+/*
+ inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
+ - Using bit fields for code structure
+ - Different op definition to avoid & for extra bits (do & for table bits)
+ - Three separate decoding do-loops for direct, window, and wnext == 0
+ - Special case for distance > 1 copies to do overlapped load and store copy
+ - Explicit branch predictions (based on measured branch probabilities)
+ - Deferring match copy and interspersed it with decoding subsequent codes
+ - Swapping literal/length else
+ - Swapping window/direct else
+ - Larger unrolled copy loops (three is about right)
+ - Moving len -= 3 statement into middle of loop
+ */
+
+#endif /* !ASMINF */
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inflate.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inflate.c
new file mode 100644
index 0000000000..bc5e448daf
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inflate.c
@@ -0,0 +1,1513 @@
+/* inflate.c -- zlib decompression
+ * Copyright (C) 1995-2012 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * Change history:
+ *
+ * 1.2.beta0 24 Nov 2002
+ * - First version -- complete rewrite of inflate to simplify code, avoid
+ * creation of window when not needed, minimize use of window when it is
+ * needed, make inffast.c even faster, implement gzip decoding, and to
+ * improve code readability and style over the previous zlib inflate code
+ *
+ * 1.2.beta1 25 Nov 2002
+ * - Use pointers for available input and output checking in inffast.c
+ * - Remove input and output counters in inffast.c
+ * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
+ * - Remove unnecessary second byte pull from length extra in inffast.c
+ * - Unroll direct copy to three copies per loop in inffast.c
+ *
+ * 1.2.beta2 4 Dec 2002
+ * - Change external routine names to reduce potential conflicts
+ * - Correct filename to inffixed.h for fixed tables in inflate.c
+ * - Make hbuf[] unsigned char to match parameter type in inflate.c
+ * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
+ * to avoid negation problem on Alphas (64 bit) in inflate.c
+ *
+ * 1.2.beta3 22 Dec 2002
+ * - Add comments on state->bits assertion in inffast.c
+ * - Add comments on op field in inftrees.h
+ * - Fix bug in reuse of allocated window after inflateReset()
+ * - Remove bit fields--back to byte structure for speed
+ * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
+ * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
+ * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
+ * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
+ * - Use local copies of stream next and avail values, as well as local bit
+ * buffer and bit count in inflate()--for speed when inflate_fast() not used
+ *
+ * 1.2.beta4 1 Jan 2003
+ * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
+ * - Move a comment on output buffer sizes from inffast.c to inflate.c
+ * - Add comments in inffast.c to introduce the inflate_fast() routine
+ * - Rearrange window copies in inflate_fast() for speed and simplification
+ * - Unroll last copy for window match in inflate_fast()
+ * - Use local copies of window variables in inflate_fast() for speed
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
+ * - Make op and len in inflate_fast() unsigned for consistency
+ * - Add FAR to lcode and dcode declarations in inflate_fast()
+ * - Simplified bad distance check in inflate_fast()
+ * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
+ * source file infback.c to provide a call-back interface to inflate for
+ * programs like gzip and unzip -- uses window as output buffer to avoid
+ * window copying
+ *
+ * 1.2.beta5 1 Jan 2003
+ * - Improved inflateBack() interface to allow the caller to provide initial
+ * input in strm.
+ * - Fixed stored blocks bug in inflateBack()
+ *
+ * 1.2.beta6 4 Jan 2003
+ * - Added comments in inffast.c on effectiveness of POSTINC
+ * - Typecasting all around to reduce compiler warnings
+ * - Changed loops from while (1) or do {} while (1) to for (;;), again to
+ * make compilers happy
+ * - Changed type of window in inflateBackInit() to unsigned char *
+ *
+ * 1.2.beta7 27 Jan 2003
+ * - Changed many types to unsigned or unsigned short to avoid warnings
+ * - Added inflateCopy() function
+ *
+ * 1.2.0 9 Mar 2003
+ * - Changed inflateBack() interface to provide separate opaque descriptors
+ * for the in() and out() functions
+ * - Changed inflateBack() argument and in_func typedef to swap the length
+ * and buffer address return values for the input function
+ * - Check next_in and next_out for Z_NULL on entry to inflate()
+ *
+ * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+#include "inflate.h"
+#include "inffast.h"
+
+#ifdef MAKEFIXED
+# ifndef BUILDFIXED
+# define BUILDFIXED
+# endif
+#endif
+
+/* function prototypes */
+local void fixedtables OF((struct inflate_state FAR *state));
+local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
+ unsigned copy));
+#ifdef BUILDFIXED
+ void makefixed OF((void));
+#endif
+local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
+ unsigned len));
+
+int ZEXPORT inflateResetKeep(
+z_streamp strm)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ strm->total_in = strm->total_out = state->total = 0;
+ strm->msg = Z_NULL;
+ if (state->wrap) /* to support ill-conceived Java test suite */
+ strm->adler = state->wrap & 1;
+ state->mode = HEAD;
+ state->last = 0;
+ state->havedict = 0;
+ state->dmax = 32768U;
+ state->head = Z_NULL;
+ state->hold = 0;
+ state->bits = 0;
+ state->lencode = state->distcode = state->next = state->codes;
+ state->sane = 1;
+ state->back = -1;
+ Tracev((stderr, "inflate: reset\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateReset(
+z_streamp strm)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ state->wsize = 0;
+ state->whave = 0;
+ state->wnext = 0;
+ return inflateResetKeep(strm);
+}
+
+int ZEXPORT inflateReset2(
+z_streamp strm,
+int windowBits)
+{
+ int wrap;
+ struct inflate_state FAR *state;
+
+ /* get the state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* extract wrap request from windowBits parameter */
+ if (windowBits < 0) {
+ wrap = 0;
+ windowBits = -windowBits;
+ }
+ else {
+ wrap = (windowBits >> 4) + 1;
+#ifdef GUNZIP
+ if (windowBits < 48)
+ windowBits &= 15;
+#endif
+ }
+
+ /* set number of window bits, free window if different */
+ if (windowBits && (windowBits < 8 || windowBits > 15))
+ return Z_STREAM_ERROR;
+ if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
+ ZFREE(strm, state->window);
+ state->window = Z_NULL;
+ }
+
+ /* update state and reset the rest of it */
+ state->wrap = wrap;
+ state->wbits = (unsigned)windowBits;
+ return inflateReset(strm);
+}
+
+int ZEXPORT inflateInit2_(
+z_streamp strm,
+int windowBits,
+const char *version,
+int stream_size)
+{
+ int ret;
+ struct inflate_state FAR *state;
+
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+ stream_size != (int)(sizeof(z_stream)))
+ return Z_VERSION_ERROR;
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
+ strm->msg = Z_NULL; /* in case we return an error */
+ if (strm->zalloc == (alloc_func)0) {
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zalloc = zcalloc;
+ strm->opaque = (voidpf)0;
+#endif
+ }
+ if (strm->zfree == (free_func)0)
+#ifdef Z_SOLO
+ return Z_STREAM_ERROR;
+#else
+ strm->zfree = zcfree;
+#endif
+ state = (struct inflate_state FAR *)
+ ZALLOC(strm, 1, sizeof(struct inflate_state));
+ if (state == Z_NULL) return Z_MEM_ERROR;
+ Tracev((stderr, "inflate: allocated\n"));
+ strm->state = (struct internal_state FAR *)state;
+ state->window = Z_NULL;
+ ret = inflateReset2(strm, windowBits);
+ if (ret != Z_OK) {
+ ZFREE(strm, state);
+ strm->state = Z_NULL;
+ }
+ return ret;
+}
+
+int ZEXPORT inflateInit_(
+z_streamp strm,
+const char *version,
+int stream_size)
+{
+ return inflateInit2_(strm, DEF_WBITS, version, stream_size);
+}
+
+int ZEXPORT inflatePrime(
+z_streamp strm,
+int bits,
+int value)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (bits < 0) {
+ state->hold = 0;
+ state->bits = 0;
+ return Z_OK;
+ }
+ if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
+ value &= (1L << bits) - 1;
+ state->hold += value << state->bits;
+ state->bits += bits;
+ return Z_OK;
+}
+
+/*
+ Return state with length and distance decoding tables and index sizes set to
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
+ If BUILDFIXED is defined, then instead this routine builds the tables the
+ first time it's called, and returns those tables the first time and
+ thereafter. This reduces the size of the code by about 2K bytes, in
+ exchange for a little execution time. However, BUILDFIXED should not be
+ used for threaded applications, since the rewriting of the tables and virgin
+ may not be thread-safe.
+ */
+local void fixedtables(
+struct inflate_state FAR *state)
+{
+#ifdef BUILDFIXED
+ static int virgin = 1;
+ static code *lenfix, *distfix;
+ static code fixed[544];
+
+ /* build fixed huffman tables if first call (may not be thread safe) */
+ if (virgin) {
+ unsigned sym, bits;
+ static code *next;
+
+ /* literal/length table */
+ sym = 0;
+ while (sym < 144) state->lens[sym++] = 8;
+ while (sym < 256) state->lens[sym++] = 9;
+ while (sym < 280) state->lens[sym++] = 7;
+ while (sym < 288) state->lens[sym++] = 8;
+ next = fixed;
+ lenfix = next;
+ bits = 9;
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
+
+ /* distance table */
+ sym = 0;
+ while (sym < 32) state->lens[sym++] = 5;
+ distfix = next;
+ bits = 5;
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
+
+ /* do this just once */
+ virgin = 0;
+ }
+#else /* !BUILDFIXED */
+# include "inffixed.h"
+#endif /* BUILDFIXED */
+ state->lencode = lenfix;
+ state->lenbits = 9;
+ state->distcode = distfix;
+ state->distbits = 5;
+}
+
+#ifdef MAKEFIXED
+#include <stdio.h>
+
+/*
+ Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
+ defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
+ those tables to stdout, which would be piped to inffixed.h. A small program
+ can simply call makefixed to do this:
+
+ void makefixed(void);
+
+ int main(void)
+ {
+ makefixed();
+ return 0;
+ }
+
+ Then that can be linked with zlib built with MAKEFIXED defined and run:
+
+ a.out > inffixed.h
+ */
+void makefixed()
+{
+ unsigned low, size;
+ struct inflate_state state;
+
+ fixedtables(&state);
+ puts(" /* inffixed.h -- table for decoding fixed codes");
+ puts(" * Generated automatically by makefixed().");
+ puts(" */");
+ puts("");
+ puts(" /* WARNING: this file should *not* be used by applications.");
+ puts(" It is part of the implementation of this library and is");
+ puts(" subject to change. Applications should only use zlib.h.");
+ puts(" */");
+ puts("");
+ size = 1U << 9;
+ printf(" static const code lenfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 7) == 0) printf("\n ");
+ printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
+ state.lencode[low].bits, state.lencode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+ size = 1U << 5;
+ printf("\n static const code distfix[%u] = {", size);
+ low = 0;
+ for (;;) {
+ if ((low % 6) == 0) printf("\n ");
+ printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
+ state.distcode[low].val);
+ if (++low == size) break;
+ putchar(',');
+ }
+ puts("\n };");
+}
+#endif /* MAKEFIXED */
+
+/*
+ Update the window with the last wsize (normally 32K) bytes written before
+ returning. If window does not exist yet, create it. This is only called
+ when a window is already in use, or when output has been written during this
+ inflate call, but the end of the deflate stream has not been reached yet.
+ It is also called to create a window for dictionary data when a dictionary
+ is loaded.
+
+ Providing output buffers larger than 32K to inflate() should provide a speed
+ advantage, since only the last 32K of output is copied to the sliding window
+ upon return from inflate(), and since all distances after the first 32K of
+ output will fall in the output data, making match copies simpler and faster.
+ The advantage may be dependent on the size of the processor's data caches.
+ */
+local int updatewindow(
+z_streamp strm,
+const Bytef *end,
+unsigned copy)
+{
+ struct inflate_state FAR *state;
+ unsigned dist;
+
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* if it hasn't been done already, allocate space for the window */
+ if (state->window == Z_NULL) {
+ state->window = (unsigned char FAR *)
+ ZALLOC(strm, 1U << state->wbits,
+ sizeof(unsigned char));
+ if (state->window == Z_NULL) return 1;
+ }
+
+ /* if window not in use yet, initialize */
+ if (state->wsize == 0) {
+ state->wsize = 1U << state->wbits;
+ state->wnext = 0;
+ state->whave = 0;
+ }
+
+ /* copy state->wsize or less output bytes into the circular window */
+ if (copy >= state->wsize) {
+ zmemcpy(state->window, end - state->wsize, state->wsize);
+ state->wnext = 0;
+ state->whave = state->wsize;
+ }
+ else {
+ dist = state->wsize - state->wnext;
+ if (dist > copy) dist = copy;
+ zmemcpy(state->window + state->wnext, end - copy, dist);
+ copy -= dist;
+ if (copy) {
+ zmemcpy(state->window, end - copy, copy);
+ state->wnext = copy;
+ state->whave = state->wsize;
+ }
+ else {
+ state->wnext += dist;
+ if (state->wnext == state->wsize) state->wnext = 0;
+ if (state->whave < state->wsize) state->whave += dist;
+ }
+ }
+ return 0;
+}
+
+/* Macros for inflate(): */
+
+/* check function to use adler32() for zlib or crc32() for gzip */
+#ifdef GUNZIP
+# define UPDATE(check, buf, len) \
+ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
+#else
+# define UPDATE(check, buf, len) adler32(check, buf, len)
+#endif
+
+/* check macros for header crc */
+#ifdef GUNZIP
+# define CRC2(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ check = crc32(check, hbuf, 2); \
+ } while (0)
+
+# define CRC4(check, word) \
+ do { \
+ hbuf[0] = (unsigned char)(word); \
+ hbuf[1] = (unsigned char)((word) >> 8); \
+ hbuf[2] = (unsigned char)((word) >> 16); \
+ hbuf[3] = (unsigned char)((word) >> 24); \
+ check = crc32(check, hbuf, 4); \
+ } while (0)
+#endif
+
+/* Load registers with state in inflate() for speed */
+#define LOAD() \
+ do { \
+ put = strm->next_out; \
+ left = strm->avail_out; \
+ next = strm->next_in; \
+ have = strm->avail_in; \
+ hold = state->hold; \
+ bits = state->bits; \
+ } while (0)
+
+/* Restore state from registers in inflate() */
+#define RESTORE() \
+ do { \
+ strm->next_out = put; \
+ strm->avail_out = left; \
+ strm->next_in = next; \
+ strm->avail_in = have; \
+ state->hold = hold; \
+ state->bits = bits; \
+ } while (0)
+
+/* Clear the input bit accumulator */
+#define INITBITS() \
+ do { \
+ hold = 0; \
+ bits = 0; \
+ } while (0)
+
+/* Get a byte of input into the bit accumulator, or return from inflate()
+ if there is no input available. */
+#define PULLBYTE() \
+ do { \
+ if (have == 0) goto inf_leave; \
+ have--; \
+ hold += (unsigned long)(*next++) << bits; \
+ bits += 8; \
+ } while (0)
+
+/* Assure that there are at least n bits in the bit accumulator. If there is
+ not enough available input to do that, then return from inflate(). */
+#define NEEDBITS(n) \
+ do { \
+ while (bits < (unsigned)(n)) \
+ PULLBYTE(); \
+ } while (0)
+
+/* Return the low n bits of the bit accumulator (n < 16) */
+#define BITS(n) \
+ ((unsigned)hold & ((1U << (n)) - 1))
+
+/* Remove n bits from the bit accumulator */
+#define DROPBITS(n) \
+ do { \
+ hold >>= (n); \
+ bits -= (unsigned)(n); \
+ } while (0)
+
+/* Remove zero to seven bits as needed to go to a byte boundary */
+#define BYTEBITS() \
+ do { \
+ hold >>= bits & 7; \
+ bits -= bits & 7; \
+ } while (0)
+
+/*
+ inflate() uses a state machine to process as much input data and generate as
+ much output data as possible before returning. The state machine is
+ structured roughly as follows:
+
+ for (;;) switch (state) {
+ ...
+ case STATEn:
+ if (not enough input data or output space to make progress)
+ return;
+ ... make progress ...
+ state = STATEm;
+ break;
+ ...
+ }
+
+ so when inflate() is called again, the same case is attempted again, and
+ if the appropriate resources are provided, the machine proceeds to the
+ next state. The NEEDBITS() macro is usually the way the state evaluates
+ whether it can proceed or should return. NEEDBITS() does the return if
+ the requested bits are not available. The typical use of the BITS macros
+ is:
+
+ NEEDBITS(n);
+ ... do something with BITS(n) ...
+ DROPBITS(n);
+
+ where NEEDBITS(n) either returns from inflate() if there isn't enough
+ input left to load n bits into the accumulator, or it continues. BITS(n)
+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
+ the low n bits off the accumulator. INITBITS() clears the accumulator
+ and sets the number of available bits to zero. BYTEBITS() discards just
+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
+
+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
+ if there is no input available. The decoding of variable length codes uses
+ PULLBYTE() directly in order to pull just enough bytes to decode the next
+ code, and no more.
+
+ Some states loop until they get enough input, making sure that enough
+ state information is maintained to continue the loop where it left off
+ if NEEDBITS() returns in the loop. For example, want, need, and keep
+ would all have to actually be part of the saved state in case NEEDBITS()
+ returns:
+
+ case STATEw:
+ while (want < need) {
+ NEEDBITS(n);
+ keep[want++] = BITS(n);
+ DROPBITS(n);
+ }
+ state = STATEx;
+ case STATEx:
+
+ As shown above, if the next state is also the next case, then the break
+ is omitted.
+
+ A state may also return if there is not enough output space available to
+ complete that state. Those states are copying stored data, writing a
+ literal byte, and copying a matching string.
+
+ When returning, a "goto inf_leave" is used to update the total counters,
+ update the check value, and determine whether any progress has been made
+ during that inflate() call in order to return the proper return code.
+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
+ When there is a window, goto inf_leave will update the window with the last
+ output written. If a goto inf_leave occurs in the middle of decompression
+ and there is no window currently, goto inf_leave will create one and copy
+ output to the window for the next call of inflate().
+
+ In this implementation, the flush parameter of inflate() only affects the
+ return code (per zlib.h). inflate() always writes as much as possible to
+ strm->next_out, given the space available and the provided input--the effect
+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
+ the allocation of and copying into a sliding window until necessary, which
+ provides the effect documented in zlib.h for Z_FINISH when the entire input
+ stream available. So the only thing the flush parameter actually does is:
+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
+ will return Z_BUF_ERROR if it has not reached the end of the stream.
+ */
+
+int ZEXPORT inflate(
+z_streamp strm,
+int flush)
+{
+ struct inflate_state FAR *state;
+ z_const unsigned char FAR *next; /* next input */
+ unsigned char FAR *put; /* next output */
+ unsigned have, left; /* available input and output */
+ unsigned long hold; /* bit buffer */
+ unsigned bits; /* bits in bit buffer */
+ unsigned in, out; /* save starting available input and output */
+ unsigned copy; /* number of stored or match bytes to copy */
+ unsigned char FAR *from; /* where to copy match bytes from */
+ code here; /* current decoding table entry */
+ code last; /* parent table entry */
+ unsigned len; /* length to copy for repeats, bits to drop */
+ int ret; /* return code */
+#ifdef GUNZIP
+ unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
+#endif
+ static const unsigned short order[19] = /* permutation of code lengths */
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
+ (strm->next_in == Z_NULL && strm->avail_in != 0))
+ return Z_STREAM_ERROR;
+
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
+ LOAD();
+ in = have;
+ out = left;
+ ret = Z_OK;
+ for (;;)
+ switch (state->mode) {
+ case HEAD:
+ if (state->wrap == 0) {
+ state->mode = TYPEDO;
+ break;
+ }
+ NEEDBITS(16);
+#ifdef GUNZIP
+ if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
+ state->check = crc32(0L, Z_NULL, 0);
+ CRC2(state->check, hold);
+ INITBITS();
+ state->mode = FLAGS;
+ break;
+ }
+ state->flags = 0; /* expect zlib header */
+ if (state->head != Z_NULL)
+ state->head->done = -1;
+ if (!(state->wrap & 1) || /* check if zlib header allowed */
+#else
+ if (
+#endif
+ ((BITS(8) << 8) + (hold >> 8)) % 31) {
+ strm->msg = (char *)"incorrect header check";
+ state->mode = BAD;
+ break;
+ }
+ if (BITS(4) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ DROPBITS(4);
+ len = BITS(4) + 8;
+ if (state->wbits == 0)
+ state->wbits = len;
+ else if (len > state->wbits) {
+ strm->msg = (char *)"invalid window size";
+ state->mode = BAD;
+ break;
+ }
+ state->dmax = 1U << len;
+ Tracev((stderr, "inflate: zlib header ok\n"));
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = hold & 0x200 ? DICTID : TYPE;
+ INITBITS();
+ break;
+#ifdef GUNZIP
+ case FLAGS:
+ NEEDBITS(16);
+ state->flags = (int)(hold);
+ if ((state->flags & 0xff) != Z_DEFLATED) {
+ strm->msg = (char *)"unknown compression method";
+ state->mode = BAD;
+ break;
+ }
+ if (state->flags & 0xe000) {
+ strm->msg = (char *)"unknown header flags set";
+ state->mode = BAD;
+ break;
+ }
+ if (state->head != Z_NULL)
+ state->head->text = (int)((hold >> 8) & 1);
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ state->mode = TIME;
+ case TIME:
+ NEEDBITS(32);
+ if (state->head != Z_NULL)
+ state->head->time = hold;
+ if (state->flags & 0x0200) CRC4(state->check, hold);
+ INITBITS();
+ state->mode = OS;
+ case OS:
+ NEEDBITS(16);
+ if (state->head != Z_NULL) {
+ state->head->xflags = (int)(hold & 0xff);
+ state->head->os = (int)(hold >> 8);
+ }
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ state->mode = EXLEN;
+ case EXLEN:
+ if (state->flags & 0x0400) {
+ NEEDBITS(16);
+ state->length = (unsigned)(hold);
+ if (state->head != Z_NULL)
+ state->head->extra_len = (unsigned)hold;
+ if (state->flags & 0x0200) CRC2(state->check, hold);
+ INITBITS();
+ }
+ else if (state->head != Z_NULL)
+ state->head->extra = Z_NULL;
+ state->mode = EXTRA;
+ case EXTRA:
+ if (state->flags & 0x0400) {
+ copy = state->length;
+ if (copy > have) copy = have;
+ if (copy) {
+ if (state->head != Z_NULL &&
+ state->head->extra != Z_NULL) {
+ len = state->head->extra_len - state->length;
+ zmemcpy(state->head->extra + len, next,
+ len + copy > state->head->extra_max ?
+ state->head->extra_max - len : copy);
+ }
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ state->length -= copy;
+ }
+ if (state->length) goto inf_leave;
+ }
+ state->length = 0;
+ state->mode = NAME;
+ case NAME:
+ if (state->flags & 0x0800) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->name != Z_NULL &&
+ state->length < state->head->name_max)
+ state->head->name[state->length++] = len;
+ } while (len && copy < have);
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->name = Z_NULL;
+ state->length = 0;
+ state->mode = COMMENT;
+ case COMMENT:
+ if (state->flags & 0x1000) {
+ if (have == 0) goto inf_leave;
+ copy = 0;
+ do {
+ len = (unsigned)(next[copy++]);
+ if (state->head != Z_NULL &&
+ state->head->comment != Z_NULL &&
+ state->length < state->head->comm_max)
+ state->head->comment[state->length++] = len;
+ } while (len && copy < have);
+ if (state->flags & 0x0200)
+ state->check = crc32(state->check, next, copy);
+ have -= copy;
+ next += copy;
+ if (len) goto inf_leave;
+ }
+ else if (state->head != Z_NULL)
+ state->head->comment = Z_NULL;
+ state->mode = HCRC;
+ case HCRC:
+ if (state->flags & 0x0200) {
+ NEEDBITS(16);
+ if (hold != (state->check & 0xffff)) {
+ strm->msg = (char *)"header crc mismatch";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ }
+ if (state->head != Z_NULL) {
+ state->head->hcrc = (int)((state->flags >> 9) & 1);
+ state->head->done = 1;
+ }
+ strm->adler = state->check = crc32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ break;
+#endif
+ case DICTID:
+ NEEDBITS(32);
+ strm->adler = state->check = ZSWAP32(hold);
+ INITBITS();
+ state->mode = DICT;
+ case DICT:
+ if (state->havedict == 0) {
+ RESTORE();
+ return Z_NEED_DICT;
+ }
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
+ state->mode = TYPE;
+ case TYPE:
+ if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
+ case TYPEDO:
+ if (state->last) {
+ BYTEBITS();
+ state->mode = CHECK;
+ break;
+ }
+ NEEDBITS(3);
+ state->last = BITS(1);
+ DROPBITS(1);
+ switch (BITS(2)) {
+ case 0: /* stored block */
+ Tracev((stderr, "inflate: stored block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = STORED;
+ break;
+ case 1: /* fixed block */
+ fixedtables(state);
+ Tracev((stderr, "inflate: fixed codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = LEN_; /* decode codes */
+ if (flush == Z_TREES) {
+ DROPBITS(2);
+ goto inf_leave;
+ }
+ break;
+ case 2: /* dynamic block */
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
+ state->last ? " (last)" : ""));
+ state->mode = TABLE;
+ break;
+ case 3:
+ strm->msg = (char *)"invalid block type";
+ state->mode = BAD;
+ }
+ DROPBITS(2);
+ break;
+ case STORED:
+ BYTEBITS(); /* go to byte boundary */
+ NEEDBITS(32);
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
+ strm->msg = (char *)"invalid stored block lengths";
+ state->mode = BAD;
+ break;
+ }
+ state->length = (unsigned)hold & 0xffff;
+ Tracev((stderr, "inflate: stored length %u\n",
+ state->length));
+ INITBITS();
+ state->mode = COPY_;
+ if (flush == Z_TREES) goto inf_leave;
+ case COPY_:
+ state->mode = COPY;
+ case COPY:
+ copy = state->length;
+ if (copy) {
+ if (copy > have) copy = have;
+ if (copy > left) copy = left;
+ if (copy == 0) goto inf_leave;
+ zmemcpy(put, next, copy);
+ have -= copy;
+ next += copy;
+ left -= copy;
+ put += copy;
+ state->length -= copy;
+ break;
+ }
+ Tracev((stderr, "inflate: stored end\n"));
+ state->mode = TYPE;
+ break;
+ case TABLE:
+ NEEDBITS(14);
+ state->nlen = BITS(5) + 257;
+ DROPBITS(5);
+ state->ndist = BITS(5) + 1;
+ DROPBITS(5);
+ state->ncode = BITS(4) + 4;
+ DROPBITS(4);
+#ifndef PKZIP_BUG_WORKAROUND
+ if (state->nlen > 286 || state->ndist > 30) {
+ strm->msg = (char *)"too many length or distance symbols";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracev((stderr, "inflate: table sizes ok\n"));
+ state->have = 0;
+ state->mode = LENLENS;
+ case LENLENS:
+ while (state->have < state->ncode) {
+ NEEDBITS(3);
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
+ DROPBITS(3);
+ }
+ while (state->have < 19)
+ state->lens[order[state->have++]] = 0;
+ state->next = state->codes;
+ state->lencode = (const code FAR *)(state->next);
+ state->lenbits = 7;
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid code lengths set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: code lengths ok\n"));
+ state->have = 0;
+ state->mode = CODELENS;
+ case CODELENS:
+ while (state->have < state->nlen + state->ndist) {
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.val < 16) {
+ DROPBITS(here.bits);
+ state->lens[state->have++] = here.val;
+ }
+ else {
+ if (here.val == 16) {
+ NEEDBITS(here.bits + 2);
+ DROPBITS(here.bits);
+ if (state->have == 0) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ len = state->lens[state->have - 1];
+ copy = 3 + BITS(2);
+ DROPBITS(2);
+ }
+ else if (here.val == 17) {
+ NEEDBITS(here.bits + 3);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 3 + BITS(3);
+ DROPBITS(3);
+ }
+ else {
+ NEEDBITS(here.bits + 7);
+ DROPBITS(here.bits);
+ len = 0;
+ copy = 11 + BITS(7);
+ DROPBITS(7);
+ }
+ if (state->have + copy > state->nlen + state->ndist) {
+ strm->msg = (char *)"invalid bit length repeat";
+ state->mode = BAD;
+ break;
+ }
+ while (copy--)
+ state->lens[state->have++] = (unsigned short)len;
+ }
+ }
+
+ /* handle error breaks in while */
+ if (state->mode == BAD) break;
+
+ /* check for end-of-block code (better have one) */
+ if (state->lens[256] == 0) {
+ strm->msg = (char *)"invalid code -- missing end-of-block";
+ state->mode = BAD;
+ break;
+ }
+
+ /* build code tables -- note: do not change the lenbits or distbits
+ values here (9 and 6) without reading the comments in inftrees.h
+ concerning the ENOUGH constants, which depend on those values */
+ state->next = state->codes;
+ state->lencode = (const code FAR *)(state->next);
+ state->lenbits = 9;
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
+ &(state->lenbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid literal/lengths set";
+ state->mode = BAD;
+ break;
+ }
+ state->distcode = (const code FAR *)(state->next);
+ state->distbits = 6;
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
+ &(state->next), &(state->distbits), state->work);
+ if (ret) {
+ strm->msg = (char *)"invalid distances set";
+ state->mode = BAD;
+ break;
+ }
+ Tracev((stderr, "inflate: codes ok\n"));
+ state->mode = LEN_;
+ if (flush == Z_TREES) goto inf_leave;
+ case LEN_:
+ state->mode = LEN;
+ case LEN:
+ if (have >= 6 && left >= 258) {
+ RESTORE();
+ inflate_fast(strm, out);
+ LOAD();
+ if (state->mode == TYPE)
+ state->back = -1;
+ break;
+ }
+ state->back = 0;
+ for (;;) {
+ here = state->lencode[BITS(state->lenbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if (here.op && (here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->lencode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ state->length = (unsigned)here.val;
+ if ((int)(here.op) == 0) {
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
+ "inflate: literal '%c'\n" :
+ "inflate: literal 0x%02x\n", here.val));
+ state->mode = LIT;
+ break;
+ }
+ if (here.op & 32) {
+ Tracevv((stderr, "inflate: end of block\n"));
+ state->back = -1;
+ state->mode = TYPE;
+ break;
+ }
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid literal/length code";
+ state->mode = BAD;
+ break;
+ }
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = LENEXT;
+ case LENEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->length += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+ Tracevv((stderr, "inflate: length %u\n", state->length));
+ state->was = state->length;
+ state->mode = DIST;
+ case DIST:
+ for (;;) {
+ here = state->distcode[BITS(state->distbits)];
+ if ((unsigned)(here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ if ((here.op & 0xf0) == 0) {
+ last = here;
+ for (;;) {
+ here = state->distcode[last.val +
+ (BITS(last.bits + last.op) >> last.bits)];
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
+ PULLBYTE();
+ }
+ DROPBITS(last.bits);
+ state->back += last.bits;
+ }
+ DROPBITS(here.bits);
+ state->back += here.bits;
+ if (here.op & 64) {
+ strm->msg = (char *)"invalid distance code";
+ state->mode = BAD;
+ break;
+ }
+ state->offset = (unsigned)here.val;
+ state->extra = (unsigned)(here.op) & 15;
+ state->mode = DISTEXT;
+ case DISTEXT:
+ if (state->extra) {
+ NEEDBITS(state->extra);
+ state->offset += BITS(state->extra);
+ DROPBITS(state->extra);
+ state->back += state->extra;
+ }
+#ifdef INFLATE_STRICT
+ if (state->offset > state->dmax) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#endif
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
+ state->mode = MATCH;
+ case MATCH:
+ if (left == 0) goto inf_leave;
+ copy = out - left;
+ if (state->offset > copy) { /* copy from window */
+ copy = state->offset - copy;
+ if (copy > state->whave) {
+ if (state->sane) {
+ strm->msg = (char *)"invalid distance too far back";
+ state->mode = BAD;
+ break;
+ }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ Trace((stderr, "inflate.c too far\n"));
+ copy -= state->whave;
+ if (copy > state->length) copy = state->length;
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = 0;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+#endif
+ }
+ if (copy > state->wnext) {
+ copy -= state->wnext;
+ from = state->window + (state->wsize - copy);
+ }
+ else
+ from = state->window + (state->wnext - copy);
+ if (copy > state->length) copy = state->length;
+ }
+ else { /* copy from output */
+ from = put - state->offset;
+ copy = state->length;
+ }
+ if (copy > left) copy = left;
+ left -= copy;
+ state->length -= copy;
+ do {
+ *put++ = *from++;
+ } while (--copy);
+ if (state->length == 0) state->mode = LEN;
+ break;
+ case LIT:
+ if (left == 0) goto inf_leave;
+ *put++ = (unsigned char)(state->length);
+ left--;
+ state->mode = LEN;
+ break;
+ case CHECK:
+ if (state->wrap) {
+ NEEDBITS(32);
+ out -= left;
+ strm->total_out += out;
+ state->total += out;
+ if (out)
+ strm->adler = state->check =
+ UPDATE(state->check, put - out, out);
+ out = left;
+ if ((
+#ifdef GUNZIP
+ state->flags ? hold :
+#endif
+ ZSWAP32(hold)) != state->check) {
+ strm->msg = (char *)"incorrect data check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: check matches trailer\n"));
+ }
+#ifdef GUNZIP
+ state->mode = LENGTH;
+ case LENGTH:
+ if (state->wrap && state->flags) {
+ NEEDBITS(32);
+ if (hold != (state->total & 0xffffffffUL)) {
+ strm->msg = (char *)"incorrect length check";
+ state->mode = BAD;
+ break;
+ }
+ INITBITS();
+ Tracev((stderr, "inflate: length matches trailer\n"));
+ }
+#endif
+ state->mode = DONE;
+ case DONE:
+ ret = Z_STREAM_END;
+ goto inf_leave;
+ case BAD:
+ ret = Z_DATA_ERROR;
+ goto inf_leave;
+ case MEM:
+ return Z_MEM_ERROR;
+ case SYNC:
+ default:
+ return Z_STREAM_ERROR;
+ }
+
+ /*
+ Return from inflate(), updating the total counts and the check value.
+ If there was no progress during the inflate() call, return a buffer
+ error. Call updatewindow() to create and/or update the window state.
+ Note: a memory error from inflate() is non-recoverable.
+ */
+ inf_leave:
+ RESTORE();
+ if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
+ (state->mode < CHECK || flush != Z_FINISH)))
+ if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ in -= strm->avail_in;
+ out -= strm->avail_out;
+ strm->total_in += in;
+ strm->total_out += out;
+ state->total += out;
+ if (state->wrap && out)
+ strm->adler = state->check =
+ UPDATE(state->check, strm->next_out - out, out);
+ strm->data_type = state->bits + (state->last ? 64 : 0) +
+ (state->mode == TYPE ? 128 : 0) +
+ (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
+ if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
+ ret = Z_BUF_ERROR;
+ return ret;
+}
+
+int ZEXPORT inflateEnd(
+z_streamp strm)
+{
+ struct inflate_state FAR *state;
+ if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->window != Z_NULL) ZFREE(strm, state->window);
+ ZFREE(strm, strm->state);
+ strm->state = Z_NULL;
+ Tracev((stderr, "inflate: end\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateGetDictionary(
+z_streamp strm,
+Bytef *dictionary,
+uInt *dictLength)
+{
+ struct inflate_state FAR *state;
+
+ /* check state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+
+ /* copy dictionary */
+ if (state->whave && dictionary != Z_NULL) {
+ zmemcpy(dictionary, state->window + state->wnext,
+ state->whave - state->wnext);
+ zmemcpy(dictionary + state->whave - state->wnext,
+ state->window, state->wnext);
+ }
+ if (dictLength != Z_NULL)
+ *dictLength = state->whave;
+ return Z_OK;
+}
+
+int ZEXPORT inflateSetDictionary(
+z_streamp strm,
+const Bytef *dictionary,
+uInt dictLength)
+{
+ struct inflate_state FAR *state;
+ unsigned long dictid;
+ int ret;
+
+ dictid = 0;
+ /* check state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (state->wrap != 0 && state->mode != DICT)
+ return Z_STREAM_ERROR;
+
+ /* check for correct dictionary identifier */
+ if (state->mode == DICT) {
+ dictid = adler32(0L, Z_NULL, 0);
+ dictid = adler32(dictid, dictionary, dictLength);
+ if (dictid != state->check)
+ return Z_DATA_ERROR;
+ }
+
+ /* copy dictionary to window using updatewindow(), which will amend the
+ existing dictionary if appropriate */
+ ret = updatewindow(strm, dictionary + dictLength, dictLength);
+ if (ret) {
+ state->mode = MEM;
+ return Z_MEM_ERROR;
+ }
+ state->havedict = 1;
+ Tracev((stderr, "inflate: dictionary set\n"));
+ return Z_OK;
+}
+
+int ZEXPORT inflateGetHeader(
+z_streamp strm,
+gz_headerp head)
+{
+ struct inflate_state FAR *state;
+
+ /* check state */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
+
+ /* save header structure */
+ state->head = head;
+ head->done = 0;
+ return Z_OK;
+}
+
+/*
+ Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
+ or when out of input. When called, *have is the number of pattern bytes
+ found in order so far, in 0..3. On return *have is updated to the new
+ state. If on return *have equals four, then the pattern was found and the
+ return value is how many bytes were read including the last byte of the
+ pattern. If *have is less than four, then the pattern has not been found
+ yet and the return value is len. In the latter case, syncsearch() can be
+ called again with more data and the *have state. *have is initialized to
+ zero for the first call.
+ */
+local unsigned syncsearch(
+unsigned FAR *have,
+const unsigned char FAR *buf,
+unsigned len)
+{
+ unsigned got;
+ unsigned next;
+
+ got = *have;
+ next = 0;
+ while (next < len && got < 4) {
+ if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
+ got++;
+ else if (buf[next])
+ got = 0;
+ else
+ got = 4 - got;
+ next++;
+ }
+ *have = got;
+ return next;
+}
+
+int ZEXPORT inflateSync(
+z_streamp strm)
+{
+ unsigned len; /* number of bytes to look at or looked at */
+ unsigned long in, out; /* temporary to save total_in and total_out */
+ unsigned char buf[4]; /* to restore bit buffer to byte string */
+ struct inflate_state FAR *state;
+
+ /* check parameters */
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
+
+ /* if first time, start search in bit buffer */
+ if (state->mode != SYNC) {
+ state->mode = SYNC;
+ state->hold <<= state->bits & 7;
+ state->bits -= state->bits & 7;
+ len = 0;
+ while (state->bits >= 8) {
+ buf[len++] = (unsigned char)(state->hold);
+ state->hold >>= 8;
+ state->bits -= 8;
+ }
+ state->have = 0;
+ syncsearch(&(state->have), buf, len);
+ }
+
+ /* search available input */
+ len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
+ strm->avail_in -= len;
+ strm->next_in += len;
+ strm->total_in += len;
+
+ /* return no joy or set up to restart inflate() on a new block */
+ if (state->have != 4) return Z_DATA_ERROR;
+ in = strm->total_in; out = strm->total_out;
+ inflateReset(strm);
+ strm->total_in = in; strm->total_out = out;
+ state->mode = TYPE;
+ return Z_OK;
+}
+
+/*
+ Returns true if inflate is currently at the end of a block generated by
+ Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
+ implementation to provide an additional safety check. PPP uses
+ Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
+ block. When decompressing, PPP checks that at the end of input packet,
+ inflate is waiting for these length bytes.
+ */
+int ZEXPORT inflateSyncPoint(
+z_streamp strm)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ return state->mode == STORED && state->bits == 0;
+}
+
+int ZEXPORT inflateCopy(
+z_streamp dest,
+z_streamp source)
+{
+ struct inflate_state FAR *state;
+ struct inflate_state FAR *copy;
+ unsigned char FAR *window;
+ unsigned wsize;
+
+ /* check input */
+ if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
+ source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
+ return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)source->state;
+
+ /* allocate space */
+ copy = (struct inflate_state FAR *)
+ ZALLOC(source, 1, sizeof(struct inflate_state));
+ if (copy == Z_NULL) return Z_MEM_ERROR;
+ window = Z_NULL;
+ if (state->window != Z_NULL) {
+ window = (unsigned char FAR *)
+ ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
+ if (window == Z_NULL) {
+ ZFREE(source, copy);
+ return Z_MEM_ERROR;
+ }
+ }
+
+ /* copy state */
+ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
+ zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
+ if (state->lencode >= state->codes &&
+ state->lencode <= state->codes + ENOUGH - 1) {
+ copy->lencode = copy->codes + (state->lencode - state->codes);
+ copy->distcode = copy->codes + (state->distcode - state->codes);
+ }
+ copy->next = copy->codes + (state->next - state->codes);
+ if (window != Z_NULL) {
+ wsize = 1U << state->wbits;
+ zmemcpy(window, state->window, wsize);
+ }
+ copy->window = window;
+ dest->state = (struct internal_state FAR *)copy;
+ return Z_OK;
+}
+
+int ZEXPORT inflateUndermine(
+z_streamp strm,
+int subvert)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+ state = (struct inflate_state FAR *)strm->state;
+ state->sane = !subvert;
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+ return Z_OK;
+#else
+ state->sane = 1;
+ return Z_DATA_ERROR;
+#endif
+}
+
+long ZEXPORT inflateMark(
+z_streamp strm)
+{
+ struct inflate_state FAR *state;
+
+ if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
+ state = (struct inflate_state FAR *)strm->state;
+ return ((long)(state->back) << 16) +
+ (state->mode == COPY ? state->length :
+ (state->mode == MATCH ? state->was - state->length : 0));
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inftrees.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inftrees.c
new file mode 100644
index 0000000000..05ba1324f4
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_inftrees.c
@@ -0,0 +1,306 @@
+/* inftrees.c -- generate Huffman trees for efficient decoding
+ * Copyright (C) 1995-2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "zutil.h"
+#include "inftrees.h"
+
+#define MAXBITS 15
+
+const char inflate_copyright[] =
+ " inflate 1.2.8 Copyright 1995-2013 Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
+
+/*
+ Build a set of tables to decode the provided canonical Huffman code.
+ The code lengths are lens[0..codes-1]. The result starts at *table,
+ whose indices are 0..2^bits-1. work is a writable array of at least
+ lens shorts, which is used as a work area. type is the type of code
+ to be generated, CODES, LENS, or DISTS. On return, zero is success,
+ -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
+ on return points to the next available entry's address. bits is the
+ requested root table index bits, and on return it is the actual root
+ table index bits. It will differ if the request is greater than the
+ longest code or if it is less than the shortest code.
+ */
+int ZLIB_INTERNAL inflate_table(
+codetype type,
+unsigned short FAR *lens,
+unsigned codes,
+code FAR * FAR *table,
+unsigned FAR *bits,
+unsigned short FAR *work)
+{
+ unsigned len; /* a code's length in bits */
+ unsigned sym; /* index of code symbols */
+ unsigned min, max; /* minimum and maximum code lengths */
+ unsigned root; /* number of index bits for root table */
+ unsigned curr; /* number of index bits for current table */
+ unsigned drop; /* code bits to drop for sub-table */
+ int left; /* number of prefix codes available */
+ unsigned used; /* code entries in table used */
+ unsigned huff; /* Huffman code */
+ unsigned incr; /* for incrementing code, index */
+ unsigned fill; /* index for replicating entries */
+ unsigned low; /* low bits for current root entry */
+ unsigned mask; /* mask for low root bits */
+ code here; /* table entry for duplication */
+ code FAR *next; /* next available space in table */
+ const unsigned short FAR *base; /* base value table to use */
+ const unsigned short FAR *extra; /* extra bits table to use */
+ int end; /* use base and extra for symbol > end */
+ unsigned short count[MAXBITS+1]; /* number of codes of each length */
+ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
+ static const unsigned short lbase[31] = { /* Length codes 257..285 base */
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
+ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
+ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
+ 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
+ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+ 8193, 12289, 16385, 24577, 0, 0};
+ static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
+ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
+ 28, 28, 29, 29, 64, 64};
+
+ /*
+ Process a set of code lengths to create a canonical Huffman code. The
+ code lengths are lens[0..codes-1]. Each length corresponds to the
+ symbols 0..codes-1. The Huffman code is generated by first sorting the
+ symbols by length from short to long, and retaining the symbol order
+ for codes with equal lengths. Then the code starts with all zero bits
+ for the first code of the shortest length, and the codes are integer
+ increments for the same length, and zeros are appended as the length
+ increases. For the deflate format, these bits are stored backwards
+ from their more natural integer increment ordering, and so when the
+ decoding tables are built in the large loop below, the integer codes
+ are incremented backwards.
+
+ This routine assumes, but does not check, that all of the entries in
+ lens[] are in the range 0..MAXBITS. The caller must assure this.
+ 1..MAXBITS is interpreted as that code length. zero means that that
+ symbol does not occur in this code.
+
+ The codes are sorted by computing a count of codes for each length,
+ creating from that a table of starting indices for each length in the
+ sorted table, and then entering the symbols in order in the sorted
+ table. The sorted table is work[], with that space being provided by
+ the caller.
+
+ The length counts are used for other purposes as well, i.e. finding
+ the minimum and maximum length codes, determining if there are any
+ codes at all, checking for a valid set of lengths, and looking ahead
+ at length counts to determine sub-table sizes when building the
+ decoding tables.
+ */
+
+ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
+ for (len = 0; len <= MAXBITS; len++)
+ count[len] = 0;
+ for (sym = 0; sym < codes; sym++)
+ count[lens[sym]]++;
+
+ /* bound code lengths, force root to be within code lengths */
+ root = *bits;
+ for (max = MAXBITS; max >= 1; max--)
+ if (count[max] != 0) break;
+ if (root > max) root = max;
+ if (max == 0) { /* no symbols to code at all */
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)1;
+ here.val = (unsigned short)0;
+ *(*table)++ = here; /* make a table to force an error */
+ *(*table)++ = here;
+ *bits = 1;
+ return 0; /* no symbols, but wait for decoding to report error */
+ }
+ for (min = 1; min < max; min++)
+ if (count[min] != 0) break;
+ if (root < min) root = min;
+
+ /* check for an over-subscribed or incomplete set of lengths */
+ left = 1;
+ for (len = 1; len <= MAXBITS; len++) {
+ left <<= 1;
+ left -= count[len];
+ if (left < 0) return -1; /* over-subscribed */
+ }
+ if (left > 0 && (type == CODES || max != 1))
+ return -1; /* incomplete set */
+
+ /* generate offsets into symbol table for each length for sorting */
+ offs[1] = 0;
+ for (len = 1; len < MAXBITS; len++)
+ offs[len + 1] = offs[len] + count[len];
+
+ /* sort symbols by length, by symbol order within each length */
+ for (sym = 0; sym < codes; sym++)
+ if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
+
+ /*
+ Create and fill in decoding tables. In this loop, the table being
+ filled is at next and has curr index bits. The code being used is huff
+ with length len. That code is converted to an index by dropping drop
+ bits off of the bottom. For codes where len is less than drop + curr,
+ those top drop + curr - len bits are incremented through all values to
+ fill the table with replicated entries.
+
+ root is the number of index bits for the root table. When len exceeds
+ root, sub-tables are created pointed to by the root entry with an index
+ of the low root bits of huff. This is saved in low to check for when a
+ new sub-table should be started. drop is zero when the root table is
+ being filled, and drop is root when sub-tables are being filled.
+
+ When a new sub-table is needed, it is necessary to look ahead in the
+ code lengths to determine what size sub-table is needed. The length
+ counts are used for this, and so count[] is decremented as codes are
+ entered in the tables.
+
+ used keeps track of how many table entries have been allocated from the
+ provided *table space. It is checked for LENS and DIST tables against
+ the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
+ the initial root table size constants. See the comments in inftrees.h
+ for more information.
+
+ sym increments through all symbols, and the loop terminates when
+ all codes of length max, i.e. all codes, have been processed. This
+ routine permits incomplete codes, so another loop after this one fills
+ in the rest of the decoding tables with invalid code markers.
+ */
+
+ /* set up for code type */
+ switch (type) {
+ case CODES:
+ base = extra = work; /* dummy value--not used */
+ end = 19;
+ break;
+ case LENS:
+ base = lbase;
+ base -= 257;
+ extra = lext;
+ extra -= 257;
+ end = 256;
+ break;
+ default: /* DISTS */
+ base = dbase;
+ extra = dext;
+ end = -1;
+ }
+
+ /* initialize state for loop */
+ huff = 0; /* starting code */
+ sym = 0; /* starting code symbol */
+ len = min; /* starting code length */
+ next = *table; /* current table to fill in */
+ curr = root; /* current table index bits */
+ drop = 0; /* current bits to drop from code for index */
+ low = (unsigned)(-1); /* trigger new sub-table when len > root */
+ used = 1U << root; /* use root table entries */
+ mask = used - 1; /* mask for comparing low */
+
+ /* check available table space */
+ if ((type == LENS && used > ENOUGH_LENS) ||
+ (type == DISTS && used > ENOUGH_DISTS))
+ return 1;
+
+ /* process all codes and make table entries */
+ for (;;) {
+ /* create table entry */
+ here.bits = (unsigned char)(len - drop);
+ if ((int)(work[sym]) < end) {
+ here.op = (unsigned char)0;
+ here.val = work[sym];
+ }
+ else if ((int)(work[sym]) > end) {
+ here.op = (unsigned char)(extra[work[sym]]);
+ here.val = base[work[sym]];
+ }
+ else {
+ here.op = (unsigned char)(32 + 64); /* end of block */
+ here.val = 0;
+ }
+
+ /* replicate for those indices with low len bits equal to huff */
+ incr = 1U << (len - drop);
+ fill = 1U << curr;
+ min = fill; /* save offset to next table */
+ do {
+ fill -= incr;
+ next[(huff >> drop) + fill] = here;
+ } while (fill != 0);
+
+ /* backwards increment the len-bit code huff */
+ incr = 1U << (len - 1);
+ while (huff & incr)
+ incr >>= 1;
+ if (incr != 0) {
+ huff &= incr - 1;
+ huff += incr;
+ }
+ else
+ huff = 0;
+
+ /* go to next symbol, update count, len */
+ sym++;
+ if (--(count[len]) == 0) {
+ if (len == max) break;
+ len = lens[work[sym]];
+ }
+
+ /* create new sub-table if needed */
+ if (len > root && (huff & mask) != low) {
+ /* if first time, transition to sub-tables */
+ if (drop == 0)
+ drop = root;
+
+ /* increment past last table */
+ next += min; /* here min is 1 << curr */
+
+ /* determine length of next table */
+ curr = len - drop;
+ left = (int)(1 << curr);
+ while (curr + drop < max) {
+ left -= count[curr + drop];
+ if (left <= 0) break;
+ curr++;
+ left <<= 1;
+ }
+
+ /* check for enough space */
+ used += 1U << curr;
+ if ((type == LENS && used > ENOUGH_LENS) ||
+ (type == DISTS && used > ENOUGH_DISTS))
+ return 1;
+
+ /* point entry in root table to sub-table */
+ low = huff & mask;
+ (*table)[low].op = (unsigned char)curr;
+ (*table)[low].bits = (unsigned char)root;
+ (*table)[low].val = (unsigned short)(next - *table);
+ }
+ }
+
+ /* fill in remaining table entry if code is incomplete (guaranteed to have
+ at most one remaining entry, since if the code is incomplete, the
+ maximum code length that was allowed to get this far is one bit) */
+ if (huff != 0) {
+ here.op = (unsigned char)64; /* invalid code marker */
+ here.bits = (unsigned char)(len - drop);
+ here.val = (unsigned short)0;
+ next[huff] = here;
+ }
+
+ /* set return parameters */
+ *table += used;
+ *bits = root;
+ return 0;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_trees.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_trees.c
new file mode 100644
index 0000000000..e1fd952046
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_trees.c
@@ -0,0 +1,1226 @@
+/* trees.c -- output deflated data using Huffman coding
+ * Copyright (C) 1995-2012 Jean-loup Gailly
+ * detect_data_type() function provided freely by Cosmin Truta, 2006
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/*
+ * ALGORITHM
+ *
+ * The "deflation" process uses several Huffman trees. The more
+ * common source values are represented by shorter bit sequences.
+ *
+ * Each code tree is stored in a compressed form which is itself
+ * a Huffman encoding of the lengths of all the code strings (in
+ * ascending order by source values). The actual code strings are
+ * reconstructed from the lengths in the inflate process, as described
+ * in the deflate specification.
+ *
+ * REFERENCES
+ *
+ * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
+ * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
+ *
+ * Storer, James A.
+ * Data Compression: Methods and Theory, pp. 49-50.
+ * Computer Science Press, 1988. ISBN 0-7167-8156-5.
+ *
+ * Sedgewick, R.
+ * Algorithms, p290.
+ * Addison-Wesley, 1983. ISBN 0-201-06672-6.
+ */
+
+/* @(#) $Id$ */
+
+/* #define GEN_TREES_H */
+
+#include "deflate.h"
+
+#ifdef DEBUG
+# include <ctype.h>
+#endif
+
+/* ===========================================================================
+ * Constants
+ */
+
+#define MAX_BL_BITS 7
+/* Bit length codes must not exceed MAX_BL_BITS bits */
+
+#define END_BLOCK 256
+/* end of block literal code */
+
+#define REP_3_6 16
+/* repeat previous bit length 3-6 times (2 bits of repeat count) */
+
+#define REPZ_3_10 17
+/* repeat a zero length 3-10 times (3 bits of repeat count) */
+
+#define REPZ_11_138 18
+/* repeat a zero length 11-138 times (7 bits of repeat count) */
+
+local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
+ = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
+
+local const int extra_dbits[D_CODES] /* extra bits for each distance code */
+ = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
+
+local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
+ = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
+
+local const uch bl_order[BL_CODES]
+ = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
+/* The lengths of the bit length codes are sent in order of decreasing
+ * probability, to avoid transmitting the lengths for unused bit length codes.
+ */
+
+/* ===========================================================================
+ * Local data. These are initialized only once.
+ */
+
+#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
+
+#if defined(GEN_TREES_H) || !defined(STDC)
+/* non ANSI compilers may not accept trees.h */
+
+local ct_data static_ltree[L_CODES+2];
+/* The static literal tree. Since the bit lengths are imposed, there is no
+ * need for the L_CODES extra codes used during heap construction. However
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
+ * below).
+ */
+
+local ct_data static_dtree[D_CODES];
+/* The static distance tree. (Actually a trivial tree since all codes use
+ * 5 bits.)
+ */
+
+uch _dist_code[DIST_CODE_LEN];
+/* Distance codes. The first 256 values correspond to the distances
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
+ * the 15 bit distances.
+ */
+
+uch _length_code[MAX_MATCH-MIN_MATCH+1];
+/* length code for each normalized match length (0 == MIN_MATCH) */
+
+local int base_length[LENGTH_CODES];
+/* First normalized length for each code (0 = MIN_MATCH) */
+
+local int base_dist[D_CODES];
+/* First normalized distance for each code (0 = distance of 1) */
+
+#else
+# include "trees.h"
+#endif /* GEN_TREES_H */
+
+struct static_tree_desc_s {
+ const ct_data *static_tree; /* static tree or NULL */
+ const intf *extra_bits; /* extra bits for each code or NULL */
+ int extra_base; /* base index for extra_bits */
+ int elems; /* max number of elements in the tree */
+ int max_length; /* max bit length for the codes */
+};
+
+local static_tree_desc static_l_desc =
+{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
+
+local static_tree_desc static_d_desc =
+{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
+
+local static_tree_desc static_bl_desc =
+{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
+
+/* ===========================================================================
+ * Local (static) routines in this file.
+ */
+
+local void tr_static_init OF((void));
+local void init_block OF((deflate_state *s));
+local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
+local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
+local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
+local void build_tree OF((deflate_state *s, tree_desc *desc));
+local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
+local int build_bl_tree OF((deflate_state *s));
+local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
+ int blcodes));
+local void compress_block OF((deflate_state *s, const ct_data *ltree,
+ const ct_data *dtree));
+local int detect_data_type OF((deflate_state *s));
+local unsigned bi_reverse OF((unsigned value, int length));
+local void bi_windup OF((deflate_state *s));
+local void bi_flush OF((deflate_state *s));
+local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
+ int header));
+
+#ifdef GEN_TREES_H
+local void gen_trees_header OF((void));
+#endif
+
+#ifndef DEBUG
+# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
+ /* Send a code of the given tree. c and tree must not have side effects */
+
+#else /* DEBUG */
+# define send_code(s, c, tree) \
+ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
+ send_bits(s, tree[c].Code, tree[c].Len); }
+#endif
+
+/* ===========================================================================
+ * Output a short LSB first on the stream.
+ * IN assertion: there is enough room in pendingBuf.
+ */
+#define put_short(s, w) { \
+ put_byte(s, (uch)((w) & 0xff)); \
+ put_byte(s, (uch)((ush)(w) >> 8)); \
+}
+
+/* ===========================================================================
+ * Send a value on a given number of bits.
+ * IN assertion: length <= 16 and value fits in length bits.
+ */
+#ifdef DEBUG
+local void send_bits OF((deflate_state *s, int value, int length));
+
+local void send_bits(
+ deflate_state *s,
+ int value, /* value to send */
+ int length) /* number of bits */
+{
+ Tracevv((stderr," l %2d v %4x ", length, value));
+ Assert(length > 0 && length <= 15, "invalid length");
+ s->bits_sent += (ulg)length;
+
+ /* If not enough room in bi_buf, use (valid) bits from bi_buf and
+ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
+ * unused bits in value.
+ */
+ if (s->bi_valid > (int)Buf_size - length) {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ put_short(s, s->bi_buf);
+ s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
+ s->bi_valid += length - Buf_size;
+ } else {
+ s->bi_buf |= (ush)value << s->bi_valid;
+ s->bi_valid += length;
+ }
+}
+#else /* !DEBUG */
+
+#define send_bits(s, value, length) \
+{ int len = length;\
+ if (s->bi_valid > (int)Buf_size - len) {\
+ int val = value;\
+ s->bi_buf |= (ush)val << s->bi_valid;\
+ put_short(s, s->bi_buf);\
+ s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
+ s->bi_valid += len - Buf_size;\
+ } else {\
+ s->bi_buf |= (ush)(value) << s->bi_valid;\
+ s->bi_valid += len;\
+ }\
+}
+#endif /* DEBUG */
+
+
+/* the arguments must not have side effects */
+
+/* ===========================================================================
+ * Initialize the various 'constant' tables.
+ */
+local void tr_static_init()
+{
+#if defined(GEN_TREES_H) || !defined(STDC)
+ static int static_init_done = 0;
+ int n; /* iterates over tree elements */
+ int bits; /* bit counter */
+ int length; /* length value */
+ int code; /* code value */
+ int dist; /* distance index */
+ ush bl_count[MAX_BITS+1];
+ /* number of codes at each bit length for an optimal tree */
+
+ if (static_init_done) return;
+
+ /* For some embedded targets, global variables are not initialized: */
+#ifdef NO_INIT_GLOBAL_POINTERS
+ static_l_desc.static_tree = static_ltree;
+ static_l_desc.extra_bits = extra_lbits;
+ static_d_desc.static_tree = static_dtree;
+ static_d_desc.extra_bits = extra_dbits;
+ static_bl_desc.extra_bits = extra_blbits;
+#endif
+
+ /* Initialize the mapping length (0..255) -> length code (0..28) */
+ length = 0;
+ for (code = 0; code < LENGTH_CODES-1; code++) {
+ base_length[code] = length;
+ for (n = 0; n < (1<<extra_lbits[code]); n++) {
+ _length_code[length++] = (uch)code;
+ }
+ }
+ Assert (length == 256, "tr_static_init: length != 256");
+ /* Note that the length 255 (match length 258) can be represented
+ * in two different ways: code 284 + 5 bits or code 285, so we
+ * overwrite length_code[255] to use the best encoding:
+ */
+ _length_code[length-1] = (uch)code;
+
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
+ dist = 0;
+ for (code = 0 ; code < 16; code++) {
+ base_dist[code] = dist;
+ for (n = 0; n < (1<<extra_dbits[code]); n++) {
+ _dist_code[dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: dist != 256");
+ dist >>= 7; /* from now on, all distances are divided by 128 */
+ for ( ; code < D_CODES; code++) {
+ base_dist[code] = dist << 7;
+ for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
+ _dist_code[256 + dist++] = (uch)code;
+ }
+ }
+ Assert (dist == 256, "tr_static_init: 256+dist != 512");
+
+ /* Construct the codes of the static literal tree */
+ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
+ n = 0;
+ while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
+ while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
+ while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
+ while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
+ /* Codes 286 and 287 do not exist, but we must include them in the
+ * tree construction to get a canonical Huffman tree (longest code
+ * all ones)
+ */
+ gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
+
+ /* The static distance tree is trivial: */
+ for (n = 0; n < D_CODES; n++) {
+ static_dtree[n].Len = 5;
+ static_dtree[n].Code = bi_reverse((unsigned)n, 5);
+ }
+ static_init_done = 1;
+
+# ifdef GEN_TREES_H
+ gen_trees_header();
+# endif
+#endif /* defined(GEN_TREES_H) || !defined(STDC) */
+}
+
+/* ===========================================================================
+ * Genererate the file trees.h describing the static trees.
+ */
+#ifdef GEN_TREES_H
+# ifndef DEBUG
+# include <stdio.h>
+# endif
+
+# define SEPARATOR(i, last, width) \
+ ((i) == (last)? "\n};\n\n" : \
+ ((i) % (width) == (width)-1 ? ",\n" : ", "))
+
+void gen_trees_header()
+{
+ FILE *header = fopen("trees.h", "w");
+ int i;
+
+ Assert (header != NULL, "Can't open trees.h");
+ fprintf(header,
+ "/* header created automatically with -DGEN_TREES_H */\n\n");
+
+ fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
+ for (i = 0; i < L_CODES+2; i++) {
+ fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
+ static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
+ }
+
+ fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
+ static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
+ }
+
+ fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
+ for (i = 0; i < DIST_CODE_LEN; i++) {
+ fprintf(header, "%2u%s", _dist_code[i],
+ SEPARATOR(i, DIST_CODE_LEN-1, 20));
+ }
+
+ fprintf(header,
+ "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
+ for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
+ fprintf(header, "%2u%s", _length_code[i],
+ SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
+ }
+
+ fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
+ for (i = 0; i < LENGTH_CODES; i++) {
+ fprintf(header, "%1u%s", base_length[i],
+ SEPARATOR(i, LENGTH_CODES-1, 20));
+ }
+
+ fprintf(header, "local const int base_dist[D_CODES] = {\n");
+ for (i = 0; i < D_CODES; i++) {
+ fprintf(header, "%5u%s", base_dist[i],
+ SEPARATOR(i, D_CODES-1, 10));
+ }
+
+ fclose(header);
+}
+#endif /* GEN_TREES_H */
+
+/* ===========================================================================
+ * Initialize the tree data structures for a new zlib stream.
+ */
+void ZLIB_INTERNAL _tr_init(
+ deflate_state *s)
+{
+ tr_static_init();
+
+ s->l_desc.dyn_tree = s->dyn_ltree;
+ s->l_desc.stat_desc = &static_l_desc;
+
+ s->d_desc.dyn_tree = s->dyn_dtree;
+ s->d_desc.stat_desc = &static_d_desc;
+
+ s->bl_desc.dyn_tree = s->bl_tree;
+ s->bl_desc.stat_desc = &static_bl_desc;
+
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+#ifdef DEBUG
+ s->compressed_len = 0L;
+ s->bits_sent = 0L;
+#endif
+
+ /* Initialize the first block of the first file: */
+ init_block(s);
+}
+
+/* ===========================================================================
+ * Initialize a new block.
+ */
+local void init_block(
+ deflate_state *s)
+{
+ int n; /* iterates over tree elements */
+
+ /* Initialize the trees. */
+ for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
+ for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
+ for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
+
+ s->dyn_ltree[END_BLOCK].Freq = 1;
+ s->opt_len = s->static_len = 0L;
+ s->last_lit = s->matches = 0;
+}
+
+#define SMALLEST 1
+/* Index within the heap array of least frequent node in the Huffman tree */
+
+
+/* ===========================================================================
+ * Remove the smallest element from the heap and recreate the heap with
+ * one less element. Updates heap and heap_len.
+ */
+#define pqremove(s, tree, top) \
+{\
+ top = s->heap[SMALLEST]; \
+ s->heap[SMALLEST] = s->heap[s->heap_len--]; \
+ pqdownheap(s, tree, SMALLEST); \
+}
+
+/* ===========================================================================
+ * Compares to subtrees, using the tree depth as tie breaker when
+ * the subtrees have equal frequency. This minimizes the worst case length.
+ */
+#define smaller(tree, n, m, depth) \
+ (tree[n].Freq < tree[m].Freq || \
+ (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
+
+/* ===========================================================================
+ * Restore the heap property by moving down the tree starting at node k,
+ * exchanging a node with the smallest of its two sons if necessary, stopping
+ * when the heap property is re-established (each father smaller than its
+ * two sons).
+ */
+local void pqdownheap(
+ deflate_state *s,
+ ct_data *tree, /* the tree to restore */
+ int k) /* node to move down */
+{
+ int v = s->heap[k];
+ int j = k << 1; /* left son of k */
+ while (j <= s->heap_len) {
+ /* Set j to the smallest of the two sons: */
+ if (j < s->heap_len &&
+ smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
+ j++;
+ }
+ /* Exit if v is smaller than both sons */
+ if (smaller(tree, v, s->heap[j], s->depth)) break;
+
+ /* Exchange v with the smallest son */
+ s->heap[k] = s->heap[j]; k = j;
+
+ /* And continue down the tree, setting j to the left son of k */
+ j <<= 1;
+ }
+ s->heap[k] = v;
+}
+
+/* ===========================================================================
+ * Compute the optimal bit lengths for a tree and update the total bit length
+ * for the current block.
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
+ * above are the tree nodes sorted by increasing frequency.
+ * OUT assertions: the field len is set to the optimal bit length, the
+ * array bl_count contains the frequencies for each bit length.
+ * The length opt_len is updated; static_len is also updated if stree is
+ * not null.
+ */
+local void gen_bitlen(
+ deflate_state *s,
+ tree_desc *desc) /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ int max_code = desc->max_code;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ const intf *extra = desc->stat_desc->extra_bits;
+ int base = desc->stat_desc->extra_base;
+ int max_length = desc->stat_desc->max_length;
+ int h; /* heap index */
+ int n, m; /* iterate over the tree elements */
+ int bits; /* bit length */
+ int xbits; /* extra bits */
+ ush f; /* frequency */
+ int overflow = 0; /* number of elements with bit length too large */
+
+ for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
+
+ /* In a first pass, compute the optimal bit lengths (which may
+ * overflow in the case of the bit length tree).
+ */
+ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
+
+ for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
+ n = s->heap[h];
+ bits = tree[tree[n].Dad].Len + 1;
+ if (bits > max_length) bits = max_length, overflow++;
+ tree[n].Len = (ush)bits;
+ /* We overwrite tree[n].Dad which is no longer needed */
+
+ if (n > max_code) continue; /* not a leaf node */
+
+ s->bl_count[bits]++;
+ xbits = 0;
+ if (n >= base) xbits = extra[n-base];
+ f = tree[n].Freq;
+ s->opt_len += (ulg)f * (bits + xbits);
+ if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
+ }
+ if (overflow == 0) return;
+
+ Trace((stderr,"\nbit length overflow\n"));
+ /* This happens for example on obj2 and pic of the Calgary corpus */
+
+ /* Find the first bit length which could increase: */
+ do {
+ bits = max_length-1;
+ while (s->bl_count[bits] == 0) bits--;
+ s->bl_count[bits]--; /* move one leaf down the tree */
+ s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
+ s->bl_count[max_length]--;
+ /* The brother of the overflow item also moves one step up,
+ * but this does not affect bl_count[max_length]
+ */
+ overflow -= 2;
+ } while (overflow > 0);
+
+ /* Now recompute all bit lengths, scanning in increasing frequency.
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
+ * lengths instead of fixing only the wrong ones. This idea is taken
+ * from 'ar' written by Haruhiko Okumura.)
+ */
+ for (bits = max_length; bits != 0; bits--) {
+ n = s->bl_count[bits];
+ while (n != 0) {
+ m = s->heap[--h];
+ if (m > max_code) continue;
+ if ((unsigned) tree[m].Len != (unsigned) bits) {
+ Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
+ s->opt_len += ((long)bits - (long)tree[m].Len)
+ *(long)tree[m].Freq;
+ tree[m].Len = (ush)bits;
+ }
+ n--;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Generate the codes for a given tree and bit counts (which need not be
+ * optimal).
+ * IN assertion: the array bl_count contains the bit length statistics for
+ * the given tree and the field len is set for all tree elements.
+ * OUT assertion: the field code is set for all tree elements of non
+ * zero code length.
+ */
+local void gen_codes (
+ ct_data *tree, /* the tree to decorate */
+ int max_code, /* largest code with non zero frequency */
+ ushf *bl_count) /* number of codes at each bit length */
+{
+ ush next_code[MAX_BITS+1]; /* next code value for each bit length */
+ ush code = 0; /* running code value */
+ int bits; /* bit index */
+ int n; /* code index */
+
+ /* The distribution counts are first used to generate the code values
+ * without bit reversal.
+ */
+ for (bits = 1; bits <= MAX_BITS; bits++) {
+ next_code[bits] = code = (code + bl_count[bits-1]) << 1;
+ }
+ /* Check that the bit counts in bl_count are consistent. The last code
+ * must be all ones.
+ */
+ Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
+ "inconsistent bit counts");
+ Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
+
+ for (n = 0; n <= max_code; n++) {
+ int len = tree[n].Len;
+ if (len == 0) continue;
+ /* Now reverse the bits */
+ tree[n].Code = bi_reverse(next_code[len]++, len);
+
+ Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
+ n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
+ }
+}
+
+/* ===========================================================================
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
+ * Update the total bit length for the current block.
+ * IN assertion: the field freq is set for all tree elements.
+ * OUT assertions: the fields len and code are set to the optimal bit length
+ * and corresponding code. The length opt_len is updated; static_len is
+ * also updated if stree is not null. The field max_code is set.
+ */
+local void build_tree(
+ deflate_state *s,
+ tree_desc *desc) /* the tree descriptor */
+{
+ ct_data *tree = desc->dyn_tree;
+ const ct_data *stree = desc->stat_desc->static_tree;
+ int elems = desc->stat_desc->elems;
+ int n, m; /* iterate over heap elements */
+ int max_code = -1; /* largest code with non zero frequency */
+ int node; /* new node being created */
+
+ /* Construct the initial heap, with least frequent element in
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
+ * heap[0] is not used.
+ */
+ s->heap_len = 0, s->heap_max = HEAP_SIZE;
+
+ for (n = 0; n < elems; n++) {
+ if (tree[n].Freq != 0) {
+ s->heap[++(s->heap_len)] = max_code = n;
+ s->depth[n] = 0;
+ } else {
+ tree[n].Len = 0;
+ }
+ }
+
+ /* The pkzip format requires that at least one distance code exists,
+ * and that at least one bit should be sent even if there is only one
+ * possible code. So to avoid special checks later on we force at least
+ * two codes of non zero frequency.
+ */
+ while (s->heap_len < 2) {
+ node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
+ tree[node].Freq = 1;
+ s->depth[node] = 0;
+ s->opt_len--; if (stree) s->static_len -= stree[node].Len;
+ /* node is 0 or 1 so it does not have extra bits */
+ }
+ desc->max_code = max_code;
+
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
+ * establish sub-heaps of increasing lengths:
+ */
+ for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
+
+ /* Construct the Huffman tree by repeatedly combining the least two
+ * frequent nodes.
+ */
+ node = elems; /* next internal node of the tree */
+ do {
+ pqremove(s, tree, n); /* n = node of least frequency */
+ m = s->heap[SMALLEST]; /* m = node of next least frequency */
+
+ s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
+ s->heap[--(s->heap_max)] = m;
+
+ /* Create a new node father of n and m */
+ tree[node].Freq = tree[n].Freq + tree[m].Freq;
+ s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
+ s->depth[n] : s->depth[m]) + 1);
+ tree[n].Dad = tree[m].Dad = (ush)node;
+#ifdef DUMP_BL_TREE
+ if (tree == s->bl_tree) {
+ fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
+ node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
+ }
+#endif
+ /* and insert the new node in the heap */
+ s->heap[SMALLEST] = node++;
+ pqdownheap(s, tree, SMALLEST);
+
+ } while (s->heap_len >= 2);
+
+ s->heap[--(s->heap_max)] = s->heap[SMALLEST];
+
+ /* At this point, the fields freq and dad are set. We can now
+ * generate the bit lengths.
+ */
+ gen_bitlen(s, (tree_desc *)desc);
+
+ /* The field len is now set, we can generate the bit codes */
+ gen_codes ((ct_data *)tree, max_code, s->bl_count);
+}
+
+/* ===========================================================================
+ * Scan a literal or distance tree to determine the frequencies of the codes
+ * in the bit length tree.
+ */
+local void scan_tree (
+ deflate_state *s,
+ ct_data *tree, /* the tree to be scanned */
+ int max_code) /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ if (nextlen == 0) max_count = 138, min_count = 3;
+ tree[max_code+1].Len = (ush)0xffff; /* guard */
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ s->bl_tree[curlen].Freq += count;
+ } else if (curlen != 0) {
+ if (curlen != prevlen) s->bl_tree[curlen].Freq++;
+ s->bl_tree[REP_3_6].Freq++;
+ } else if (count <= 10) {
+ s->bl_tree[REPZ_3_10].Freq++;
+ } else {
+ s->bl_tree[REPZ_11_138].Freq++;
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Send a literal or distance tree in compressed form, using the codes in
+ * bl_tree.
+ */
+local void send_tree (
+ deflate_state *s,
+ ct_data *tree, /* the tree to be scanned */
+ int max_code) /* and its largest code of non zero frequency */
+{
+ int n; /* iterates over all tree elements */
+ int prevlen = -1; /* last emitted length */
+ int curlen; /* length of current code */
+ int nextlen = tree[0].Len; /* length of next code */
+ int count = 0; /* repeat count of the current code */
+ int max_count = 7; /* max repeat count */
+ int min_count = 4; /* min repeat count */
+
+ /* tree[max_code+1].Len = -1; */ /* guard already set */
+ if (nextlen == 0) max_count = 138, min_count = 3;
+
+ for (n = 0; n <= max_code; n++) {
+ curlen = nextlen; nextlen = tree[n+1].Len;
+ if (++count < max_count && curlen == nextlen) {
+ continue;
+ } else if (count < min_count) {
+ do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
+
+ } else if (curlen != 0) {
+ if (curlen != prevlen) {
+ send_code(s, curlen, s->bl_tree); count--;
+ }
+ Assert(count >= 3 && count <= 6, " 3_6?");
+ send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
+
+ } else if (count <= 10) {
+ send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
+
+ } else {
+ send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
+ }
+ count = 0; prevlen = curlen;
+ if (nextlen == 0) {
+ max_count = 138, min_count = 3;
+ } else if (curlen == nextlen) {
+ max_count = 6, min_count = 3;
+ } else {
+ max_count = 7, min_count = 4;
+ }
+ }
+}
+
+/* ===========================================================================
+ * Construct the Huffman tree for the bit lengths and return the index in
+ * bl_order of the last bit length code to send.
+ */
+local int build_bl_tree(
+ deflate_state *s)
+{
+ int max_blindex; /* index of last bit length code of non zero freq */
+
+ /* Determine the bit length frequencies for literal and distance trees */
+ scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
+ scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
+
+ /* Build the bit length tree: */
+ build_tree(s, (tree_desc *)(&(s->bl_desc)));
+ /* opt_len now includes the length of the tree representations, except
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
+ */
+
+ /* Determine the number of bit length codes to send. The pkzip format
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
+ * 3 but the actual value used is 4.)
+ */
+ for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
+ if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
+ }
+ /* Update opt_len to include the bit length tree and counts */
+ s->opt_len += 3*(max_blindex+1) + 5+5+4;
+ Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
+ s->opt_len, s->static_len));
+
+ return max_blindex;
+}
+
+/* ===========================================================================
+ * Send the header for a block using dynamic Huffman trees: the counts, the
+ * lengths of the bit length codes, the literal tree and the distance tree.
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
+ */
+local void send_all_trees(
+ deflate_state *s,
+ int lcodes, int dcodes, int blcodes) /* number of codes for each tree */
+{
+ int rank; /* index in bl_order */
+
+ Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
+ Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
+ "too many codes");
+ Tracev((stderr, "\nbl counts: "));
+ send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
+ send_bits(s, dcodes-1, 5);
+ send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
+ for (rank = 0; rank < blcodes; rank++) {
+ Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
+ send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
+ }
+ Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
+ Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
+
+ send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
+ Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
+}
+
+/* ===========================================================================
+ * Send a stored block
+ */
+void ZLIB_INTERNAL _tr_stored_block(
+ deflate_state *s,
+ charf *buf, /* input block */
+ ulg stored_len, /* length of input block */
+ int last) /* one if this is the last block for a file */
+{
+ send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
+#ifdef DEBUG
+ s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
+ s->compressed_len += (stored_len + 4) << 3;
+#endif
+ copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
+}
+
+/* ===========================================================================
+ * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
+ */
+void ZLIB_INTERNAL _tr_flush_bits(
+ deflate_state *s)
+{
+ bi_flush(s);
+}
+
+/* ===========================================================================
+ * Send one empty static block to give enough lookahead for inflate.
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
+ */
+void ZLIB_INTERNAL _tr_align(
+ deflate_state *s)
+{
+ send_bits(s, STATIC_TREES<<1, 3);
+ send_code(s, END_BLOCK, static_ltree);
+#ifdef DEBUG
+ s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
+#endif
+ bi_flush(s);
+}
+
+/* ===========================================================================
+ * Determine the best encoding for the current block: dynamic trees, static
+ * trees or store, and output the encoded block to the zip file.
+ */
+void ZLIB_INTERNAL _tr_flush_block(
+ deflate_state *s,
+ charf *buf, /* input block, or NULL if too old */
+ ulg stored_len, /* length of input block */
+ int last) /* one if this is the last block for a file */
+{
+ ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
+ int max_blindex = 0; /* index of last bit length code of non zero freq */
+
+ /* Build the Huffman trees unless a stored block is forced */
+ if (s->level > 0) {
+
+ /* Check if the file is binary or text */
+ if (s->strm->data_type == Z_UNKNOWN)
+ s->strm->data_type = detect_data_type(s);
+
+ /* Construct the literal and distance trees */
+ build_tree(s, (tree_desc *)(&(s->l_desc)));
+ Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+
+ build_tree(s, (tree_desc *)(&(s->d_desc)));
+ Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
+ s->static_len));
+ /* At this point, opt_len and static_len are the total bit lengths of
+ * the compressed block data, excluding the tree representations.
+ */
+
+ /* Build the bit length tree for the above two trees, and get the index
+ * in bl_order of the last bit length code to send.
+ */
+ max_blindex = build_bl_tree(s);
+
+ /* Determine the best encoding. Compute the block lengths in bytes. */
+ opt_lenb = (s->opt_len+3+7)>>3;
+ static_lenb = (s->static_len+3+7)>>3;
+
+ Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
+ opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
+ s->last_lit));
+
+ if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
+
+ } else {
+ Assert(buf != (char*)0, "lost buf");
+ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
+ }
+
+#ifdef FORCE_STORED
+ if (buf != (char*)0) { /* force stored block */
+#else
+ if (stored_len+4 <= opt_lenb && buf != (char*)0) {
+ /* 4: two words for the lengths */
+#endif
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
+ * Otherwise we can't have processed more than WSIZE input bytes since
+ * the last block flush, because compression would have been
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
+ * transform a block into a stored block.
+ */
+ _tr_stored_block(s, buf, stored_len, last);
+
+#ifdef FORCE_STATIC
+ } else if (static_lenb >= 0) { /* force static trees */
+#else
+ } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
+#endif
+ send_bits(s, (STATIC_TREES<<1)+last, 3);
+ compress_block(s, (const ct_data *)static_ltree,
+ (const ct_data *)static_dtree);
+#ifdef DEBUG
+ s->compressed_len += 3 + s->static_len;
+#endif
+ } else {
+ send_bits(s, (DYN_TREES<<1)+last, 3);
+ send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
+ max_blindex+1);
+ compress_block(s, (const ct_data *)s->dyn_ltree,
+ (const ct_data *)s->dyn_dtree);
+#ifdef DEBUG
+ s->compressed_len += 3 + s->opt_len;
+#endif
+ }
+ Assert (s->compressed_len == s->bits_sent, "bad compressed size");
+ /* The above check is made mod 2^32, for files larger than 512 MB
+ * and uLong implemented on 32 bits.
+ */
+ init_block(s);
+
+ if (last) {
+ bi_windup(s);
+#ifdef DEBUG
+ s->compressed_len += 7; /* align on byte boundary */
+#endif
+ }
+ Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
+ s->compressed_len-7*last));
+}
+
+/* ===========================================================================
+ * Save the match info and tally the frequency counts. Return true if
+ * the current block must be flushed.
+ */
+int ZLIB_INTERNAL _tr_tally (
+ deflate_state *s,
+ unsigned dist, /* distance of matched string */
+ unsigned lc) /* match length-MIN_MATCH or unmatched char (if dist==0) */
+{
+ s->d_buf[s->last_lit] = (ush)dist;
+ s->l_buf[s->last_lit++] = (uch)lc;
+ if (dist == 0) {
+ /* lc is the unmatched char */
+ s->dyn_ltree[lc].Freq++;
+ } else {
+ s->matches++;
+ /* Here, lc is the match length - MIN_MATCH */
+ dist--; /* dist = match distance - 1 */
+ Assert((ush)dist < (ush)MAX_DIST(s) &&
+ (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
+ (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
+
+ s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
+ s->dyn_dtree[d_code(dist)].Freq++;
+ }
+
+#ifdef TRUNCATE_BLOCK
+ /* Try to guess if it is profitable to stop the current block here */
+ if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
+ /* Compute an upper bound for the compressed length */
+ ulg out_length = (ulg)s->last_lit*8L;
+ ulg in_length = (ulg)((long)s->strstart - s->block_start);
+ int dcode;
+ for (dcode = 0; dcode < D_CODES; dcode++) {
+ out_length += (ulg)s->dyn_dtree[dcode].Freq *
+ (5L+extra_dbits[dcode]);
+ }
+ out_length >>= 3;
+ Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
+ s->last_lit, in_length, out_length,
+ 100L - out_length*100L/in_length));
+ if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
+ }
+#endif
+ return (s->last_lit == s->lit_bufsize-1);
+ /* We avoid equality with lit_bufsize because of wraparound at 64K
+ * on 16 bit machines and because stored blocks are restricted to
+ * 64K-1 bytes.
+ */
+}
+
+/* ===========================================================================
+ * Send the block data compressed using the given Huffman trees
+ */
+local void compress_block(
+ deflate_state *s,
+ const ct_data *ltree, /* literal tree */
+ const ct_data *dtree) /* distance tree */
+{
+ unsigned dist; /* distance of matched string */
+ int lc; /* match length or unmatched char (if dist == 0) */
+ unsigned lx = 0; /* running index in l_buf */
+ unsigned code; /* the code to send */
+ int extra; /* number of extra bits to send */
+
+ if (s->last_lit != 0) do {
+ dist = s->d_buf[lx];
+ lc = s->l_buf[lx++];
+ if (dist == 0) {
+ send_code(s, lc, ltree); /* send a literal byte */
+ Tracecv(isgraph(lc), (stderr," '%c' ", lc));
+ } else {
+ /* Here, lc is the match length - MIN_MATCH */
+ code = _length_code[lc];
+ send_code(s, code+LITERALS+1, ltree); /* send the length code */
+ extra = extra_lbits[code];
+ if (extra != 0) {
+ lc -= base_length[code];
+ send_bits(s, lc, extra); /* send the extra length bits */
+ }
+ dist--; /* dist is now the match distance - 1 */
+ code = d_code(dist);
+ Assert (code < D_CODES, "bad d_code");
+
+ send_code(s, code, dtree); /* send the distance code */
+ extra = extra_dbits[code];
+ if (extra != 0) {
+ dist -= base_dist[code];
+ send_bits(s, dist, extra); /* send the extra distance bits */
+ }
+ } /* literal or match pair ? */
+
+ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
+ Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
+ "pendingBuf overflow");
+
+ } while (lx < s->last_lit);
+
+ send_code(s, END_BLOCK, ltree);
+}
+
+/* ===========================================================================
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
+ * - TEXT if the two conditions below are satisfied:
+ * a) There are no non-portable control characters belonging to the
+ * "black list" (0..6, 14..25, 28..31).
+ * b) There is at least one printable character belonging to the
+ * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
+ * - BINARY otherwise.
+ * - The following partially-portable control characters form a
+ * "gray list" that is ignored in this detection algorithm:
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
+ * IN assertion: the fields Freq of dyn_ltree are set.
+ */
+local int detect_data_type(
+ deflate_state *s)
+{
+ /* black_mask is the bit mask of black-listed bytes
+ * set bits 0..6, 14..25, and 28..31
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
+ */
+ unsigned long black_mask = 0xf3ffc07fUL;
+ int n;
+
+ /* Check for non-textual ("black-listed") bytes. */
+ for (n = 0; n <= 31; n++, black_mask >>= 1)
+ if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
+ return Z_BINARY;
+
+ /* Check for textual ("white-listed") bytes. */
+ if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
+ || s->dyn_ltree[13].Freq != 0)
+ return Z_TEXT;
+ for (n = 32; n < LITERALS; n++)
+ if (s->dyn_ltree[n].Freq != 0)
+ return Z_TEXT;
+
+ /* There are no "black-listed" or "white-listed" bytes:
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
+ */
+ return Z_BINARY;
+}
+
+/* ===========================================================================
+ * Reverse the first len bits of a code, using straightforward code (a faster
+ * method would use a table)
+ * IN assertion: 1 <= len <= 15
+ */
+local unsigned bi_reverse(
+ unsigned code, /* the value to invert */
+ int len) /* its bit length */
+{
+ register unsigned res = 0;
+ do {
+ res |= code & 1;
+ code >>= 1, res <<= 1;
+ } while (--len > 0);
+ return res >> 1;
+}
+
+/* ===========================================================================
+ * Flush the bit buffer, keeping at most 7 bits in it.
+ */
+local void bi_flush(
+ deflate_state *s)
+{
+ if (s->bi_valid == 16) {
+ put_short(s, s->bi_buf);
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+ } else if (s->bi_valid >= 8) {
+ put_byte(s, (Byte)s->bi_buf);
+ s->bi_buf >>= 8;
+ s->bi_valid -= 8;
+ }
+}
+
+/* ===========================================================================
+ * Flush the bit buffer and align the output on a byte boundary
+ */
+local void bi_windup(
+ deflate_state *s)
+{
+ if (s->bi_valid > 8) {
+ put_short(s, s->bi_buf);
+ } else if (s->bi_valid > 0) {
+ put_byte(s, (Byte)s->bi_buf);
+ }
+ s->bi_buf = 0;
+ s->bi_valid = 0;
+#ifdef DEBUG
+ s->bits_sent = (s->bits_sent+7) & ~7;
+#endif
+}
+
+/* ===========================================================================
+ * Copy a stored block, storing first the length and its
+ * one's complement if requested.
+ */
+local void copy_block(
+ deflate_state *s,
+ charf *buf, /* the input data */
+ unsigned len, /* its length */
+ int header) /* true if block header must be written */
+{
+ bi_windup(s); /* align on byte boundary */
+
+ if (header) {
+ put_short(s, (ush)len);
+ put_short(s, (ush)~len);
+#ifdef DEBUG
+ s->bits_sent += 2*16;
+#endif
+ }
+#ifdef DEBUG
+ s->bits_sent += (ulg)len<<3;
+#endif
+ while (len--) {
+ put_byte(s, *buf++);
+ }
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_uncompr.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_uncompr.c
new file mode 100644
index 0000000000..d3cb8fe183
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_uncompr.c
@@ -0,0 +1,59 @@
+/* uncompr.c -- decompress a memory buffer
+ * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Decompresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total
+ size of the destination buffer, which must be large enough to hold the
+ entire uncompressed data. (The size of the uncompressed data must have
+ been saved previously by the compressor and transmitted to the decompressor
+ by some mechanism outside the scope of this compression library.)
+ Upon exit, destLen is the actual size of the compressed buffer.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer, or Z_DATA_ERROR if the input data was corrupted.
+*/
+int ZEXPORT uncompress (
+ Bytef *dest,
+ uLongf *destLen,
+ const Bytef *source,
+ uLong sourceLen)
+{
+ z_stream stream;
+ int err;
+
+ stream.next_in = (z_const Bytef *)source;
+ stream.avail_in = (uInt)sourceLen;
+ /* Check for source > 64K on 16-bit machine: */
+ if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+
+ stream.next_out = dest;
+ stream.avail_out = (uInt)*destLen;
+ if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+
+ err = inflateInit(&stream);
+ if (err != Z_OK) return err;
+
+ err = inflate(&stream, Z_FINISH);
+ if (err != Z_STREAM_END) {
+ inflateEnd(&stream);
+ if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
+ return Z_DATA_ERROR;
+ return err;
+ }
+ *destLen = stream.total_out;
+
+ err = inflateEnd(&stream);
+ return err;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_zutil.c b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_zutil.c
new file mode 100644
index 0000000000..02b945f7d8
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/fx_zlib_zutil.c
@@ -0,0 +1,349 @@
+/* zutil.c -- target dependent utility functions for the compression library
+ * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#include "zutil.h"
+#ifndef Z_SOLO
+# include "gzguts.h"
+#endif
+
+#ifndef NO_DUMMY_DECL
+struct internal_state {int dummy;}; /* for buggy compilers */
+#endif
+
+z_const char * const z_errmsg[10] = {
+"need dictionary", /* Z_NEED_DICT 2 */
+"stream end", /* Z_STREAM_END 1 */
+"", /* Z_OK 0 */
+"file error", /* Z_ERRNO (-1) */
+"stream error", /* Z_STREAM_ERROR (-2) */
+"data error", /* Z_DATA_ERROR (-3) */
+"insufficient memory", /* Z_MEM_ERROR (-4) */
+"buffer error", /* Z_BUF_ERROR (-5) */
+"incompatible version",/* Z_VERSION_ERROR (-6) */
+""};
+
+
+const char * ZEXPORT zlibVersion()
+{
+ return ZLIB_VERSION;
+}
+
+uLong ZEXPORT zlibCompileFlags()
+{
+ uLong flags;
+
+ flags = 0;
+ switch ((int)(sizeof(uInt))) {
+ case 2: break;
+ case 4: flags += 1; break;
+ case 8: flags += 2; break;
+ default: flags += 3;
+ }
+ switch ((int)(sizeof(uLong))) {
+ case 2: break;
+ case 4: flags += 1 << 2; break;
+ case 8: flags += 2 << 2; break;
+ default: flags += 3 << 2;
+ }
+ switch ((int)(sizeof(voidpf))) {
+ case 2: break;
+ case 4: flags += 1 << 4; break;
+ case 8: flags += 2 << 4; break;
+ default: flags += 3 << 4;
+ }
+ switch ((int)(sizeof(z_off_t))) {
+ case 2: break;
+ case 4: flags += 1 << 6; break;
+ case 8: flags += 2 << 6; break;
+ default: flags += 3 << 6;
+ }
+#ifdef DEBUG
+ flags += 1 << 8;
+#endif
+#if defined(ASMV) || defined(ASMINF)
+ flags += 1 << 9;
+#endif
+#ifdef ZLIB_WINAPI
+ flags += 1 << 10;
+#endif
+#ifdef BUILDFIXED
+ flags += 1 << 12;
+#endif
+#ifdef DYNAMIC_CRC_TABLE
+ flags += 1 << 13;
+#endif
+#ifdef NO_GZCOMPRESS
+ flags += 1L << 16;
+#endif
+#ifdef NO_GZIP
+ flags += 1L << 17;
+#endif
+#ifdef PKZIP_BUG_WORKAROUND
+ flags += 1L << 20;
+#endif
+#ifdef FASTEST
+ flags += 1L << 21;
+#endif
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifdef NO_vsnprintf
+ flags += 1L << 25;
+# ifdef HAS_vsprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_vsnprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#else
+ flags += 1L << 24;
+# ifdef NO_snprintf
+ flags += 1L << 25;
+# ifdef HAS_sprintf_void
+ flags += 1L << 26;
+# endif
+# else
+# ifdef HAS_snprintf_void
+ flags += 1L << 26;
+# endif
+# endif
+#endif
+ return flags;
+}
+
+#ifdef DEBUG
+
+# ifndef verbose
+# define verbose 0
+# endif
+int ZLIB_INTERNAL z_verbose = verbose;
+
+void ZLIB_INTERNAL z_error (
+ char *m)
+{
+ fprintf(stderr, "%s\n", m);
+ exit(1);
+}
+#endif
+
+/* exported to allow conversion of error code to string for compress() and
+ * uncompress()
+ */
+const char * ZEXPORT zError(
+ int err)
+{
+ return ERR_MSG(err);
+}
+
+#if defined(_WIN32_WCE)
+ /* The Microsoft C Run-Time Library for Windows CE doesn't have
+ * errno. We define it as a global variable to simplify porting.
+ * Its value is always 0 and should not be used.
+ */
+ /* does not exist on WCE. XYQ: and we don't need it! */
+ /*int errno = 0;*/
+#endif
+
+#ifndef HAVE_MEMCPY
+
+void ZLIB_INTERNAL zmemcpy(
+ Bytef* dest,
+ const Bytef* source,
+ uInt len)
+{
+ if (len == 0) return;
+ do {
+ *dest++ = *source++; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+
+int ZLIB_INTERNAL zmemcmp(
+ const Bytef* s1,
+ const Bytef* s2,
+ uInt len)
+{
+ uInt j;
+
+ for (j = 0; j < len; j++) {
+ if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
+ }
+ return 0;
+}
+
+void ZLIB_INTERNAL zmemzero(
+ Bytef* dest,
+ uInt len)
+{
+ if (len == 0) return;
+ do {
+ *dest++ = 0; /* ??? to be unrolled */
+ } while (--len != 0);
+}
+#endif
+
+#if 0
+#ifndef Z_SOLO
+
+#ifdef SYS16BIT
+
+#ifdef __TURBOC__
+/* Turbo C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
+ * and farmalloc(64K) returns a pointer with an offset of 8, so we
+ * must fix the pointer. Warning: the pointer must be put back to its
+ * original form in order to free it, use zcfree().
+ */
+
+#define MAX_PTR 10
+/* 10*64K = 640K */
+
+local int next_ptr = 0;
+
+typedef struct ptr_table_s {
+ voidpf org_ptr;
+ voidpf new_ptr;
+} ptr_table;
+
+local ptr_table table[MAX_PTR];
+/* This table is used to remember the original form of pointers
+ * to large buffers (64K). Such pointers are normalized with a zero offset.
+ * Since MSDOS is not a preemptive multitasking OS, this table is not
+ * protected from concurrent access. This hack doesn't work anyway on
+ * a protected system like OS/2. Use Microsoft C instead.
+ */
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
+{
+ voidpf buf = opaque; /* just to make some compilers happy */
+ ulg bsize = (ulg)items*size;
+
+ /* If we allocate less than 65520 bytes, we assume that farmalloc
+ * will return a usable pointer which doesn't have to be normalized.
+ */
+ if (bsize < 65520L) {
+ buf = farmalloc(bsize);
+ if (*(ush*)&buf != 0) return buf;
+ } else {
+ buf = farmalloc(bsize + 16L);
+ }
+ if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
+ table[next_ptr].org_ptr = buf;
+
+ /* Normalize the pointer to seg:0 */
+ *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
+ *(ush*)&buf = 0;
+ table[next_ptr++].new_ptr = buf;
+ return buf;
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ int n;
+ if (*(ush*)&ptr != 0) { /* object < 64K */
+ farfree(ptr);
+ return;
+ }
+ /* Find the original pointer */
+ for (n = 0; n < next_ptr; n++) {
+ if (ptr != table[n].new_ptr) continue;
+
+ farfree(table[n].org_ptr);
+ while (++n < next_ptr) {
+ table[n-1] = table[n];
+ }
+ next_ptr--;
+ return;
+ }
+ ptr = opaque; /* just to make some compilers happy */
+ Assert(0, "zcfree: ptr not found");
+}
+
+#endif /* __TURBOC__ */
+
+
+#ifdef M_I86
+/* Microsoft C in 16-bit mode */
+
+# define MY_ZCALLOC
+
+#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
+# define _halloc halloc
+# define _hfree hfree
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
+{
+ if (opaque) opaque = 0; /* to make compiler happy */
+ return _halloc((long)items, size);
+}
+
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
+{
+ if (opaque) opaque = 0; /* to make compiler happy */
+ _hfree(ptr);
+}
+
+#endif /* M_I86 */
+
+#endif /* SYS16BIT */
+
+
+#ifndef MY_ZCALLOC /* Any system without a special alloc function */
+
+#ifndef STDC
+extern voidp malloc OF((uInt size));
+extern voidp calloc OF((uInt items, uInt size));
+extern void free OF((voidpf ptr));
+#endif
+
+voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
+ voidpf opaque;
+ unsigned items;
+ unsigned size;
+{
+ if (opaque) items += size - size; /* make compiler happy */
+ return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
+ (voidpf)calloc(items, size);
+}
+
+void ZLIB_INTERNAL zcfree (opaque, ptr)
+ voidpf opaque;
+ voidpf ptr;
+{
+ free(ptr);
+ if (opaque) return; /* make compiler happy */
+}
+
+#endif /* MY_ZCALLOC */
+
+#endif /* !Z_SOLO */
+#endif /* 0 */
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+extern "C" {
+#endif
+
+extern void* FXMEM_DefaultAlloc(size_t, int);
+extern void FXMEM_DefaultFree(void*, int);
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+}
+#endif
+
+/* XYQ 2007-1-19 */
+voidpf zcalloc(voidpf opaque, unsigned items, unsigned size)
+{
+ return FXMEM_DefaultAlloc(items * size, 0);
+}
+
+void zcfree(voidpf opaque, voidpf ptr)
+{
+ FXMEM_DefaultFree(ptr, 0);
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/gzclose.c b/core/src/fxcodec/fx_zlib/zlib_v128/gzclose.c
new file mode 100644
index 0000000000..0f010b84c8
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/gzclose.c
@@ -0,0 +1,25 @@
+/* gzclose.c -- zlib gzclose() function
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* gzclose() is in a separate file so that it is linked in only if it is used.
+ That way the other gzclose functions can be used instead to avoid linking in
+ unneeded compression or decompression routines. */
+int ZEXPORT gzclose(
+ gzFile file)
+{
+#ifndef NO_GZCOMPRESS
+ gz_statep state;
+
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
+#else
+ return gzclose_r(file);
+#endif
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/gzguts.h b/core/src/fxcodec/fx_zlib/zlib_v128/gzguts.h
new file mode 100644
index 0000000000..e4454aa65d
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/gzguts.h
@@ -0,0 +1,227 @@
+/* gzguts.h -- zlib internal header definitions for gz* operations
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#ifdef _LARGEFILE64_SOURCE
+# ifndef _LARGEFILE_SOURCE
+# define _LARGEFILE_SOURCE 1
+# endif
+# ifdef _FILE_OFFSET_BITS
+# undef _FILE_OFFSET_BITS
+# endif
+#endif
+
+#ifdef HAVE_HIDDEN
+# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+# define ZLIB_INTERNAL
+#endif
+
+#include <stdio.h>
+#include "zlib.h"
+#ifdef STDC
+# include <string.h>
+# include <stdlib.h>
+# include <limits.h>
+#endif
+#include <fcntl.h>
+
+#ifdef _WIN32
+# include <stddef.h>
+#endif
+
+#ifdef __APPLE__
+#include <unistd.h>
+#endif
+
+#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32)
+# include <io.h>
+#endif
+
+#ifdef WINAPI_FAMILY
+# define open _open
+# define read _read
+# define write _write
+# define close _close
+#endif
+
+#ifdef NO_DEFLATE /* for compatibility with old definition */
+# define NO_GZCOMPRESS
+#endif
+
+#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#if defined(__CYGWIN__)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
+# ifndef HAVE_VSNPRINTF
+# define HAVE_VSNPRINTF
+# endif
+#endif
+
+#ifndef HAVE_VSNPRINTF
+# ifdef MSDOS
+/* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
+ but for now we just assume it doesn't. */
+# define NO_vsnprintf
+# endif
+# ifdef __TURBOC__
+# define NO_vsnprintf
+# endif
+# ifdef WIN32
+/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
+# if !defined(vsnprintf) && !defined(NO_vsnprintf)
+# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
+# define vsnprintf _vsnprintf
+# endif
+# endif
+# endif
+# ifdef __SASC
+# define NO_vsnprintf
+# endif
+# ifdef VMS
+# define NO_vsnprintf
+# endif
+# ifdef __OS400__
+# define NO_vsnprintf
+# endif
+# ifdef __MVS__
+# define NO_vsnprintf
+# endif
+#endif
+
+/* unlike snprintf (which is required in C99, yet still not supported by
+ Microsoft more than a decade later!), _snprintf does not guarantee null
+ termination of the result -- however this is only used in gzlib.c where
+ the result is assured to fit in the space provided */
+#ifdef _MSC_VER
+# define snprintf _snprintf
+#endif
+
+#ifndef local
+# define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+/* gz* functions always use library allocation functions */
+#ifndef STDC
+ extern voidp malloc OF((uInt size));
+ extern void free OF((voidpf ptr));
+#endif
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+extern "C" {
+#endif
+
+extern void* FXMEM_DefaultAlloc(size_t, int);
+extern void FXMEM_DefaultFree(void*, int);
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+}
+#endif
+
+#define malloc(size) FXMEM_DefaultAlloc(size, 0)
+#define free(ptr) FXMEM_DefaultFree(ptr, 0)
+
+/* get errno and strerror definition */
+#if defined UNDER_CE
+# include <windows.h>
+# define zstrerror() gz_strwinerror((DWORD)GetLastError())
+#else
+# ifndef NO_STRERROR
+# include <errno.h>
+# define zstrerror() strerror(errno)
+# else
+# define zstrerror() "stdio error (consult errno)"
+# endif
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+#endif
+
+/* default memLevel */
+#if MAX_MEM_LEVEL >= 8
+# define DEF_MEM_LEVEL 8
+#else
+# define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+
+/* default i/o buffer size -- double this for output when reading (this and
+ twice this must be able to fit in an unsigned type) */
+#define GZBUFSIZE 8192
+
+/* gzip modes, also provide a little integrity check on the passed structure */
+#define GZ_NONE 0
+#define GZ_READ 7247
+#define GZ_WRITE 31153
+#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
+
+/* values for gz_state how */
+#define LOOK 0 /* look for a gzip header */
+#define COPY 1 /* copy input directly */
+#define GZIP 2 /* decompress a gzip stream */
+
+/* internal gzip file state data structure */
+typedef struct {
+ /* exposed contents for gzgetc() macro */
+ struct gzFile_s x; /* "x" for exposed */
+ /* x.have: number of bytes available at x.next */
+ /* x.next: next output data to deliver or write */
+ /* x.pos: current position in uncompressed data */
+ /* used for both reading and writing */
+ int mode; /* see gzip modes above */
+ int fd; /* file descriptor */
+ char *path; /* path or fd for error messages */
+ unsigned size; /* buffer size, zero if not allocated yet */
+ unsigned want; /* requested buffer size, default is GZBUFSIZE */
+ unsigned char *in; /* input buffer */
+ unsigned char *out; /* output buffer (double-sized when reading) */
+ int direct; /* 0 if processing gzip, 1 if transparent */
+ /* just for reading */
+ int how; /* 0: get header, 1: copy, 2: decompress */
+ z_off64_t start; /* where the gzip data started, for rewinding */
+ int eof; /* true if end of input file reached */
+ int past; /* true if read requested past end */
+ /* just for writing */
+ int level; /* compression level */
+ int strategy; /* compression strategy */
+ /* seek request */
+ z_off64_t skip; /* amount to skip (already rewound if backwards) */
+ int seek; /* true if seek request pending */
+ /* error information */
+ int err; /* error code */
+ char *msg; /* error message */
+ /* zlib inflate or deflate stream */
+ z_stream strm; /* stream structure in-place (not a pointer) */
+} gz_state;
+typedef gz_state FAR *gz_statep;
+
+/* shared functions */
+void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
+#if defined UNDER_CE
+char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
+#endif
+
+/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
+ value -- needed when comparing unsigned to z_off64_t, which is signed
+ (possible z_off64_t types off_t, off64_t, and long are all signed) */
+#ifdef INT_MAX
+# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
+#else
+unsigned ZLIB_INTERNAL gz_intmax OF((void));
+# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
+#endif
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/gzlib.c b/core/src/fxcodec/fx_zlib/zlib_v128/gzlib.c
new file mode 100644
index 0000000000..68b176478d
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/gzlib.c
@@ -0,0 +1,634 @@
+/* gzlib.c -- zlib functions common to reading and writing gzip files
+ * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+#if defined(_WIN32) && !defined(__BORLANDC__)
+# define LSEEK _lseeki64
+#else
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+# define LSEEK lseek64
+#else
+# define LSEEK lseek
+#endif
+#endif
+
+/* Local functions */
+local void gz_reset OF((gz_statep));
+local gzFile gz_open OF((const void *, int, const char *));
+
+#if defined UNDER_CE
+
+/* Map the Windows error number in ERROR to a locale-dependent error message
+ string and return a pointer to it. Typically, the values for ERROR come
+ from GetLastError.
+
+ The string pointed to shall not be modified by the application, but may be
+ overwritten by a subsequent call to gz_strwinerror
+
+ The gz_strwinerror function does not change the current setting of
+ GetLastError. */
+char ZLIB_INTERNAL *gz_strwinerror (
+ DWORD error)
+{
+ static char buf[1024];
+
+ wchar_t *msgbuf;
+ DWORD lasterr = GetLastError();
+ DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL,
+ error,
+ 0, /* Default language */
+ (LPVOID)&msgbuf,
+ 0,
+ NULL);
+ if (chars != 0) {
+ /* If there is an \r\n appended, zap it. */
+ if (chars >= 2
+ && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+ chars -= 2;
+ msgbuf[chars] = 0;
+ }
+
+ if (chars > sizeof (buf) - 1) {
+ chars = sizeof (buf) - 1;
+ msgbuf[chars] = 0;
+ }
+
+ wcstombs(buf, msgbuf, chars + 1);
+ LocalFree(msgbuf);
+ }
+ else {
+ sprintf(buf, "unknown win32 error (%ld)", error);
+ }
+
+ SetLastError(lasterr);
+ return buf;
+}
+
+#endif /* UNDER_CE */
+
+/* Reset gzip file state */
+local void gz_reset(
+ gz_statep state)
+{
+ state->x.have = 0; /* no output data available */
+ if (state->mode == GZ_READ) { /* for reading ... */
+ state->eof = 0; /* not at end of file */
+ state->past = 0; /* have not read past end yet */
+ state->how = LOOK; /* look for gzip header */
+ }
+ state->seek = 0; /* no seek request pending */
+ gz_error(state, Z_OK, NULL); /* clear error */
+ state->x.pos = 0; /* no uncompressed data yet */
+ state->strm.avail_in = 0; /* no input data yet */
+}
+
+/* Open a gzip file either by name or file descriptor. */
+local gzFile gz_open(
+ const void *path,
+ int fd,
+ const char *mode)
+{
+ gz_statep state;
+ size_t len;
+ int oflag;
+#ifdef O_CLOEXEC
+ int cloexec = 0;
+#endif
+#ifdef O_EXCL
+ int exclusive = 0;
+#endif
+
+ /* check input */
+ if (path == NULL)
+ return NULL;
+
+ /* allocate gzFile structure to return */
+ state = (gz_statep)malloc(sizeof(gz_state));
+ if (state == NULL)
+ return NULL;
+ state->size = 0; /* no buffers allocated yet */
+ state->want = GZBUFSIZE; /* requested buffer size */
+ state->msg = NULL; /* no error message yet */
+
+ /* interpret mode */
+ state->mode = GZ_NONE;
+ state->level = Z_DEFAULT_COMPRESSION;
+ state->strategy = Z_DEFAULT_STRATEGY;
+ state->direct = 0;
+ while (*mode) {
+ if (*mode >= '0' && *mode <= '9')
+ state->level = *mode - '0';
+ else
+ switch (*mode) {
+ case 'r':
+ state->mode = GZ_READ;
+ break;
+#ifndef NO_GZCOMPRESS
+ case 'w':
+ state->mode = GZ_WRITE;
+ break;
+ case 'a':
+ state->mode = GZ_APPEND;
+ break;
+#endif
+ case '+': /* can't read and write at the same time */
+ free(state);
+ return NULL;
+ case 'b': /* ignore -- will request binary anyway */
+ break;
+#ifdef O_CLOEXEC
+ case 'e':
+ cloexec = 1;
+ break;
+#endif
+#ifdef O_EXCL
+ case 'x':
+ exclusive = 1;
+ break;
+#endif
+ case 'f':
+ state->strategy = Z_FILTERED;
+ break;
+ case 'h':
+ state->strategy = Z_HUFFMAN_ONLY;
+ break;
+ case 'R':
+ state->strategy = Z_RLE;
+ break;
+ case 'F':
+ state->strategy = Z_FIXED;
+ break;
+ case 'T':
+ state->direct = 1;
+ break;
+ default: /* could consider as an error, but just ignore */
+ ;
+ }
+ mode++;
+ }
+
+ /* must provide an "r", "w", or "a" */
+ if (state->mode == GZ_NONE) {
+ free(state);
+ return NULL;
+ }
+
+ /* can't force transparent read */
+ if (state->mode == GZ_READ) {
+ if (state->direct) {
+ free(state);
+ return NULL;
+ }
+ state->direct = 1; /* for empty file */
+ }
+
+ /* save the path name for error messages */
+#ifdef _WIN32
+ if (fd == -2) {
+ len = wcstombs(NULL, path, 0);
+ if (len == (size_t)-1)
+ len = 0;
+ }
+ else
+#endif
+ len = strlen((const char *)path);
+ state->path = (char *)malloc(len + 1);
+ if (state->path == NULL) {
+ free(state);
+ return NULL;
+ }
+#ifdef _WIN32
+ if (fd == -2)
+ if (len)
+ wcstombs(state->path, path, len + 1);
+ else
+ *(state->path) = 0;
+ else
+#endif
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(state->path, len + 1, "%s", (const char *)path);
+#else
+ strcpy(state->path, path);
+#endif
+
+ /* compute the flags for open() */
+ oflag =
+#ifdef O_LARGEFILE
+ O_LARGEFILE |
+#endif
+#ifdef O_BINARY
+ O_BINARY |
+#endif
+#ifdef O_CLOEXEC
+ (cloexec ? O_CLOEXEC : 0) |
+#endif
+ (state->mode == GZ_READ ?
+ O_RDONLY :
+ (O_WRONLY | O_CREAT |
+#ifdef O_EXCL
+ (exclusive ? O_EXCL : 0) |
+#endif
+ (state->mode == GZ_WRITE ?
+ O_TRUNC :
+ O_APPEND)));
+
+ /* open the file with the appropriate flags (or just use fd) */
+ state->fd = fd > -1 ? fd : (
+#ifdef _WIN32
+ fd == -2 ? _wopen(path, oflag, 0666) :
+#endif
+ open((const char *)path, oflag, 0666));
+ if (state->fd == -1) {
+ free(state->path);
+ free(state);
+ return NULL;
+ }
+ if (state->mode == GZ_APPEND)
+ state->mode = GZ_WRITE; /* simplify later checks */
+
+ /* save the current position for rewinding (only if reading) */
+ if (state->mode == GZ_READ) {
+ state->start = LSEEK(state->fd, 0, SEEK_CUR);
+ if (state->start == -1) state->start = 0;
+ }
+
+ /* initialize stream */
+ gz_reset(state);
+
+ /* return stream */
+ return (gzFile)state;
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen(
+ const char *path,
+ const char *mode)
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen64(
+ const char *path,
+ const char *mode)
+{
+ return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzdopen(
+ int fd,
+ const char *mode)
+{
+ char *path; /* identifier for error messages */
+ gzFile gz;
+
+ if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
+ return NULL;
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
+#else
+ sprintf(path, "<fd:%d>", fd); /* for debugging */
+#endif
+ gz = gz_open(path, fd, mode);
+ free(path);
+ return gz;
+}
+
+/* -- see zlib.h -- */
+#ifdef _WIN32
+gzFile ZEXPORT gzopen_w(
+ const wchar_t *path,
+ const char *mode)
+{
+ return gz_open(path, -2, mode);
+}
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzbuffer(
+ gzFile file,
+ unsigned size)
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* make sure we haven't already allocated memory */
+ if (state->size != 0)
+ return -1;
+
+ /* check and set requested size */
+ if (size < 2)
+ size = 2; /* need two bytes to check magic header */
+ state->want = size;
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzrewind(
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* back up and start over */
+ if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
+ return -1;
+ gz_reset(state);
+ return 0;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzseek64(
+ gzFile file,
+ z_off64_t offset,
+ int whence)
+{
+ unsigned n;
+ z_off64_t ret;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* check that there's no error */
+ if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+ return -1;
+
+ /* can only seek from start or relative to current position */
+ if (whence != SEEK_SET && whence != SEEK_CUR)
+ return -1;
+
+ /* normalize offset to a SEEK_CUR specification */
+ if (whence == SEEK_SET)
+ offset -= state->x.pos;
+ else if (state->seek)
+ offset += state->skip;
+ state->seek = 0;
+
+ /* if within raw area while reading, just go there */
+ if (state->mode == GZ_READ && state->how == COPY &&
+ state->x.pos + offset >= 0) {
+ ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
+ if (ret == -1)
+ return -1;
+ state->x.have = 0;
+ state->eof = 0;
+ state->past = 0;
+ state->seek = 0;
+ gz_error(state, Z_OK, NULL);
+ state->strm.avail_in = 0;
+ state->x.pos += offset;
+ return state->x.pos;
+ }
+
+ /* calculate skip amount, rewinding if needed for back seek when reading */
+ if (offset < 0) {
+ if (state->mode != GZ_READ) /* writing -- can't go backwards */
+ return -1;
+ offset += state->x.pos;
+ if (offset < 0) /* before start of file! */
+ return -1;
+ if (gzrewind(file) == -1) /* rewind, then skip to offset */
+ return -1;
+ }
+
+ /* if reading, skip what's in output buffer (one less gzgetc() check) */
+ if (state->mode == GZ_READ) {
+ n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
+ (unsigned)offset : state->x.have;
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ offset -= n;
+ }
+
+ /* request skip (if not zero) */
+ if (offset) {
+ state->seek = 1;
+ state->skip = offset;
+ }
+ return state->x.pos + offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzseek(
+ gzFile file,
+ z_off_t offset,
+ int whence)
+{
+ z_off64_t ret;
+
+ ret = gzseek64(file, (z_off64_t)offset, whence);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gztell64(
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* return position */
+ return state->x.pos + (state->seek ? state->skip : 0);
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gztell(
+ gzFile file)
+{
+ z_off64_t ret;
+
+ ret = gztell64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzoffset64(
+ gzFile file)
+{
+ z_off64_t offset;
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return -1;
+
+ /* compute and return effective offset in file */
+ offset = LSEEK(state->fd, 0, SEEK_CUR);
+ if (offset == -1)
+ return -1;
+ if (state->mode == GZ_READ) /* reading */
+ offset -= state->strm.avail_in; /* don't count buffered input */
+ return offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzoffset(
+ gzFile file)
+{
+ z_off64_t ret;
+
+ ret = gzoffset64(file);
+ return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzeof(
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return 0;
+
+ /* return end-of-file state */
+ return state->mode == GZ_READ ? state->past : 0;
+}
+
+/* -- see zlib.h -- */
+const char * ZEXPORT gzerror(
+ gzFile file,
+ int *errnum)
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return NULL;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return NULL;
+
+ /* return error information */
+ if (errnum != NULL)
+ *errnum = state->err;
+ return state->err == Z_MEM_ERROR ? "out of memory" :
+ (state->msg == NULL ? "" : state->msg);
+}
+
+/* -- see zlib.h -- */
+void ZEXPORT gzclearerr(
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure and check integrity */
+ if (file == NULL)
+ return;
+ state = (gz_statep)file;
+ if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+ return;
+
+ /* clear error and end-of-file */
+ if (state->mode == GZ_READ) {
+ state->eof = 0;
+ state->past = 0;
+ }
+ gz_error(state, Z_OK, NULL);
+}
+
+/* Create an error message in allocated memory and set state->err and
+ state->msg accordingly. Free any previous error message already there. Do
+ not try to free or allocate space if the error is Z_MEM_ERROR (out of
+ memory). Simply save the error message as a static string. If there is an
+ allocation failure constructing the error message, then convert the error to
+ out of memory. */
+void ZLIB_INTERNAL gz_error(
+ gz_statep state,
+ int err,
+ const char *msg)
+{
+ /* free previously allocated message and clear */
+ if (state->msg != NULL) {
+ if (state->err != Z_MEM_ERROR)
+ free(state->msg);
+ state->msg = NULL;
+ }
+
+ /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
+ if (err != Z_OK && err != Z_BUF_ERROR)
+ state->x.have = 0;
+
+ /* set error code, and if no message, then done */
+ state->err = err;
+ if (msg == NULL)
+ return;
+
+ /* for an out of memory error, return literal string when requested */
+ if (err == Z_MEM_ERROR)
+ return;
+
+ /* construct error message with path */
+ if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
+ NULL) {
+ state->err = Z_MEM_ERROR;
+ return;
+ }
+#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
+ snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
+ "%s%s%s", state->path, ": ", msg);
+#else
+ strcpy(state->msg, state->path);
+ strcat(state->msg, ": ");
+ strcat(state->msg, msg);
+#endif
+ return;
+}
+
+#ifndef INT_MAX
+/* portably return maximum value for an int (when limits.h presumed not
+ available) -- we need to do this to cover cases where 2's complement not
+ used, since C standard permits 1's complement and sign-bit representations,
+ otherwise we could just use ((unsigned)-1) >> 1 */
+unsigned ZLIB_INTERNAL gz_intmax()
+{
+ unsigned p, q;
+
+ p = 1;
+ do {
+ q = p;
+ p <<= 1;
+ p++;
+ } while (p > q);
+ return q >> 1;
+}
+#endif
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/gzread.c b/core/src/fxcodec/fx_zlib/zlib_v128/gzread.c
new file mode 100644
index 0000000000..f162a32f10
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/gzread.c
@@ -0,0 +1,594 @@
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
+local int gz_avail OF((gz_statep));
+local int gz_look OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_fetch OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+
+/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
+ state->fd, and update state->eof, state->err, and state->msg as appropriate.
+ This function needs to loop on read(), since read() is not guaranteed to
+ read the number of bytes requested, depending on the type of descriptor. */
+local int gz_load(
+ gz_statep state,
+ unsigned char *buf,
+ unsigned len,
+ unsigned *have)
+{
+ int ret;
+
+ *have = 0;
+ do {
+ ret = read(state->fd, buf + *have, len - *have);
+ if (ret <= 0)
+ break;
+ *have += ret;
+ } while (*have < len);
+ if (ret < 0) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ if (ret == 0)
+ state->eof = 1;
+ return 0;
+}
+
+/* Load up input buffer and set eof flag if last data loaded -- return -1 on
+ error, 0 otherwise. Note that the eof flag is set when the end of the input
+ file is reached, even though there may be unused data in the buffer. Once
+ that data has been used, no more attempts will be made to read the file.
+ If strm->avail_in != 0, then the current data is moved to the beginning of
+ the input buffer, and then the remainder of the buffer is loaded with the
+ available data from the input file. */
+local int gz_avail(
+ gz_statep state)
+{
+ unsigned got;
+ z_streamp strm = &(state->strm);
+
+ if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+ return -1;
+ if (state->eof == 0) {
+ if (strm->avail_in) { /* copy what's there to the start */
+ unsigned char *p = state->in;
+ unsigned const char *q = strm->next_in;
+ unsigned n = strm->avail_in;
+ do {
+ *p++ = *q++;
+ } while (--n);
+ }
+ if (gz_load(state, state->in + strm->avail_in,
+ state->size - strm->avail_in, &got) == -1)
+ return -1;
+ strm->avail_in += got;
+ strm->next_in = state->in;
+ }
+ return 0;
+}
+
+/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
+ If this is the first time in, allocate required memory. state->how will be
+ left unchanged if there is no more input data available, will be set to COPY
+ if there is no gzip header and direct copying will be performed, or it will
+ be set to GZIP for decompression. If direct copying, then leftover input
+ data from the input buffer will be copied to the output buffer. In that
+ case, all further file reads will be directly to either the output buffer or
+ a user buffer. If decompressing, the inflate state will be initialized.
+ gz_look() will return 0 on success or -1 on failure. */
+local int gz_look(
+ gz_statep state)
+{
+ z_streamp strm = &(state->strm);
+
+ /* allocate read buffers and inflate memory */
+ if (state->size == 0) {
+ /* allocate buffers */
+ state->in = (unsigned char *)malloc(state->want);
+ state->out = (unsigned char *)malloc(state->want << 1);
+ if (state->in == NULL || state->out == NULL) {
+ if (state->out != NULL)
+ free(state->out);
+ if (state->in != NULL)
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ state->size = state->want;
+
+ /* allocate inflate memory */
+ state->strm.zalloc = Z_NULL;
+ state->strm.zfree = Z_NULL;
+ state->strm.opaque = Z_NULL;
+ state->strm.avail_in = 0;
+ state->strm.next_in = Z_NULL;
+ if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
+ free(state->out);
+ free(state->in);
+ state->size = 0;
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ }
+
+ /* get at least the magic bytes in the input buffer */
+ if (strm->avail_in < 2) {
+ if (gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0)
+ return 0;
+ }
+
+ /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
+ a logical dilemma here when considering the case of a partially written
+ gzip file, to wit, if a single 31 byte is written, then we cannot tell
+ whether this is a single-byte file, or just a partially written gzip
+ file -- for here we assume that if a gzip file is being written, then
+ the header will be written in a single operation, so that reading a
+ single byte is sufficient indication that it is not a gzip file) */
+ if (strm->avail_in > 1 &&
+ strm->next_in[0] == 31 && strm->next_in[1] == 139) {
+ inflateReset(strm);
+ state->how = GZIP;
+ state->direct = 0;
+ return 0;
+ }
+
+ /* no gzip header -- if we were decoding gzip before, then this is trailing
+ garbage. Ignore the trailing garbage and finish. */
+ if (state->direct == 0) {
+ strm->avail_in = 0;
+ state->eof = 1;
+ state->x.have = 0;
+ return 0;
+ }
+
+ /* doing raw i/o, copy any leftover input to output -- this assumes that
+ the output buffer is larger than the input buffer, which also assures
+ space for gzungetc() */
+ state->x.next = state->out;
+ if (strm->avail_in) {
+ memcpy(state->x.next, strm->next_in, strm->avail_in);
+ state->x.have = strm->avail_in;
+ strm->avail_in = 0;
+ }
+ state->how = COPY;
+ state->direct = 1;
+ return 0;
+}
+
+/* Decompress from input to the provided next_out and avail_out in the state.
+ On return, state->x.have and state->x.next point to the just decompressed
+ data. If the gzip stream completes, state->how is reset to LOOK to look for
+ the next gzip stream or raw data, once state->x.have is depleted. Returns 0
+ on success, -1 on failure. */
+local int gz_decomp(
+ gz_statep state)
+{
+ int ret = Z_OK;
+ unsigned had;
+ z_streamp strm = &(state->strm);
+
+ /* fill output buffer up to end of deflate stream */
+ had = strm->avail_out;
+ do {
+ /* get more input for inflate() */
+ if (strm->avail_in == 0 && gz_avail(state) == -1)
+ return -1;
+ if (strm->avail_in == 0) {
+ gz_error(state, Z_BUF_ERROR, "unexpected end of file");
+ break;
+ }
+
+ /* decompress and handle errors */
+ ret = inflate(strm, Z_NO_FLUSH);
+ if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: inflate stream corrupt");
+ return -1;
+ }
+ if (ret == Z_MEM_ERROR) {
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
+ gz_error(state, Z_DATA_ERROR,
+ strm->msg == NULL ? "compressed data error" : strm->msg);
+ return -1;
+ }
+ } while (strm->avail_out && ret != Z_STREAM_END);
+
+ /* update available output */
+ state->x.have = had - strm->avail_out;
+ state->x.next = strm->next_out - state->x.have;
+
+ /* if the gzip stream completed successfully, look for another */
+ if (ret == Z_STREAM_END)
+ state->how = LOOK;
+
+ /* good decompression */
+ return 0;
+}
+
+/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
+ Data is either copied from the input file or decompressed from the input
+ file depending on state->how. If state->how is LOOK, then a gzip header is
+ looked for to determine whether to copy or decompress. Returns -1 on error,
+ otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
+ end of the input file has been reached and all data has been processed. */
+local int gz_fetch(
+ gz_statep state)
+{
+ z_streamp strm = &(state->strm);
+
+ do {
+ switch(state->how) {
+ case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
+ if (gz_look(state) == -1)
+ return -1;
+ if (state->how == LOOK)
+ return 0;
+ break;
+ case COPY: /* -> COPY */
+ if (gz_load(state, state->out, state->size << 1, &(state->x.have))
+ == -1)
+ return -1;
+ state->x.next = state->out;
+ return 0;
+ case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
+ strm->avail_out = state->size << 1;
+ strm->next_out = state->out;
+ if (gz_decomp(state) == -1)
+ return -1;
+ }
+ } while (state->x.have == 0 && (!state->eof || strm->avail_in));
+ return 0;
+}
+
+/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
+local int gz_skip(
+ gz_statep state,
+ z_off64_t len)
+{
+ unsigned n;
+
+ /* skip over len bytes or reach end-of-file, whichever comes first */
+ while (len)
+ /* skip over whatever is in output buffer */
+ if (state->x.have) {
+ n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
+ (unsigned)len : state->x.have;
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ len -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && state->strm.avail_in == 0)
+ break;
+
+ /* need more data to skip -- load up output buffer */
+ else {
+ /* get more output, looking for header if required */
+ if (gz_fetch(state) == -1)
+ return -1;
+ }
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzread(
+ gzFile file,
+ voidp buf,
+ unsigned len)
+{
+ unsigned got, n;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids the flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+ return -1;
+ }
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* get len bytes to buf, or less than len if at the end */
+ got = 0;
+ do {
+ /* first just try copying data from the output buffer */
+ if (state->x.have) {
+ n = state->x.have > len ? len : state->x.have;
+ memcpy(buf, state->x.next, n);
+ state->x.next += n;
+ state->x.have -= n;
+ }
+
+ /* output buffer empty -- return if we're at the end of the input */
+ else if (state->eof && strm->avail_in == 0) {
+ state->past = 1; /* tried to read past end */
+ break;
+ }
+
+ /* need output data -- for small len or new stream load up our output
+ buffer */
+ else if (state->how == LOOK || len < (state->size << 1)) {
+ /* get more output, looking for header if required */
+ if (gz_fetch(state) == -1)
+ return -1;
+ continue; /* no progress yet -- go back to copy above */
+ /* the copy above assures that we will leave with space in the
+ output buffer, allowing at least one gzungetc() to succeed */
+ }
+
+ /* large len -- read directly into user buffer */
+ else if (state->how == COPY) { /* read directly */
+ if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
+ return -1;
+ }
+
+ /* large len -- decompress directly into user buffer */
+ else { /* state->how == GZIP */
+ strm->avail_out = len;
+ strm->next_out = (unsigned char *)buf;
+ if (gz_decomp(state) == -1)
+ return -1;
+ n = state->x.have;
+ state->x.have = 0;
+ }
+
+ /* update progress */
+ len -= n;
+ buf = (char *)buf + n;
+ got += n;
+ state->x.pos += n;
+ } while (len);
+
+ /* return number of bytes read into user buffer (will fit in int) */
+ return (int)got;
+}
+
+/* -- see zlib.h -- */
+#ifdef Z_PREFIX_SET
+# undef z_gzgetc
+#else
+# undef gzgetc
+#endif
+int ZEXPORT gzgetc(
+ gzFile file)
+{
+ int ret;
+ unsigned char buf[1];
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* try output buffer (no need to check for skip request) */
+ if (state->x.have) {
+ state->x.have--;
+ state->x.pos++;
+ return *(state->x.next)++;
+ }
+
+ /* nothing there -- try gzread() */
+ ret = gzread(file, buf, 1);
+ return ret < 1 ? -1 : buf[0];
+}
+
+int ZEXPORT gzgetc_(
+gzFile file)
+{
+ return gzgetc(file);
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzungetc(
+ int c,
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return -1;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* can't push EOF */
+ if (c < 0)
+ return -1;
+
+ /* if output buffer empty, put byte at end (allows more pushing) */
+ if (state->x.have == 0) {
+ state->x.have = 1;
+ state->x.next = state->out + (state->size << 1) - 1;
+ state->x.next[0] = c;
+ state->x.pos--;
+ state->past = 0;
+ return c;
+ }
+
+ /* if no room, give up (must have already done a gzungetc()) */
+ if (state->x.have == (state->size << 1)) {
+ gz_error(state, Z_DATA_ERROR, "out of room to push characters");
+ return -1;
+ }
+
+ /* slide output data if needed and insert byte before existing data */
+ if (state->x.next == state->out) {
+ unsigned char *src = state->out + state->x.have;
+ unsigned char *dest = state->out + (state->size << 1);
+ while (src > state->out)
+ *--dest = *--src;
+ state->x.next = dest;
+ }
+ state->x.have++;
+ state->x.next--;
+ state->x.next[0] = c;
+ state->x.pos--;
+ state->past = 0;
+ return c;
+}
+
+/* -- see zlib.h -- */
+char * ZEXPORT gzgets(
+ gzFile file,
+ char *buf,
+ int len)
+{
+ unsigned left, n;
+ char *str;
+ unsigned char *eol;
+ gz_statep state;
+
+ /* check parameters and get internal structure */
+ if (file == NULL || buf == NULL || len < 1)
+ return NULL;
+ state = (gz_statep)file;
+
+ /* check that we're reading and that there's no (serious) error */
+ if (state->mode != GZ_READ ||
+ (state->err != Z_OK && state->err != Z_BUF_ERROR))
+ return NULL;
+
+ /* process a skip request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_skip(state, state->skip) == -1)
+ return NULL;
+ }
+
+ /* copy output bytes up to new line or len - 1, whichever comes first --
+ append a terminating zero to the string (we don't check for a zero in
+ the contents, let the user worry about that) */
+ str = buf;
+ left = (unsigned)len - 1;
+ if (left) do {
+ /* assure that something is in the output buffer */
+ if (state->x.have == 0 && gz_fetch(state) == -1)
+ return NULL; /* error */
+ if (state->x.have == 0) { /* end of file */
+ state->past = 1; /* read past end */
+ break; /* return what we have */
+ }
+
+ /* look for end-of-line in current output buffer */
+ n = state->x.have > left ? left : state->x.have;
+ eol = (unsigned char *)memchr(state->x.next, '\n', n);
+ if (eol != NULL)
+ n = (unsigned)(eol - state->x.next) + 1;
+
+ /* copy through end-of-line, or remainder if not found */
+ memcpy(buf, state->x.next, n);
+ state->x.have -= n;
+ state->x.next += n;
+ state->x.pos += n;
+ left -= n;
+ buf += n;
+ } while (left && eol == NULL);
+
+ /* return terminated string, or if nothing, end of file */
+ if (buf == str)
+ return NULL;
+ buf[0] = 0;
+ return str;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzdirect(
+ gzFile file)
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+
+ /* if the state is not known, but we can find out, then do so (this is
+ mainly for right after a gzopen() or gzdopen()) */
+ if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
+ (void)gz_look(state);
+
+ /* return 1 if transparent, 0 if processing a gzip stream */
+ return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(
+ gzFile file)
+{
+ int ret, err;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're reading */
+ if (state->mode != GZ_READ)
+ return Z_STREAM_ERROR;
+
+ /* free memory and close file */
+ if (state->size) {
+ inflateEnd(&(state->strm));
+ free(state->out);
+ free(state->in);
+ }
+ err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ ret = close(state->fd);
+ free(state);
+ return ret ? Z_ERRNO : err;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/gzwrite.c b/core/src/fxcodec/fx_zlib/zlib_v128/gzwrite.c
new file mode 100644
index 0000000000..161070866f
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/gzwrite.c
@@ -0,0 +1,576 @@
+/* gzwrite.c -- zlib functions for writing gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_init OF((gz_statep));
+local int gz_comp OF((gz_statep, int));
+local int gz_zero OF((gz_statep, z_off64_t));
+
+/* Initialize state for writing a gzip file. Mark initialization by setting
+ state->size to non-zero. Return -1 on failure or 0 on success. */
+local int gz_init(
+ gz_statep state)
+{
+ int ret;
+ z_streamp strm = &(state->strm);
+
+ /* allocate input buffer */
+ state->in = (unsigned char *)malloc(state->want);
+ if (state->in == NULL) {
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* only need output buffer and deflate state if compressing */
+ if (!state->direct) {
+ /* allocate output buffer */
+ state->out = (unsigned char *)malloc(state->want);
+ if (state->out == NULL) {
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+
+ /* allocate deflate memory, set up for gzip compression */
+ strm->zalloc = Z_NULL;
+ strm->zfree = Z_NULL;
+ strm->opaque = Z_NULL;
+ ret = deflateInit2(strm, state->level, Z_DEFLATED,
+ MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
+ if (ret != Z_OK) {
+ free(state->out);
+ free(state->in);
+ gz_error(state, Z_MEM_ERROR, "out of memory");
+ return -1;
+ }
+ }
+
+ /* mark state as initialized */
+ state->size = state->want;
+
+ /* initialize write buffer if compressing */
+ if (!state->direct) {
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ state->x.next = strm->next_out;
+ }
+ return 0;
+}
+
+/* Compress whatever is at avail_in and next_in and write to the output file.
+ Return -1 if there is an error writing to the output file, otherwise 0.
+ flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
+ then the deflate() state is reset to start a new gzip stream. If gz->direct
+ is true, then simply write to the output file without compressing, and
+ ignore flush. */
+local int gz_comp(
+ gz_statep state,
+ int flush)
+{
+ int ret, got;
+ unsigned have;
+ z_streamp strm = &(state->strm);
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return -1;
+
+ /* write directly if requested */
+ if (state->direct) {
+ got = write(state->fd, strm->next_in, strm->avail_in);
+ if (got < 0 || (unsigned)got != strm->avail_in) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ strm->avail_in = 0;
+ return 0;
+ }
+
+ /* run deflate() on provided input until it produces no more output */
+ ret = Z_OK;
+ do {
+ /* write out current buffer contents if full, or if flushing, but if
+ doing Z_FINISH then don't write until we get to Z_STREAM_END */
+ if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
+ (flush != Z_FINISH || ret == Z_STREAM_END))) {
+ have = (unsigned)(strm->next_out - state->x.next);
+ if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
+ (unsigned)got != have)) {
+ gz_error(state, Z_ERRNO, zstrerror());
+ return -1;
+ }
+ if (strm->avail_out == 0) {
+ strm->avail_out = state->size;
+ strm->next_out = state->out;
+ }
+ state->x.next = strm->next_out;
+ }
+
+ /* compress */
+ have = strm->avail_out;
+ ret = deflate(strm, flush);
+ if (ret == Z_STREAM_ERROR) {
+ gz_error(state, Z_STREAM_ERROR,
+ "internal error: deflate stream corrupt");
+ return -1;
+ }
+ have -= strm->avail_out;
+ } while (have);
+
+ /* if that completed a deflate stream, allow another to start */
+ if (flush == Z_FINISH)
+ deflateReset(strm);
+
+ /* all done, no errors */
+ return 0;
+}
+
+/* Compress len zeros to output. Return -1 on error, 0 on success. */
+local int gz_zero(
+ gz_statep state,
+ z_off64_t len)
+{
+ int first;
+ unsigned n;
+ z_streamp strm = &(state->strm);
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+
+ /* compress len zeros (len guaranteed > 0) */
+ first = 1;
+ while (len) {
+ n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
+ (unsigned)len : state->size;
+ if (first) {
+ memset(state->in, 0, n);
+ first = 0;
+ }
+ strm->avail_in = n;
+ strm->next_in = state->in;
+ state->x.pos += n;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return -1;
+ len -= n;
+ }
+ return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzwrite(
+ gzFile file,
+ voidpc buf,
+ unsigned len)
+{
+ unsigned put = len;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return 0;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* since an int is returned, make sure len fits in one, otherwise return
+ with an error (this avoids the flaw in the interface) */
+ if ((int)len < 0) {
+ gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+ return 0;
+ }
+
+ /* if len is zero, avoid unnecessary operations */
+ if (len == 0)
+ return 0;
+
+ /* allocate memory if this is the first time through */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* for small len, copy to input buffer, otherwise compress directly */
+ if (len < state->size) {
+ /* copy to input buffer, compress when full */
+ do {
+ unsigned have, copy;
+
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
+ copy = state->size - have;
+ if (copy > len)
+ copy = len;
+ memcpy(state->in + have, buf, copy);
+ strm->avail_in += copy;
+ state->x.pos += copy;
+ buf = (const char *)buf + copy;
+ len -= copy;
+ if (len && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ } while (len);
+ }
+ else {
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* directly compress user buffer to file */
+ strm->avail_in = len;
+ strm->next_in = (z_const Bytef *)buf;
+ state->x.pos += len;
+ if (gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+ }
+
+ /* input was all buffered or compressed (put will fit in int) */
+ return (int)put;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputc(
+ gzFile file,
+ int c)
+{
+ unsigned have;
+ unsigned char buf[1];
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return -1;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* try writing to input buffer for speed (state->size == 0 if buffer not
+ initialized) */
+ if (state->size) {
+ if (strm->avail_in == 0)
+ strm->next_in = state->in;
+ have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
+ if (have < state->size) {
+ state->in[have] = c;
+ strm->avail_in++;
+ state->x.pos++;
+ return c & 0xff;
+ }
+ }
+
+ /* no room in buffer or not initialized, use gz_write() */
+ buf[0] = c;
+ if (gzwrite(file, buf, 1) != 1)
+ return -1;
+ return c & 0xff;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputs(
+ gzFile file,
+ const char *str)
+{
+ int ret;
+ unsigned len;
+
+ /* write string */
+ len = (unsigned)strlen(str);
+ ret = gzwrite(file, str, len);
+ return ret == 0 && len != 0 ? -1 : ret;
+}
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+#include <stdarg.h>
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
+{
+ int size, len;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* do the printf() into the input buffer, put length in len */
+ size = (int)(state->size);
+ state->in[size - 1] = 0;
+#ifdef NO_vsnprintf
+# ifdef HAS_vsprintf_void
+ (void)vsprintf((char *)(state->in), format, va);
+ for (len = 0; len < size; len++)
+ if (state->in[len] == 0) break;
+# else
+ len = vsprintf((char *)(state->in), format, va);
+# endif
+#else
+# ifdef HAS_vsnprintf_void
+ (void)vsnprintf((char *)(state->in), size, format, va);
+ len = strlen((char *)(state->in));
+# else
+ len = vsnprintf((char *)(state->in), size, format, va);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, defer compression until needed */
+ strm->avail_in = (unsigned)len;
+ strm->next_in = state->in;
+ state->x.pos += len;
+ return len;
+}
+
+int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
+{
+ va_list va;
+ int ret;
+
+ va_start(va, format);
+ ret = gzvprintf(file, format, va);
+ va_end(va);
+ return ret;
+}
+
+#else /* !STDC && !Z_HAVE_STDARG_H */
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (
+ gzFile file,
+ const char *format,
+ int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10,
+ int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
+{
+ int size, len;
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that can really pass pointer in ints */
+ if (sizeof(int) != sizeof(void *))
+ return 0;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return 0;
+
+ /* make sure we have some buffer space */
+ if (state->size == 0 && gz_init(state) == -1)
+ return 0;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return 0;
+ }
+
+ /* consume whatever's left in the input buffer */
+ if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+ return 0;
+
+ /* do the printf() into the input buffer, put length in len */
+ size = (int)(state->size);
+ state->in[size - 1] = 0;
+#ifdef NO_snprintf
+# ifdef HAS_sprintf_void
+ sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+ for (len = 0; len < size; len++)
+ if (state->in[len] == 0) break;
+# else
+ len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+# endif
+#else
+# ifdef HAS_snprintf_void
+ snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+ a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+ len = strlen((char *)(state->in));
+# else
+ len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
+ a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
+ a19, a20);
+# endif
+#endif
+
+ /* check that printf() results fit in buffer */
+ if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+ return 0;
+
+ /* update buffer and position, defer compression until needed */
+ strm->avail_in = (unsigned)len;
+ strm->next_in = state->in;
+ state->x.pos += len;
+ return len;
+}
+
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzflush(
+ gzFile file,
+ int flush)
+{
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return -1;
+ state = (gz_statep)file;
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* check flush parameter */
+ if (flush < 0 || flush > Z_FINISH)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* compress remaining data with requested flush */
+ gz_comp(state, flush);
+ return state->err;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzsetparams(
+ gzFile file,
+ int level,
+ int strategy)
+{
+ gz_statep state;
+ z_streamp strm;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+ strm = &(state->strm);
+
+ /* check that we're writing and that there's no error */
+ if (state->mode != GZ_WRITE || state->err != Z_OK)
+ return Z_STREAM_ERROR;
+
+ /* if no change is requested, then do nothing */
+ if (level == state->level && strategy == state->strategy)
+ return Z_OK;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ return -1;
+ }
+
+ /* change compression parameters for subsequent input */
+ if (state->size) {
+ /* flush previous input with previous parameters before changing */
+ if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
+ return state->err;
+ deflateParams(strm, level, strategy);
+ }
+ state->level = level;
+ state->strategy = strategy;
+ return Z_OK;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_w(
+ gzFile file)
+{
+ int ret = Z_OK;
+ gz_statep state;
+
+ /* get internal structure */
+ if (file == NULL)
+ return Z_STREAM_ERROR;
+ state = (gz_statep)file;
+
+ /* check that we're writing */
+ if (state->mode != GZ_WRITE)
+ return Z_STREAM_ERROR;
+
+ /* check for seek request */
+ if (state->seek) {
+ state->seek = 0;
+ if (gz_zero(state, state->skip) == -1)
+ ret = state->err;
+ }
+
+ /* flush, free memory, and close file */
+ if (gz_comp(state, Z_FINISH) == -1)
+ ret = state->err;
+ if (state->size) {
+ if (!state->direct) {
+ (void)deflateEnd(&(state->strm));
+ free(state->out);
+ }
+ free(state->in);
+ }
+ gz_error(state, Z_OK, NULL);
+ free(state->path);
+ if (close(state->fd) == -1)
+ ret = Z_ERRNO;
+ free(state);
+ return ret;
+}
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/inffast.h b/core/src/fxcodec/fx_zlib/zlib_v128/inffast.h
new file mode 100644
index 0000000000..e5c1aa4ca8
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/inffast.h
@@ -0,0 +1,11 @@
+/* inffast.h -- header to use inffast.c
+ * Copyright (C) 1995-2003, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/inffixed.h b/core/src/fxcodec/fx_zlib/zlib_v128/inffixed.h
new file mode 100644
index 0000000000..d628327769
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/inffixed.h
@@ -0,0 +1,94 @@
+ /* inffixed.h -- table for decoding fixed codes
+ * Generated automatically by makefixed().
+ */
+
+ /* WARNING: this file should *not* be used by applications.
+ It is part of the implementation of this library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+ static const code lenfix[512] = {
+ {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
+ {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
+ {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
+ {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
+ {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
+ {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
+ {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
+ {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
+ {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
+ {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
+ {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
+ {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
+ {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
+ {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
+ {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
+ {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
+ {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
+ {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
+ {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
+ {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
+ {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
+ {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
+ {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
+ {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
+ {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
+ {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
+ {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
+ {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
+ {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
+ {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
+ {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
+ {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
+ {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
+ {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
+ {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
+ {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
+ {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
+ {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
+ {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
+ {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
+ {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
+ {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
+ {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
+ {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
+ {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
+ {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
+ {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
+ {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
+ {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
+ {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
+ {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
+ {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
+ {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
+ {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
+ {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
+ {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
+ {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
+ {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
+ {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
+ {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
+ {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
+ {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
+ {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
+ {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
+ {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
+ {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
+ {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
+ {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
+ {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
+ {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
+ {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
+ {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
+ {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
+ {0,9,255}
+ };
+
+ static const code distfix[32] = {
+ {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
+ {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
+ {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
+ {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
+ {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
+ {22,5,193},{64,5,0}
+ };
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/inflate.h b/core/src/fxcodec/fx_zlib/zlib_v128/inflate.h
new file mode 100644
index 0000000000..95f4986d40
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/inflate.h
@@ -0,0 +1,122 @@
+/* inflate.h -- internal inflate state definition
+ * Copyright (C) 1995-2009 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* define NO_GZIP when compiling if you want to disable gzip header and
+ trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
+ the crc code when it is not needed. For shared libraries, gzip decoding
+ should be left enabled. */
+#ifndef NO_GZIP
+# define GUNZIP
+#endif
+
+/* Possible inflate modes between inflate() calls */
+typedef enum {
+ HEAD, /* i: waiting for magic header */
+ FLAGS, /* i: waiting for method and flags (gzip) */
+ TIME, /* i: waiting for modification time (gzip) */
+ OS, /* i: waiting for extra flags and operating system (gzip) */
+ EXLEN, /* i: waiting for extra length (gzip) */
+ EXTRA, /* i: waiting for extra bytes (gzip) */
+ NAME, /* i: waiting for end of file name (gzip) */
+ COMMENT, /* i: waiting for end of comment (gzip) */
+ HCRC, /* i: waiting for header crc (gzip) */
+ DICTID, /* i: waiting for dictionary check value */
+ DICT, /* waiting for inflateSetDictionary() call */
+ TYPE, /* i: waiting for type bits, including last-flag bit */
+ TYPEDO, /* i: same, but skip check to exit inflate on new block */
+ STORED, /* i: waiting for stored size (length and complement) */
+ COPY_, /* i/o: same as COPY below, but only first time in */
+ COPY, /* i/o: waiting for input or output to copy stored block */
+ TABLE, /* i: waiting for dynamic block table lengths */
+ LENLENS, /* i: waiting for code length code lengths */
+ CODELENS, /* i: waiting for length/lit and distance code lengths */
+ LEN_, /* i: same as LEN below, but only first time in */
+ LEN, /* i: waiting for length/lit/eob code */
+ LENEXT, /* i: waiting for length extra bits */
+ DIST, /* i: waiting for distance code */
+ DISTEXT, /* i: waiting for distance extra bits */
+ MATCH, /* o: waiting for output space to copy string */
+ LIT, /* o: waiting for output space to write literal */
+ CHECK, /* i: waiting for 32-bit check value */
+ LENGTH, /* i: waiting for 32-bit length (gzip) */
+ DONE, /* finished check, done -- remain here until reset */
+ BAD, /* got a data error -- remain here until reset */
+ MEM, /* got an inflate() memory error -- remain here until reset */
+ SYNC /* looking for synchronization bytes to restart inflate() */
+} inflate_mode;
+
+/*
+ State transitions between above modes -
+
+ (most modes can go to BAD or MEM on error -- not shown for clarity)
+
+ Process header:
+ HEAD -> (gzip) or (zlib) or (raw)
+ (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
+ HCRC -> TYPE
+ (zlib) -> DICTID or TYPE
+ DICTID -> DICT -> TYPE
+ (raw) -> TYPEDO
+ Read deflate blocks:
+ TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
+ STORED -> COPY_ -> COPY -> TYPE
+ TABLE -> LENLENS -> CODELENS -> LEN_
+ LEN_ -> LEN
+ Read deflate codes in fixed or dynamic block:
+ LEN -> LENEXT or LIT or TYPE
+ LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
+ LIT -> LEN
+ Process trailer:
+ CHECK -> LENGTH -> DONE
+ */
+
+/* state maintained between inflate() calls. Approximately 10K bytes. */
+struct inflate_state {
+ inflate_mode mode; /* current inflate mode */
+ int last; /* true if processing last block */
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
+ int havedict; /* true if dictionary provided */
+ int flags; /* gzip header method and flags (0 if zlib) */
+ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
+ unsigned long check; /* protected copy of check value */
+ unsigned long total; /* protected copy of output count */
+ gz_headerp head; /* where to save gzip header information */
+ /* sliding window */
+ unsigned wbits; /* log base 2 of requested window size */
+ unsigned wsize; /* window size or zero if not using window */
+ unsigned whave; /* valid bytes in the window */
+ unsigned wnext; /* window write index */
+ unsigned char FAR *window; /* allocated sliding window, if needed */
+ /* bit accumulator */
+ unsigned long hold; /* input bit accumulator */
+ unsigned bits; /* number of bits in "in" */
+ /* for string and stored block copying */
+ unsigned length; /* literal or length of data to copy */
+ unsigned offset; /* distance back to copy string from */
+ /* for table and code decoding */
+ unsigned extra; /* extra bits needed */
+ /* fixed and dynamic code tables */
+ code const FAR *lencode; /* starting table for length/literal codes */
+ code const FAR *distcode; /* starting table for distance codes */
+ unsigned lenbits; /* index bits for lencode */
+ unsigned distbits; /* index bits for distcode */
+ /* dynamic table building */
+ unsigned ncode; /* number of code length code lengths */
+ unsigned nlen; /* number of length code lengths */
+ unsigned ndist; /* number of distance code lengths */
+ unsigned have; /* number of code lengths in lens[] */
+ code FAR *next; /* next available space in codes[] */
+ unsigned short lens[320]; /* temporary storage for code lengths */
+ unsigned short work[288]; /* work area for code table building */
+ code codes[ENOUGH]; /* space for code tables */
+ int sane; /* if false, allow invalid distance too far */
+ int back; /* bits back of last unprocessed length/lit */
+ unsigned was; /* initial length of match */
+};
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/inftrees.h b/core/src/fxcodec/fx_zlib/zlib_v128/inftrees.h
new file mode 100644
index 0000000000..baa53a0b1a
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/inftrees.h
@@ -0,0 +1,62 @@
+/* inftrees.h -- header to use inftrees.c
+ * Copyright (C) 1995-2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* Structure for decoding tables. Each entry provides either the
+ information needed to do the operation requested by the code that
+ indexed that table entry, or it provides a pointer to another
+ table that indexes more bits of the code. op indicates whether
+ the entry is a pointer to another table, a literal, a length or
+ distance, an end-of-block, or an invalid code. For a table
+ pointer, the low four bits of op is the number of index bits of
+ that table. For a length or distance, the low four bits of op
+ is the number of extra bits to get after the code. bits is
+ the number of bits in this code or part of the code to drop off
+ of the bit buffer. val is the actual byte to output in the case
+ of a literal, the base length or distance, or the offset from
+ the current table to the next table. Each entry is four bytes. */
+typedef struct {
+ unsigned char op; /* operation, extra bits, table bits */
+ unsigned char bits; /* bits in this part of the code */
+ unsigned short val; /* offset in table or code value */
+} code;
+
+/* op values as set by inflate_table():
+ 00000000 - literal
+ 0000tttt - table link, tttt != 0 is the number of table index bits
+ 0001eeee - length or distance, eeee is the number of extra bits
+ 01100000 - end of block
+ 01000000 - invalid code
+ */
+
+/* Maximum size of the dynamic table. The maximum number of code structures is
+ 1444, which is the sum of 852 for literal/length codes and 592 for distance
+ codes. These values were found by exhaustive searches using the program
+ examples/enough.c found in the zlib distribtution. The arguments to that
+ program are the number of symbols, the initial root table size, and the
+ maximum bit length of a code. "enough 286 9 15" for literal/length codes
+ returns returns 852, and "enough 30 6 15" for distance codes returns 592.
+ The initial root table size (9 or 6) is found in the fifth argument of the
+ inflate_table() calls in inflate.c and infback.c. If the root table size is
+ changed, then these maximum sizes would be need to be recalculated and
+ updated. */
+#define ENOUGH_LENS 852
+#define ENOUGH_DISTS 592
+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
+
+/* Type of code to build for inflate_table() */
+typedef enum {
+ CODES,
+ LENS,
+ DISTS
+} codetype;
+
+int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
+ unsigned codes, code FAR * FAR *table,
+ unsigned FAR *bits, unsigned short FAR *work));
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/trees.h b/core/src/fxcodec/fx_zlib/zlib_v128/trees.h
new file mode 100644
index 0000000000..d35639d82a
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/trees.h
@@ -0,0 +1,128 @@
+/* header created automatically with -DGEN_TREES_H */
+
+local const ct_data static_ltree[L_CODES+2] = {
+{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
+{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
+{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
+{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
+{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
+{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
+{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
+{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
+{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
+{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
+{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
+{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
+{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
+{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
+{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
+{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
+{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
+{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
+{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
+{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
+{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
+{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
+{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
+{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
+{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
+{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
+{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
+{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
+{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
+{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
+{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
+{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
+{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
+{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
+{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
+{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
+{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
+{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
+{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
+{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
+{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
+{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
+{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
+{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
+{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
+{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
+{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
+{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
+{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
+{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
+{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
+{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
+{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
+{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
+{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
+{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
+{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
+{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
+};
+
+local const ct_data static_dtree[D_CODES] = {
+{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
+{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
+{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
+{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
+{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
+{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
+};
+
+const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
+ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
+ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
+10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
+12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
+18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
+28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
+29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
+};
+
+const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
+13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
+17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
+19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
+21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
+22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
+23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
+26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
+27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
+};
+
+local const int base_length[LENGTH_CODES] = {
+0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
+64, 80, 96, 112, 128, 160, 192, 224, 0
+};
+
+local const int base_dist[D_CODES] = {
+ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
+ 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
+ 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
+};
+
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/zconf.h b/core/src/fxcodec/fx_zlib/zlib_v128/zconf.h
new file mode 100644
index 0000000000..5708a377e0
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/zconf.h
@@ -0,0 +1,519 @@
+/* zconf.h -- configuration of the zlib compression library
+ * Copyright (C) 1995-2013 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#ifndef ZCONF_H
+#define ZCONF_H
+
+/*
+ * If you *really* need a unique prefix for all types and library functions,
+ * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ * Even better than compiling with -DZ_PREFIX would be to use configure to set
+ * this permanently in zconf.h using "./configure --zprefix".
+ */
+#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */
+# define Z_PREFIX_SET
+
+/* all linked symbols */
+# define _dist_code z__dist_code
+# define _length_code z__length_code
+# define _tr_align z__tr_align
+# define _tr_flush_bits z__tr_flush_bits
+# define _tr_flush_block z__tr_flush_block
+# define _tr_init z__tr_init
+# define _tr_stored_block z__tr_stored_block
+# define _tr_tally z__tr_tally
+# define adler32 z_adler32
+# define adler32_combine z_adler32_combine
+# define adler32_combine64 z_adler32_combine64
+# ifndef Z_SOLO
+# define compress z_compress
+# define compress2 z_compress2
+# define compressBound z_compressBound
+# endif
+# define crc32 z_crc32
+# define crc32_combine z_crc32_combine
+# define crc32_combine64 z_crc32_combine64
+# define deflate z_deflate
+# define deflateBound z_deflateBound
+# define deflateCopy z_deflateCopy
+# define deflateEnd z_deflateEnd
+# define deflateInit2_ z_deflateInit2_
+# define deflateInit_ z_deflateInit_
+# define deflateParams z_deflateParams
+# define deflatePending z_deflatePending
+# define deflatePrime z_deflatePrime
+# define deflateReset z_deflateReset
+# define deflateResetKeep z_deflateResetKeep
+# define deflateSetDictionary z_deflateSetDictionary
+# define deflateSetHeader z_deflateSetHeader
+# define deflateTune z_deflateTune
+# define deflate_copyright z_deflate_copyright
+# define get_crc_table z_get_crc_table
+# ifndef Z_SOLO
+# define gz_error z_gz_error
+# define gz_intmax z_gz_intmax
+# define gz_strwinerror z_gz_strwinerror
+# define gzbuffer z_gzbuffer
+# define gzclearerr z_gzclearerr
+# define gzclose z_gzclose
+# define gzclose_r z_gzclose_r
+# define gzclose_w z_gzclose_w
+# define gzdirect z_gzdirect
+# define gzdopen z_gzdopen
+# define gzeof z_gzeof
+# define gzerror z_gzerror
+# define gzflush z_gzflush
+# define gzgetc z_gzgetc
+# define gzgetc_ z_gzgetc_
+# define gzgets z_gzgets
+# define gzoffset z_gzoffset
+# define gzoffset64 z_gzoffset64
+# define gzopen z_gzopen
+# define gzopen64 z_gzopen64
+# ifdef _WIN32
+# define gzopen_w z_gzopen_w
+# endif
+# define gzprintf z_gzprintf
+# define gzvprintf z_gzvprintf
+# define gzputc z_gzputc
+# define gzputs z_gzputs
+# define gzread z_gzread
+# define gzrewind z_gzrewind
+# define gzseek z_gzseek
+# define gzseek64 z_gzseek64
+# define gzsetparams z_gzsetparams
+# define gztell z_gztell
+# define gztell64 z_gztell64
+# define gzungetc z_gzungetc
+# define gzwrite z_gzwrite
+# endif
+# define inflate z_inflate
+# define inflateBack z_inflateBack
+# define inflateBackEnd z_inflateBackEnd
+# define inflateBackInit_ z_inflateBackInit_
+# define inflateCopy z_inflateCopy
+# define inflateEnd z_inflateEnd
+# define inflateGetHeader z_inflateGetHeader
+# define inflateInit2_ z_inflateInit2_
+# define inflateInit_ z_inflateInit_
+# define inflateMark z_inflateMark
+# define inflatePrime z_inflatePrime
+# define inflateReset z_inflateReset
+# define inflateReset2 z_inflateReset2
+# define inflateSetDictionary z_inflateSetDictionary
+# define inflateGetDictionary z_inflateGetDictionary
+# define inflateSync z_inflateSync
+# define inflateSyncPoint z_inflateSyncPoint
+# define inflateUndermine z_inflateUndermine
+# define inflateResetKeep z_inflateResetKeep
+# define inflate_copyright z_inflate_copyright
+# define inflate_fast z_inflate_fast
+# define inflate_table z_inflate_table
+# ifndef Z_SOLO
+# define uncompress z_uncompress
+# endif
+# define zError z_zError
+# ifndef Z_SOLO
+# define zcalloc z_zcalloc
+# define zcfree z_zcfree
+# endif
+# define zlibCompileFlags z_zlibCompileFlags
+# define zlibVersion z_zlibVersion
+
+/* all zlib typedefs in zlib.h and zconf.h */
+# define Byte z_Byte
+# define Bytef z_Bytef
+# define alloc_func z_alloc_func
+# define charf z_charf
+# define free_func z_free_func
+# ifndef Z_SOLO
+# define gzFile z_gzFile
+# endif
+# define gz_header z_gz_header
+# define gz_headerp z_gz_headerp
+# define in_func z_in_func
+# define intf z_intf
+# define out_func z_out_func
+# define uInt z_uInt
+# define uIntf z_uIntf
+# define uLong z_uLong
+# define uLongf z_uLongf
+# define voidp z_voidp
+# define voidpc z_voidpc
+# define voidpf z_voidpf
+
+/* all zlib structs in zlib.h and zconf.h */
+# define gz_header_s z_gz_header_s
+# define internal_state z_internal_state
+
+#endif
+
+#if defined(__MSDOS__) && !defined(MSDOS)
+# define MSDOS
+#endif
+#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
+# define OS2
+#endif
+#if defined(_WINDOWS) && !defined(WINDOWS)
+# define WINDOWS
+#endif
+#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
+# ifndef WIN32
+# define WIN32
+# endif
+#endif
+#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
+# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
+# ifndef SYS16BIT
+# define SYS16BIT
+# endif
+# endif
+#endif
+
+/*
+ * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
+ * than 64k bytes at a time (needed on systems with 16-bit int).
+ */
+#ifdef SYS16BIT
+# define MAXSEG_64K
+#endif
+#ifdef MSDOS
+# define UNALIGNED_OK
+#endif
+
+#ifdef __STDC_VERSION__
+# ifndef STDC
+# define STDC
+# endif
+# if __STDC_VERSION__ >= 199901L
+# ifndef STDC99
+# define STDC99
+# endif
+# endif
+#endif
+#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
+# define STDC
+#endif
+#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
+# define STDC
+#endif
+
+#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
+# define STDC
+#endif
+
+#ifndef STDC
+# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
+# define const /* note: need a more gentle solution here */
+# endif
+#endif
+
+#if defined(ZLIB_CONST) && !defined(z_const)
+# define z_const const
+#else
+# define z_const
+#endif
+
+/* Some Mac compilers merge all .h files incorrectly: */
+#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
+# define NO_DUMMY_DECL
+#endif
+
+/* Maximum value for memLevel in deflateInit2 */
+#ifndef MAX_MEM_LEVEL
+# ifdef MAXSEG_64K
+# define MAX_MEM_LEVEL 8
+# else
+# define MAX_MEM_LEVEL 9
+# endif
+#endif
+
+/* Maximum value for windowBits in deflateInit2 and inflateInit2.
+ * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
+ * created by gzip. (Files created by minigzip can still be extracted by
+ * gzip.)
+ */
+#ifndef MAX_WBITS
+# define MAX_WBITS 15 /* 32K LZ77 window */
+#endif
+
+/* The memory requirements for deflate are (in bytes):
+ (1 << (windowBits+2)) + (1 << (memLevel+9))
+ that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
+ plus a few kilobytes for small objects. For example, if you want to reduce
+ the default memory requirements from 256K to 128K, compile with
+ make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+ Of course this will generally degrade compression (there's no free lunch).
+
+ The memory requirements for inflate are (in bytes) 1 << windowBits
+ that is, 32K for windowBits=15 (default value) plus a few kilobytes
+ for small objects.
+*/
+
+ /* Type declarations */
+
+#ifndef OF /* function prototypes */
+# ifdef STDC
+# define OF(args) args
+# else
+# define OF(args) ()
+# endif
+#endif
+
+#ifndef Z_ARG /* function prototypes for stdarg */
+# if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# define Z_ARG(args) args
+# else
+# define Z_ARG(args) ()
+# endif
+#endif
+
+/* The following definitions for FAR are needed only for MSDOS mixed
+ * model programming (small or medium model with some far allocations).
+ * This was tested only with MSC; for other MSDOS compilers you may have
+ * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
+ * just define FAR to be empty.
+ */
+#ifdef SYS16BIT
+# if defined(M_I86SM) || defined(M_I86MM)
+ /* MSC small or medium model */
+# define SMALL_MEDIUM
+# ifdef _MSC_VER
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+# if (defined(__SMALL__) || defined(__MEDIUM__))
+ /* Turbo C small or medium model */
+# define SMALL_MEDIUM
+# ifdef __BORLANDC__
+# define FAR _far
+# else
+# define FAR far
+# endif
+# endif
+#endif
+
+#if defined(WINDOWS) || defined(WIN32)
+ /* If building or using zlib as a DLL, define ZLIB_DLL.
+ * This is not mandatory, but it offers a little performance increase.
+ */
+# ifdef ZLIB_DLL
+# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
+# ifdef ZLIB_INTERNAL
+# define ZEXTERN extern __declspec(dllexport)
+# else
+# define ZEXTERN extern __declspec(dllimport)
+# endif
+# endif
+# endif /* ZLIB_DLL */
+ /* If building or using zlib with the WINAPI/WINAPIV calling convention,
+ * define ZLIB_WINAPI.
+ * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
+ */
+# ifdef ZLIB_WINAPI
+# ifdef FAR
+# undef FAR
+# endif
+# include <windows.h>
+ /* No need for _export, use ZLIB.DEF instead. */
+ /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+# define ZEXPORT WINAPI
+# ifdef WIN32
+# define ZEXPORTVA WINAPIV
+# else
+# define ZEXPORTVA FAR CDECL
+# endif
+# endif
+#endif
+
+#if defined (__BEOS__)
+# ifdef ZLIB_DLL
+# ifdef ZLIB_INTERNAL
+# define ZEXPORT __declspec(dllexport)
+# define ZEXPORTVA __declspec(dllexport)
+# else
+# define ZEXPORT __declspec(dllimport)
+# define ZEXPORTVA __declspec(dllimport)
+# endif
+# endif
+#endif
+
+#ifndef ZEXTERN
+# define ZEXTERN extern
+#endif
+#ifndef ZEXPORT
+# define ZEXPORT
+#endif
+#ifndef ZEXPORTVA
+# define ZEXPORTVA
+#endif
+
+#ifndef FAR
+# define FAR
+#endif
+
+#if !defined(__MACTYPES__)
+typedef unsigned char Byte; /* 8 bits */
+#endif
+typedef unsigned int uInt; /* 16 bits or more */
+typedef unsigned long uLong; /* 32 bits or more */
+
+#ifdef SMALL_MEDIUM
+ /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
+# define Bytef Byte FAR
+#else
+ typedef Byte FAR Bytef;
+#endif
+typedef char FAR charf;
+typedef int FAR intf;
+typedef uInt FAR uIntf;
+typedef uLong FAR uLongf;
+
+#ifdef STDC
+ typedef void const *voidpc;
+ typedef void FAR *voidpf;
+ typedef void *voidp;
+#else
+ typedef Byte const *voidpc;
+ typedef Byte FAR *voidpf;
+ typedef Byte *voidp;
+#endif
+
+#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC)
+# include <limits.h>
+# if (UINT_MAX == 0xffffffffUL)
+# define Z_U4 unsigned
+# elif (ULONG_MAX == 0xffffffffUL)
+# define Z_U4 unsigned long
+# elif (USHRT_MAX == 0xffffffffUL)
+# define Z_U4 unsigned short
+# endif
+#endif
+
+#ifdef Z_U4
+ typedef Z_U4 z_crc_t;
+#else
+ typedef unsigned long z_crc_t;
+#endif
+
+#if 0 /* Sunliang.Liu 20100908 sync the config to the old revision, otherwise, evc compile failed. */
+#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */
+# define Z_HAVE_UNISTD_H
+#endif
+#endif /* 0 */
+
+#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */
+# define Z_HAVE_STDARG_H
+#endif
+
+#if 0 /* Sunliang.Liu 20100908 sync the config to the old revision, otherwise, evc compile failed. */
+#ifdef STDC
+# ifndef Z_SOLO
+# include <sys/types.h> /* for off_t */
+# endif
+#endif
+#endif /* 0 */
+
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifndef Z_SOLO
+# include <stdarg.h> /* for va_list */
+# endif
+#endif
+
+#ifdef _WIN32
+# ifndef Z_SOLO
+# include <stddef.h> /* for wchar_t */
+# endif
+#endif
+
+#if 0 /* Sunliang.Liu 20100908 sync the config to the old revision, otherwise, evc compile failed. */
+/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
+ * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
+ * though the former does not conform to the LFS document), but considering
+ * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
+ * equivalently requesting no 64-bit operations
+ */
+#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
+# undef _LARGEFILE64_SOURCE
+#endif
+#endif /* 0 */
+
+#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H)
+# define Z_HAVE_UNISTD_H
+#endif
+#ifndef Z_SOLO
+#if 0 /* Sunliang.Liu 20100908 sync the config to the old revision, otherwise, evc compile failed. */
+# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
+# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
+# ifdef VMS
+# include <unixio.h> /* for off_t */
+# endif
+# ifndef z_off_t
+# define z_off_t off_t
+# endif
+# endif
+#endif /* 0 */
+#endif
+
+#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
+# define Z_LFS64
+#endif
+
+#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
+# define Z_LARGE64
+#endif
+
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
+# define Z_WANT64
+#endif
+
+#if !defined(SEEK_SET) && !defined(Z_SOLO)
+# define SEEK_SET 0 /* Seek from beginning of file. */
+# define SEEK_CUR 1 /* Seek from current position. */
+# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
+#endif
+
+#ifndef z_off_t
+# define z_off_t long
+#endif
+
+#if !defined(_WIN32) && defined(Z_LARGE64)
+# define z_off64_t off64_t
+#else
+# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO)
+# define z_off64_t __int64
+# else
+# define z_off64_t z_off_t
+# endif
+#endif
+
+/* MVS linker does not support external names larger than 8 bytes */
+#if defined(__MVS__)
+ #pragma map(deflateInit_,"DEIN")
+ #pragma map(deflateInit2_,"DEIN2")
+ #pragma map(deflateEnd,"DEEND")
+ #pragma map(deflateBound,"DEBND")
+ #pragma map(inflateInit_,"ININ")
+ #pragma map(inflateInit2_,"ININ2")
+ #pragma map(inflateEnd,"INEND")
+ #pragma map(inflateSync,"INSY")
+ #pragma map(inflateSetDictionary,"INSEDI")
+ #pragma map(compressBound,"CMBND")
+ #pragma map(inflate_table,"INTABL")
+ #pragma map(inflate_fast,"INFA")
+ #pragma map(inflate_copyright,"INCOPY")
+#endif
+
+#endif /* ZCONF_H */
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/zlib.h b/core/src/fxcodec/fx_zlib/zlib_v128/zlib.h
new file mode 100644
index 0000000000..ec8ca7adb8
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/zlib.h
@@ -0,0 +1,1832 @@
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+ version 1.2.8, April 28th, 2013
+
+ Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Jean-loup Gailly Mark Adler
+ jloup@gzip.org madler@alumni.caltech.edu
+
+
+ The data format used by the zlib library is described by RFCs (Request for
+ Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
+ (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
+*/
+
+#ifndef ZLIB_H
+#define ZLIB_H
+
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
+#define deflate_copyright FPDFAPI_deflate_copyright
+#define adler32 FPDFAPI_adler32
+#define compress2 FPDFAPI_compress2
+#define compress FPDFAPI_compress
+#define compressBound FPDFAPI_compressBound
+#define get_crc_table FPDFAPI_get_crc_table
+#define crc32 FPDFAPI_crc32
+#define deflateInit_ FPDFAPI_deflateInit_
+#define deflateInit2_ FPDFAPI_deflateInit2_
+#define deflateSetDictionary FPDFAPI_deflateSetDictionary
+#define deflateReset FPDFAPI_deflateReset
+#define deflatePrime FPDFAPI_deflatePrime
+#define deflateParams FPDFAPI_deflateParams
+#define deflateBound FPDFAPI_deflateBound
+#define deflateSetHeader FPDFAPI_deflateSetHeader
+#define deflateTune FPDFAPI_deflateTune
+#define deflate FPDFAPI_deflate
+#define deflateEnd FPDFAPI_deflateEnd
+#define deflateCopy FPDFAPI_deflateCopy
+#define inflateBackInit_ FPDFAPI_inflateBackInit_
+#define inflateBack FPDFAPI_inflateBack
+#define inflateBackEnd FPDFAPI_inflateBackEnd
+#define inflateReset FPDFAPI_inflateReset
+#define inflateInit2_ FPDFAPI_inflateInit2_
+#define inflateInit_ FPDFAPI_inflateInit_
+#define inflate FPDFAPI_inflate
+#define inflateEnd FPDFAPI_inflateEnd
+#define inflateSetDictionary FPDFAPI_inflateSetDictionary
+#define inflateSync FPDFAPI_inflateSync
+#define inflateSyncPoint FPDFAPI_inflateSyncPoint
+#define inflateCopy FPDFAPI_inflateCopy
+#define uncompress FPDFAPI_uncompress
+#define zlibVersion FPDFAPI_zlibVersion
+#define zlibCompileFlags FPDFAPI_zlibCompileFlags
+#define zError FPDFAPI_zError
+#define z_errmsg FPDFAPI_z_errmsg
+#define zcfree FPDFAPI_zcfree
+#define zcalloc FPDFAPI_zcalloc
+#define inflate_fast FPDFAPI_inflate_fast
+#define inflate_table FPDFAPI_inflate_table
+#define inflate_copyright FPDFAPI_inflate_copyright
+#define _length_code FPDFAPI_length_code
+#define _tr_flush_block FPDFAPI_tr_flush_block
+#define _dist_code FPDFAPI_dist_code
+#define _tr_stored_block FPDFAPI_tr_stored_block
+#define _tr_init FPDFAPI_tr_init
+#define _tr_align FPDFAPI_tr_align
+#define _tr_tally FPDFAPI_tr_tally
+#define adler32_combine FPDFAPI_adler32_combine
+#define inflatePrime FPDFAPI_inflatePrime
+#define inflateGetHeader FPDFAPI_inflateGetHeader
+#define crc32_combine FPDFAPI_crc32_combine
+#define inflateReset2 FPDFAPI_inflateReset2
+#define inflateUndermine FPDFAPI_inflateUndermine
+#define inflateMark FPDFAPI_inflateMark
+#define adler32_combine64 FPDFAPI_adler32_combine64
+
+#include "zconf.h"
+
+/* Sunliang.Liu 20100908 sync the config to the old revision. NO_GZIP */
+#define NO_GZIP /* XYQ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ZLIB_VERSION "1.2.8"
+#define ZLIB_VERNUM 0x1280
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 8
+#define ZLIB_VER_SUBREVISION 0
+
+/*
+ The 'zlib' compression library provides in-memory compression and
+ decompression functions, including integrity checks of the uncompressed data.
+ This version of the library supports only one compression method (deflation)
+ but other algorithms will be added later and will have the same stream
+ interface.
+
+ Compression can be done in a single step if the buffers are large enough,
+ or can be done by repeated calls of the compression function. In the latter
+ case, the application must provide more input and/or consume the output
+ (providing more output space) before each call.
+
+ The compressed data format used by default by the in-memory functions is
+ the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
+ around a deflate stream, which is itself documented in RFC 1951.
+
+ The library also supports reading and writing files in gzip (.gz) format
+ with an interface similar to that of stdio using the functions that start
+ with "gz". The gzip format is different from the zlib format. gzip is a
+ gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
+
+ This library can optionally read and write gzip streams in memory as well.
+
+ The zlib format was designed to be compact and fast for use in memory
+ and on communications channels. The gzip format was designed for single-
+ file compression on file systems, has a larger header than zlib to maintain
+ directory information, and uses a different, slower check method than zlib.
+
+ The library does not install any signal handler. The decoder checks
+ the consistency of the compressed data, so the library should never crash
+ even in case of corrupted input.
+*/
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void (*free_func) OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+ z_const Bytef *next_in; /* next input byte */
+ uInt avail_in; /* number of bytes available at next_in */
+ uLong total_in; /* total number of input bytes read so far */
+
+ Bytef *next_out; /* next output byte should be put there */
+ uInt avail_out; /* remaining free space at next_out */
+ uLong total_out; /* total number of bytes output so far */
+
+ z_const char *msg; /* last error message, NULL if no error */
+ struct internal_state FAR *state; /* not visible by applications */
+
+ alloc_func zalloc; /* used to allocate the internal state */
+ free_func zfree; /* used to free the internal state */
+ voidpf opaque; /* private data object passed to zalloc and zfree */
+
+ int data_type; /* best guess about the data type: binary or text */
+ uLong adler; /* adler32 value of the uncompressed data */
+ uLong reserved; /* reserved for future use */
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+/*
+ gzip header information passed to and from zlib routines. See RFC 1952
+ for more details on the meanings of these fields.
+*/
+typedef struct gz_header_s {
+ int text; /* true if compressed data believed to be text */
+ uLong time; /* modification time */
+ int xflags; /* extra flags (not used when writing a gzip file) */
+ int os; /* operating system */
+ Bytef *extra; /* pointer to extra field or Z_NULL if none */
+ uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
+ uInt extra_max; /* space at extra (only when reading header) */
+ Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
+ uInt name_max; /* space at name (only when reading header) */
+ Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
+ uInt comm_max; /* space at comment (only when reading header) */
+ int hcrc; /* true if there was or will be a header crc */
+ int done; /* true when done reading gzip header (not used
+ when writing a gzip file) */
+} gz_header;
+
+typedef gz_header FAR *gz_headerp;
+
+/*
+ The application must update next_in and avail_in when avail_in has dropped
+ to zero. It must update next_out and avail_out when avail_out has dropped
+ to zero. The application must initialize zalloc, zfree and opaque before
+ calling the init function. All other fields are set by the compression
+ library and must not be updated by the application.
+
+ The opaque value provided by the application will be passed as the first
+ parameter for calls of zalloc and zfree. This can be useful for custom
+ memory management. The compression library attaches no meaning to the
+ opaque value.
+
+ zalloc must return Z_NULL if there is not enough memory for the object.
+ If zlib is used in a multi-threaded application, zalloc and zfree must be
+ thread safe.
+
+ On 16-bit systems, the functions zalloc and zfree must be able to allocate
+ exactly 65536 bytes, but will not be required to allocate more than this if
+ the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers
+ returned by zalloc for objects of exactly 65536 bytes *must* have their
+ offset normalized to zero. The default allocation function provided by this
+ library ensures this (see zutil.c). To reduce memory requirements and avoid
+ any allocation of 64K objects, at the expense of compression ratio, compile
+ the library with -DMAX_WBITS=14 (see zconf.h).
+
+ The fields total_in and total_out can be used for statistics or progress
+ reports. After compression, total_in holds the total size of the
+ uncompressed data and may be saved for use in the decompressor (particularly
+ if the decompressor wants to decompress everything in a single step).
+*/
+
+ /* constants */
+
+#define Z_NO_FLUSH 0
+#define Z_PARTIAL_FLUSH 1
+#define Z_SYNC_FLUSH 2
+#define Z_FULL_FLUSH 3
+#define Z_FINISH 4
+#define Z_BLOCK 5
+#define Z_TREES 6
+/* Allowed flush values; see deflate() and inflate() below for details */
+
+#define Z_OK 0
+#define Z_STREAM_END 1
+#define Z_NEED_DICT 2
+#define Z_ERRNO (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR (-3)
+#define Z_MEM_ERROR (-4)
+#define Z_BUF_ERROR (-5)
+#define Z_VERSION_ERROR (-6)
+/* Return codes for the compression/decompression functions. Negative values
+ * are errors, positive values are used for special but normal events.
+ */
+
+#define Z_NO_COMPRESSION 0
+#define Z_BEST_SPEED 1
+#define Z_BEST_COMPRESSION 9
+#define Z_DEFAULT_COMPRESSION (-1)
+/* compression levels */
+
+#define Z_FILTERED 1
+#define Z_HUFFMAN_ONLY 2
+#define Z_RLE 3
+#define Z_FIXED 4
+#define Z_DEFAULT_STRATEGY 0
+/* compression strategy; see deflateInit2() below for details */
+
+#define Z_BINARY 0
+#define Z_TEXT 1
+#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
+#define Z_UNKNOWN 2
+/* Possible values of the data_type field (though see inflate()) */
+
+#define Z_DEFLATED 8
+/* The deflate compression method (the only one supported in this version) */
+
+#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
+
+#define zlib_version zlibVersion()
+/* for compatibility with versions < 1.0.2 */
+
+
+ /* basic functions */
+
+ZEXTERN const char * ZEXPORT zlibVersion OF((void));
+/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
+ If the first character differs, the library code actually used is not
+ compatible with the zlib.h header file used by the application. This check
+ is automatically made by deflateInit and inflateInit.
+ */
+
+/*
+ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
+
+ Initializes the internal stream state for compression. The fields
+ zalloc, zfree and opaque must be initialized before by the caller. If
+ zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
+ allocation functions.
+
+ The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
+ 1 gives best speed, 9 gives best compression, 0 gives no compression at all
+ (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION
+ requests a default compromise between speed and compression (currently
+ equivalent to level 6).
+
+ deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if level is not a valid compression level, or
+ Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
+ with the version assumed by the caller (ZLIB_VERSION). msg is set to null
+ if there is no error message. deflateInit does not perform any compression:
+ this will be done by deflate().
+*/
+
+
+ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
+/*
+ deflate compresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. deflate performs one or both of the
+ following actions:
+
+ - Compress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), next_in and avail_in are updated and
+ processing will resume at this point for the next call of deflate().
+
+ - Provide more output starting at next_out and update next_out and avail_out
+ accordingly. This action is forced if the parameter flush is non zero.
+ Forcing flush frequently degrades the compression ratio, so this parameter
+ should be set only when necessary (in interactive applications). Some
+ output may be provided even if flush is not set.
+
+ Before the call of deflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating avail_in or avail_out accordingly; avail_out should
+ never be zero before the call. The application can consume the compressed
+ output when it wants, for example when the output buffer is full (avail_out
+ == 0), or after each call of deflate(). If deflate returns Z_OK and with
+ zero avail_out, it must be called again after making room in the output
+ buffer because there might be more output pending.
+
+ Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
+ decide how much data to accumulate before producing output, in order to
+ maximize compression.
+
+ If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
+ flushed to the output buffer and the output is aligned on a byte boundary, so
+ that the decompressor can get all input data available so far. (In
+ particular avail_in is zero after the call if enough output space has been
+ provided before the call.) Flushing may degrade compression for some
+ compression algorithms and so it should be used only when necessary. This
+ completes the current deflate block and follows it with an empty stored block
+ that is three bits plus filler bits to the next byte, followed by four bytes
+ (00 00 ff ff).
+
+ If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
+ output buffer, but the output is not aligned to a byte boundary. All of the
+ input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
+ This completes the current deflate block and follows it with an empty fixed
+ codes block that is 10 bits long. This assures that enough bytes are output
+ in order for the decompressor to finish the block before the empty fixed code
+ block.
+
+ If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
+ for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
+ seven bits of the current block are held to be written as the next byte after
+ the next deflate block is completed. In this case, the decompressor may not
+ be provided enough bits at this point in order to complete decompression of
+ the data provided so far to the compressor. It may need to wait for the next
+ block to be emitted. This is for advanced applications that need to control
+ the emission of deflate blocks.
+
+ If flush is set to Z_FULL_FLUSH, all output is flushed as with
+ Z_SYNC_FLUSH, and the compression state is reset so that decompression can
+ restart from this point if previous compressed data has been damaged or if
+ random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+ compression.
+
+ If deflate returns with avail_out == 0, this function must be called again
+ with the same value of the flush parameter and more output space (updated
+ avail_out), until the flush is complete (deflate returns with non-zero
+ avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+ avail_out is greater than six to avoid repeated flush markers due to
+ avail_out == 0 on return.
+
+ If the parameter flush is set to Z_FINISH, pending input is processed,
+ pending output is flushed and deflate returns with Z_STREAM_END if there was
+ enough output space; if deflate returns with Z_OK, this function must be
+ called again with Z_FINISH and more output space (updated avail_out) but no
+ more input data, until it returns with Z_STREAM_END or an error. After
+ deflate has returned Z_STREAM_END, the only possible operations on the stream
+ are deflateReset or deflateEnd.
+
+ Z_FINISH can be used immediately after deflateInit if all the compression
+ is to be done in a single step. In this case, avail_out must be at least the
+ value returned by deflateBound (see below). Then deflate is guaranteed to
+ return Z_STREAM_END. If not enough output space is provided, deflate will
+ not return Z_STREAM_END, and it must be called again as described above.
+
+ deflate() sets strm->adler to the adler32 checksum of all input read
+ so far (that is, total_in bytes).
+
+ deflate() may update strm->data_type if it can make a good guess about
+ the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
+ binary. This field is only for information purposes and does not affect the
+ compression algorithm in any manner.
+
+ deflate() returns Z_OK if some progress has been made (more input
+ processed or more output produced), Z_STREAM_END if all input has been
+ consumed and all output has been produced (only when flush is set to
+ Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
+ if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
+ (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
+ fatal, and deflate() can be called again with more input and more output
+ space to continue compressing.
+*/
+
+
+ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
+ stream state was inconsistent, Z_DATA_ERROR if the stream was freed
+ prematurely (some input or output was discarded). In the error case, msg
+ may be set but then points to a static string (which must not be
+ deallocated).
+*/
+
+
+/*
+ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
+
+ Initializes the internal stream state for decompression. The fields
+ next_in, avail_in, zalloc, zfree and opaque must be initialized before by
+ the caller. If next_in is not Z_NULL and avail_in is large enough (the
+ exact value depends on the compression method), inflateInit determines the
+ compression method from the zlib header and allocates all data structures
+ accordingly; otherwise the allocation will be deferred to the first call of
+ inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
+ use default allocation functions.
+
+ inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit does not perform any decompression
+ apart from possibly reading the zlib header if present: actual decompression
+ will be done by inflate(). (So next_in and avail_in may be modified, but
+ next_out and avail_out are unused and unchanged.) The current implementation
+ of inflateInit() does not process any header information -- that is deferred
+ until inflate() is called.
+*/
+
+
+ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
+/*
+ inflate decompresses as much data as possible, and stops when the input
+ buffer becomes empty or the output buffer becomes full. It may introduce
+ some output latency (reading input without producing any output) except when
+ forced to flush.
+
+ The detailed semantics are as follows. inflate performs one or both of the
+ following actions:
+
+ - Decompress more input starting at next_in and update next_in and avail_in
+ accordingly. If not all input can be processed (because there is not
+ enough room in the output buffer), next_in is updated and processing will
+ resume at this point for the next call of inflate().
+
+ - Provide more output starting at next_out and update next_out and avail_out
+ accordingly. inflate() provides as much output as possible, until there is
+ no more input data or no more space in the output buffer (see below about
+ the flush parameter).
+
+ Before the call of inflate(), the application should ensure that at least
+ one of the actions is possible, by providing more input and/or consuming more
+ output, and updating the next_* and avail_* values accordingly. The
+ application can consume the uncompressed output when it wants, for example
+ when the output buffer is full (avail_out == 0), or after each call of
+ inflate(). If inflate returns Z_OK and with zero avail_out, it must be
+ called again after making room in the output buffer because there might be
+ more output pending.
+
+ The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
+ Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much
+ output as possible to the output buffer. Z_BLOCK requests that inflate()
+ stop if and when it gets to the next deflate block boundary. When decoding
+ the zlib or gzip format, this will cause inflate() to return immediately
+ after the header and before the first block. When doing a raw inflate,
+ inflate() will go ahead and process the first block, and will return when it
+ gets to the end of that block, or when it runs out of data.
+
+ The Z_BLOCK option assists in appending to or combining deflate streams.
+ Also to assist in this, on return inflate() will set strm->data_type to the
+ number of unused bits in the last byte taken from strm->next_in, plus 64 if
+ inflate() is currently decoding the last block in the deflate stream, plus
+ 128 if inflate() returned immediately after decoding an end-of-block code or
+ decoding the complete header up to just before the first byte of the deflate
+ stream. The end-of-block will not be indicated until all of the uncompressed
+ data from that block has been written to strm->next_out. The number of
+ unused bits may in general be greater than seven, except when bit 7 of
+ data_type is set, in which case the number of unused bits will be less than
+ eight. data_type is set as noted here every time inflate() returns for all
+ flush options, and so can be used to determine the amount of currently
+ consumed input in bits.
+
+ The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
+ end of each deflate block header is reached, before any actual data in that
+ block is decoded. This allows the caller to determine the length of the
+ deflate block header for later use in random access within a deflate block.
+ 256 is added to the value of strm->data_type when inflate() returns
+ immediately after reaching the end of the deflate block header.
+
+ inflate() should normally be called until it returns Z_STREAM_END or an
+ error. However if all decompression is to be performed in a single step (a
+ single call of inflate), the parameter flush should be set to Z_FINISH. In
+ this case all pending input is processed and all pending output is flushed;
+ avail_out must be large enough to hold all of the uncompressed data for the
+ operation to complete. (The size of the uncompressed data may have been
+ saved by the compressor for this purpose.) The use of Z_FINISH is not
+ required to perform an inflation in one step. However it may be used to
+ inform inflate that a faster approach can be used for the single inflate()
+ call. Z_FINISH also informs inflate to not maintain a sliding window if the
+ stream completes, which reduces inflate's memory footprint. If the stream
+ does not complete, either because not all of the stream is provided or not
+ enough output space is provided, then a sliding window will be allocated and
+ inflate() can be called again to continue the operation as if Z_NO_FLUSH had
+ been used.
+
+ In this implementation, inflate() always flushes as much output as
+ possible to the output buffer, and always uses the faster approach on the
+ first call. So the effects of the flush parameter in this implementation are
+ on the return value of inflate() as noted below, when inflate() returns early
+ when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of
+ memory for a sliding window when Z_FINISH is used.
+
+ If a preset dictionary is needed after this call (see inflateSetDictionary
+ below), inflate sets strm->adler to the Adler-32 checksum of the dictionary
+ chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
+ strm->adler to the Adler-32 checksum of all output produced so far (that is,
+ total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
+ below. At the end of the stream, inflate() checks that its computed adler32
+ checksum is equal to that saved by the compressor and returns Z_STREAM_END
+ only if the checksum is correct.
+
+ inflate() can decompress and check either zlib-wrapped or gzip-wrapped
+ deflate data. The header type is detected automatically, if requested when
+ initializing with inflateInit2(). Any information contained in the gzip
+ header is not retained, so applications that need that information should
+ instead use raw inflate, see inflateInit2() below, or inflateBack() and
+ perform their own processing of the gzip header and trailer. When processing
+ gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output
+ producted so far. The CRC-32 is checked against the gzip trailer.
+
+ inflate() returns Z_OK if some progress has been made (more input processed
+ or more output produced), Z_STREAM_END if the end of the compressed data has
+ been reached and all uncompressed output has been produced, Z_NEED_DICT if a
+ preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
+ corrupted (input stream not conforming to the zlib format or incorrect check
+ value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
+ next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
+ Z_BUF_ERROR if no progress is possible or if there was not enough room in the
+ output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+ inflate() can be called again with more input and more output space to
+ continue decompressing. If Z_DATA_ERROR is returned, the application may
+ then call inflateSync() to look for a good compression block if a partial
+ recovery of the data is desired.
+*/
+
+
+ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
+/*
+ All dynamically allocated data structures for this stream are freed.
+ This function discards any unprocessed input and does not flush any pending
+ output.
+
+ inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
+ was inconsistent. In the error case, msg may be set but then points to a
+ static string (which must not be deallocated).
+*/
+
+
+ /* Advanced functions */
+
+/*
+ The following functions are needed only in some special applications.
+*/
+
+/*
+ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
+ int level,
+ int method,
+ int windowBits,
+ int memLevel,
+ int strategy));
+
+ This is another version of deflateInit with more compression options. The
+ fields next_in, zalloc, zfree and opaque must be initialized before by the
+ caller.
+
+ The method parameter is the compression method. It must be Z_DEFLATED in
+ this version of the library.
+
+ The windowBits parameter is the base two logarithm of the window size
+ (the size of the history buffer). It should be in the range 8..15 for this
+ version of the library. Larger values of this parameter result in better
+ compression at the expense of memory usage. The default value is 15 if
+ deflateInit is used instead.
+
+ windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
+ determines the window size. deflate() will then generate raw deflate data
+ with no zlib header or trailer, and will not compute an adler32 check value.
+
+ windowBits can also be greater than 15 for optional gzip encoding. Add
+ 16 to windowBits to write a simple gzip header and trailer around the
+ compressed data instead of a zlib wrapper. The gzip header will have no
+ file name, no extra data, no comment, no modification time (set to zero), no
+ header crc, and the operating system will be set to 255 (unknown). If a
+ gzip stream is being written, strm->adler is a crc32 instead of an adler32.
+
+ The memLevel parameter specifies how much memory should be allocated
+ for the internal compression state. memLevel=1 uses minimum memory but is
+ slow and reduces compression ratio; memLevel=9 uses maximum memory for
+ optimal speed. The default value is 8. See zconf.h for total memory usage
+ as a function of windowBits and memLevel.
+
+ The strategy parameter is used to tune the compression algorithm. Use the
+ value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
+ filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
+ string match), or Z_RLE to limit match distances to one (run-length
+ encoding). Filtered data consists mostly of small values with a somewhat
+ random distribution. In this case, the compression algorithm is tuned to
+ compress them better. The effect of Z_FILTERED is to force more Huffman
+ coding and less string matching; it is somewhat intermediate between
+ Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as
+ fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The
+ strategy parameter only affects the compression ratio but not the
+ correctness of the compressed output even if it is not set appropriately.
+ Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
+ decoder for special applications.
+
+ deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
+ method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
+ incompatible with the version assumed by the caller (ZLIB_VERSION). msg is
+ set to null if there is no error message. deflateInit2 does not perform any
+ compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the compression dictionary from the given byte sequence
+ without producing any compressed output. When using the zlib format, this
+ function must be called immediately after deflateInit, deflateInit2 or
+ deflateReset, and before any call of deflate. When doing raw deflate, this
+ function must be called either before any call of deflate, or immediately
+ after the completion of a deflate block, i.e. after all input has been
+ consumed and all output has been delivered when using any of the flush
+ options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The
+ compressor and decompressor must use exactly the same dictionary (see
+ inflateSetDictionary).
+
+ The dictionary should consist of strings (byte sequences) that are likely
+ to be encountered later in the data to be compressed, with the most commonly
+ used strings preferably put towards the end of the dictionary. Using a
+ dictionary is most useful when the data to be compressed is short and can be
+ predicted with good accuracy; the data can then be compressed better than
+ with the default empty dictionary.
+
+ Depending on the size of the compression data structures selected by
+ deflateInit or deflateInit2, a part of the dictionary may in effect be
+ discarded, for example if the dictionary is larger than the window size
+ provided in deflateInit or deflateInit2. Thus the strings most likely to be
+ useful should be put at the end of the dictionary, not at the front. In
+ addition, the current implementation of deflate will use at most the window
+ size minus 262 bytes of the provided dictionary.
+
+ Upon return of this function, strm->adler is set to the adler32 value
+ of the dictionary; the decompressor may later use this value to determine
+ which dictionary has been used by the compressor. (The adler32 value
+ applies to the whole dictionary even if only a subset of the dictionary is
+ actually used by the compressor.) If a raw deflate was requested, then the
+ adler32 value is not computed and strm->adler is not set.
+
+ deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent (for example if deflate has already been called for this stream
+ or if not at a block boundary for raw deflate). deflateSetDictionary does
+ not perform any compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when several compression strategies will be
+ tried, for example when there are several ways of pre-processing the input
+ data with a filter. The streams that will be discarded should then be freed
+ by calling deflateEnd. Note that deflateCopy duplicates the internal
+ compression state which can be quite large, so this strategy is slow and can
+ consume lots of memory.
+
+ deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to deflateEnd followed by deflateInit,
+ but does not free and reallocate all the internal compression state. The
+ stream will keep the same compression level and any other attributes that
+ may have been set by deflateInit2.
+
+ deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
+ int level,
+ int strategy));
+/*
+ Dynamically update the compression level and compression strategy. The
+ interpretation of level and strategy is as in deflateInit2. This can be
+ used to switch between compression and straight copy of the input data, or
+ to switch to a different kind of input data requiring a different strategy.
+ If the compression level is changed, the input available so far is
+ compressed with the old level (and may be flushed); the new level will take
+ effect only at the next call of deflate().
+
+ Before the call of deflateParams, the stream state must be set as for
+ a call of deflate(), since the currently available input may have to be
+ compressed and flushed. In particular, strm->avail_out must be non-zero.
+
+ deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
+ stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if
+ strm->avail_out was zero.
+*/
+
+ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
+ int good_length,
+ int max_lazy,
+ int nice_length,
+ int max_chain));
+/*
+ Fine tune deflate's internal compression parameters. This should only be
+ used by someone who understands the algorithm used by zlib's deflate for
+ searching for the best matching string, and even then only by the most
+ fanatic optimizer trying to squeeze out the last compressed bit for their
+ specific input data. Read the deflate.c source code for the meaning of the
+ max_lazy, good_length, nice_length, and max_chain parameters.
+
+ deflateTune() can be called after deflateInit() or deflateInit2(), and
+ returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
+ */
+
+ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
+ uLong sourceLen));
+/*
+ deflateBound() returns an upper bound on the compressed size after
+ deflation of sourceLen bytes. It must be called after deflateInit() or
+ deflateInit2(), and after deflateSetHeader(), if used. This would be used
+ to allocate an output buffer for deflation in a single pass, and so would be
+ called before deflate(). If that first deflate() call is provided the
+ sourceLen input bytes, an output buffer allocated to the size returned by
+ deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed
+ to return Z_STREAM_END. Note that it is possible for the compressed size to
+ be larger than the value returned by deflateBound() if flush options other
+ than Z_FINISH or Z_NO_FLUSH are used.
+*/
+
+ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm,
+ unsigned *pending,
+ int *bits));
+/*
+ deflatePending() returns the number of bytes and bits of output that have
+ been generated, but not yet provided in the available output. The bytes not
+ provided would be due to the available output space having being consumed.
+ The number of bits of output not provided are between 0 and 7, where they
+ await more bits to join them in order to fill out a full byte. If pending
+ or bits are Z_NULL, then those values are not set.
+
+ deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+ */
+
+ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ deflatePrime() inserts bits in the deflate output stream. The intent
+ is that this function is used to start off the deflate output with the bits
+ leftover from a previous deflate stream when appending to it. As such, this
+ function can only be used for raw deflate, and must be used before the first
+ deflate() call after a deflateInit2() or deflateReset(). bits must be less
+ than or equal to 16, and that many of the least significant bits of value
+ will be inserted in the output.
+
+ deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
+ room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
+ source stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ deflateSetHeader() provides gzip header information for when a gzip
+ stream is requested by deflateInit2(). deflateSetHeader() may be called
+ after deflateInit2() or deflateReset() and before the first call of
+ deflate(). The text, time, os, extra field, name, and comment information
+ in the provided gz_header structure are written to the gzip header (xflag is
+ ignored -- the extra flags are set according to the compression level). The
+ caller must assure that, if not Z_NULL, name and comment are terminated with
+ a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
+ available there. If hcrc is true, a gzip header crc is included. Note that
+ the current versions of the command-line version of gzip (up through version
+ 1.3.x) do not support header crc's, and will report that it is a "multi-part
+ gzip file" and give up.
+
+ If deflateSetHeader is not used, the default gzip header has text false,
+ the time set to zero, and os set to 255, with no extra, name, or comment
+ fields. The gzip header is returned to the default state by deflateReset().
+
+ deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
+ int windowBits));
+
+ This is another version of inflateInit with an extra parameter. The
+ fields next_in, avail_in, zalloc, zfree and opaque must be initialized
+ before by the caller.
+
+ The windowBits parameter is the base two logarithm of the maximum window
+ size (the size of the history buffer). It should be in the range 8..15 for
+ this version of the library. The default value is 15 if inflateInit is used
+ instead. windowBits must be greater than or equal to the windowBits value
+ provided to deflateInit2() while compressing, or it must be equal to 15 if
+ deflateInit2() was not used. If a compressed stream with a larger window
+ size is given as input, inflate() will return with the error code
+ Z_DATA_ERROR instead of trying to allocate a larger window.
+
+ windowBits can also be zero to request that inflate use the window size in
+ the zlib header of the compressed stream.
+
+ windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
+ determines the window size. inflate() will then process raw deflate data,
+ not looking for a zlib or gzip header, not generating a check value, and not
+ looking for any check values for comparison at the end of the stream. This
+ is for use with other formats that use the deflate compressed data format
+ such as zip. Those formats provide their own check values. If a custom
+ format is developed using the raw deflate format for compressed data, it is
+ recommended that a check value such as an adler32 or a crc32 be applied to
+ the uncompressed data as is done in the zlib, gzip, and zip formats. For
+ most applications, the zlib format should be used as is. Note that comments
+ above on the use in deflateInit2() applies to the magnitude of windowBits.
+
+ windowBits can also be greater than 15 for optional gzip decoding. Add
+ 32 to windowBits to enable zlib and gzip decoding with automatic header
+ detection, or add 16 to decode only the gzip format (the zlib format will
+ return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
+ crc32 instead of an adler32.
+
+ inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+ version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+ invalid, such as a null pointer to the structure. msg is set to null if
+ there is no error message. inflateInit2 does not perform any decompression
+ apart from possibly reading the zlib header if present: actual decompression
+ will be done by inflate(). (So next_in and avail_in may be modified, but
+ next_out and avail_out are unused and unchanged.) The current implementation
+ of inflateInit2() does not process any header information -- that is
+ deferred until inflate() is called.
+*/
+
+ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
+ const Bytef *dictionary,
+ uInt dictLength));
+/*
+ Initializes the decompression dictionary from the given uncompressed byte
+ sequence. This function must be called immediately after a call of inflate,
+ if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+ can be determined from the adler32 value returned by that call of inflate.
+ The compressor and decompressor must use exactly the same dictionary (see
+ deflateSetDictionary). For raw inflate, this function can be called at any
+ time to set the dictionary. If the provided dictionary is smaller than the
+ window and there is already data in the window, then the provided dictionary
+ will amend what's there. The application must insure that the dictionary
+ that was used for compression is provided.
+
+ inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
+ parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is
+ inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
+ expected one (incorrect adler32 value). inflateSetDictionary does not
+ perform any decompression: this will be done by subsequent calls of
+ inflate().
+*/
+
+ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
+ Bytef *dictionary,
+ uInt *dictLength));
+/*
+ Returns the sliding dictionary being maintained by inflate. dictLength is
+ set to the number of bytes in the dictionary, and that many bytes are copied
+ to dictionary. dictionary must have enough space, where 32768 bytes is
+ always enough. If inflateGetDictionary() is called with dictionary equal to
+ Z_NULL, then only the dictionary length is returned, and nothing is copied.
+ Similary, if dictLength is Z_NULL, then it is not set.
+
+ inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
+ stream state is inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+/*
+ Skips invalid compressed data until a possible full flush point (see above
+ for the description of deflate with Z_FULL_FLUSH) can be found, or until all
+ available input is skipped. No output is provided.
+
+ inflateSync searches for a 00 00 FF FF pattern in the compressed data.
+ All full flush points have this pattern, but not all occurrences of this
+ pattern are full flush points.
+
+ inflateSync returns Z_OK if a possible full flush point has been found,
+ Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
+ has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
+ In the success case, the application may save the current current value of
+ total_in which indicates where valid compressed data was found. In the
+ error case, the application may repeatedly call inflateSync, providing more
+ input each time, until success or end of the input data.
+*/
+
+ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
+ z_streamp source));
+/*
+ Sets the destination stream as a complete copy of the source stream.
+
+ This function can be useful when randomly accessing a large stream. The
+ first pass through the stream can periodically record the inflate state,
+ allowing restarting inflate at those points when randomly accessing the
+ stream.
+
+ inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+ (such as zalloc being Z_NULL). msg is left unchanged in both source and
+ destination.
+*/
+
+ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
+/*
+ This function is equivalent to inflateEnd followed by inflateInit,
+ but does not free and reallocate all the internal decompression state. The
+ stream will keep attributes that may have been set by inflateInit2.
+
+ inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+ int windowBits));
+/*
+ This function is the same as inflateReset, but it also permits changing
+ the wrap and window size requests. The windowBits parameter is interpreted
+ the same as it is for inflateInit2.
+
+ inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent (such as zalloc or state being Z_NULL), or if
+ the windowBits parameter is invalid.
+*/
+
+ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
+ int bits,
+ int value));
+/*
+ This function inserts bits in the inflate input stream. The intent is
+ that this function is used to start inflating at a bit position in the
+ middle of a byte. The provided bits will be used before any bytes are used
+ from next_in. This function should only be used with raw inflate, and
+ should be used before the first inflate() call after inflateInit2() or
+ inflateReset(). bits must be less than or equal to 16, and that many of the
+ least significant bits of value will be inserted in the input.
+
+ If bits is negative, then the input stream bit buffer is emptied. Then
+ inflatePrime() can be called again to put bits in the buffer. This is used
+ to clear out bits leftover after feeding inflate a block description prior
+ to feeding inflate codes.
+
+ inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+/*
+ This function returns two values, one in the lower 16 bits of the return
+ value, and the other in the remaining upper bits, obtained by shifting the
+ return value down 16 bits. If the upper value is -1 and the lower value is
+ zero, then inflate() is currently decoding information outside of a block.
+ If the upper value is -1 and the lower value is non-zero, then inflate is in
+ the middle of a stored block, with the lower value equaling the number of
+ bytes from the input remaining to copy. If the upper value is not -1, then
+ it is the number of bits back from the current bit position in the input of
+ the code (literal or length/distance pair) currently being processed. In
+ that case the lower value is the number of bytes already emitted for that
+ code.
+
+ A code is being processed if inflate is waiting for more input to complete
+ decoding of the code, or if it has completed decoding but is waiting for
+ more output space to write the literal or match data.
+
+ inflateMark() is used to mark locations in the input data for random
+ access, which may be at bit positions, and to note those cases where the
+ output of a code may span boundaries of random access blocks. The current
+ location in the input stream can be determined from avail_in and data_type
+ as noted in the description for the Z_BLOCK flush parameter for inflate.
+
+ inflateMark returns the value noted above or -1 << 16 if the provided
+ source stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
+ gz_headerp head));
+/*
+ inflateGetHeader() requests that gzip header information be stored in the
+ provided gz_header structure. inflateGetHeader() may be called after
+ inflateInit2() or inflateReset(), and before the first call of inflate().
+ As inflate() processes the gzip stream, head->done is zero until the header
+ is completed, at which time head->done is set to one. If a zlib stream is
+ being decoded, then head->done is set to -1 to indicate that there will be
+ no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be
+ used to force inflate() to return immediately after header processing is
+ complete and before any actual data is decompressed.
+
+ The text, time, xflags, and os fields are filled in with the gzip header
+ contents. hcrc is set to true if there is a header CRC. (The header CRC
+ was valid if done is set to one.) If extra is not Z_NULL, then extra_max
+ contains the maximum number of bytes to write to extra. Once done is true,
+ extra_len contains the actual extra field length, and extra contains the
+ extra field, or that field truncated if extra_max is less than extra_len.
+ If name is not Z_NULL, then up to name_max characters are written there,
+ terminated with a zero unless the length is greater than name_max. If
+ comment is not Z_NULL, then up to comm_max characters are written there,
+ terminated with a zero unless the length is greater than comm_max. When any
+ of extra, name, or comment are not Z_NULL and the respective field is not
+ present in the header, then that field is set to Z_NULL to signal its
+ absence. This allows the use of deflateSetHeader() with the returned
+ structure to duplicate the header. However if those fields are set to
+ allocated memory, then the application will need to save those pointers
+ elsewhere so that they can be eventually freed.
+
+ If inflateGetHeader is not used, then the header information is simply
+ discarded. The header is always checked for validity, including the header
+ CRC if present. inflateReset() will reset the process to discard the header
+ information. The application would need to call inflateGetHeader() again to
+ retrieve the header from the next gzip stream.
+
+ inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+ stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window));
+
+ Initialize the internal stream state for decompression using inflateBack()
+ calls. The fields zalloc, zfree and opaque in strm must be initialized
+ before the call. If zalloc and zfree are Z_NULL, then the default library-
+ derived memory allocation routines are used. windowBits is the base two
+ logarithm of the window size, in the range 8..15. window is a caller
+ supplied buffer of that size. Except for special applications where it is
+ assured that deflate was used with small window sizes, windowBits must be 15
+ and a 32K byte window must be supplied to be able to decompress general
+ deflate streams.
+
+ See inflateBack() for the usage of these routines.
+
+ inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
+ the parameters are invalid, Z_MEM_ERROR if the internal state could not be
+ allocated, or Z_VERSION_ERROR if the version of the library does not match
+ the version of the header file.
+*/
+
+typedef unsigned (*in_func) OF((void FAR *,
+ z_const unsigned char FAR * FAR *));
+typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
+
+ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
+ in_func in, void FAR *in_desc,
+ out_func out, void FAR *out_desc));
+/*
+ inflateBack() does a raw inflate with a single call using a call-back
+ interface for input and output. This is potentially more efficient than
+ inflate() for file i/o applications, in that it avoids copying between the
+ output and the sliding window by simply making the window itself the output
+ buffer. inflate() can be faster on modern CPUs when used with large
+ buffers. inflateBack() trusts the application to not change the output
+ buffer passed by the output function, at least until inflateBack() returns.
+
+ inflateBackInit() must be called first to allocate the internal state
+ and to initialize the state with the user-provided window buffer.
+ inflateBack() may then be used multiple times to inflate a complete, raw
+ deflate stream with each call. inflateBackEnd() is then called to free the
+ allocated state.
+
+ A raw deflate stream is one with no zlib or gzip header or trailer.
+ This routine would normally be used in a utility that reads zip or gzip
+ files and writes out uncompressed files. The utility would decode the
+ header and process the trailer on its own, hence this routine expects only
+ the raw deflate stream to decompress. This is different from the normal
+ behavior of inflate(), which expects either a zlib or gzip header and
+ trailer around the deflate stream.
+
+ inflateBack() uses two subroutines supplied by the caller that are then
+ called by inflateBack() for input and output. inflateBack() calls those
+ routines until it reads a complete deflate stream and writes out all of the
+ uncompressed data, or until it encounters an error. The function's
+ parameters and return types are defined above in the in_func and out_func
+ typedefs. inflateBack() will call in(in_desc, &buf) which should return the
+ number of bytes of provided input, and a pointer to that input in buf. If
+ there is no input available, in() must return zero--buf is ignored in that
+ case--and inflateBack() will return a buffer error. inflateBack() will call
+ out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
+ should return zero on success, or non-zero on failure. If out() returns
+ non-zero, inflateBack() will return with an error. Neither in() nor out()
+ are permitted to change the contents of the window provided to
+ inflateBackInit(), which is also the buffer that out() uses to write from.
+ The length written by out() will be at most the window size. Any non-zero
+ amount of input may be provided by in().
+
+ For convenience, inflateBack() can be provided input on the first call by
+ setting strm->next_in and strm->avail_in. If that input is exhausted, then
+ in() will be called. Therefore strm->next_in must be initialized before
+ calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
+ immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
+ must also be initialized, and then if strm->avail_in is not zero, input will
+ initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+
+ The in_desc and out_desc parameters of inflateBack() is passed as the
+ first parameter of in() and out() respectively when they are called. These
+ descriptors can be optionally used to pass any information that the caller-
+ supplied in() and out() functions need to do their job.
+
+ On return, inflateBack() will set strm->next_in and strm->avail_in to
+ pass back any unused input that was provided by the last in() call. The
+ return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
+ if in() or out() returned an error, Z_DATA_ERROR if there was a format error
+ in the deflate stream (in which case strm->msg is set to indicate the nature
+ of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
+ In the case of Z_BUF_ERROR, an input or output error can be distinguished
+ using strm->next_in which will be Z_NULL only if in() returned an error. If
+ strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
+ non-zero. (in() will always be called before out(), so strm->next_in is
+ assured to be defined if out() returns non-zero.) Note that inflateBack()
+ cannot return Z_OK.
+*/
+
+ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
+/*
+ All memory allocated by inflateBackInit() is freed.
+
+ inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
+ state was inconsistent.
+*/
+
+ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
+/* Return flags indicating compile-time options.
+
+ Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
+ 1.0: size of uInt
+ 3.2: size of uLong
+ 5.4: size of voidpf (pointer)
+ 7.6: size of z_off_t
+
+ Compiler, assembler, and debug options:
+ 8: DEBUG
+ 9: ASMV or ASMINF -- use ASM code
+ 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
+ 11: 0 (reserved)
+
+ One-time table building (smaller code, but not thread-safe if true):
+ 12: BUILDFIXED -- build static block decoding tables when needed
+ 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
+ 14,15: 0 (reserved)
+
+ Library content (indicates missing functionality):
+ 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
+ deflate code when not needed)
+ 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
+ and decode gzip streams (to avoid linking crc code)
+ 18-19: 0 (reserved)
+
+ Operation variations (changes in library functionality):
+ 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
+ 21: FASTEST -- deflate algorithm with only one, lowest compression level
+ 22,23: 0 (reserved)
+
+ The sprintf variant used by gzprintf (zero is best):
+ 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
+ 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
+ 26: 0 = returns value, 1 = void -- 1 means inferred string length returned
+
+ Remainder:
+ 27-31: 0 (reserved)
+ */
+
+#ifndef Z_SOLO
+
+ /* utility functions */
+
+/*
+ The following utility functions are implemented on top of the basic
+ stream-oriented functions. To simplify the interface, some default options
+ are assumed (compression level and memory usage, standard memory allocation
+ functions). The source code of these utility functions can be modified if
+ you need special options.
+*/
+
+ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Compresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed buffer.
+
+ compress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer.
+*/
+
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen,
+ int level));
+/*
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least the value returned by
+ compressBound(sourceLen). Upon exit, destLen is the actual size of the
+ compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+
+ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
+/*
+ compressBound() returns an upper bound on the compressed size after
+ compress() or compress2() on sourceLen bytes. It would be used before a
+ compress() or compress2() call to allocate the destination buffer.
+*/
+
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
+ const Bytef *source, uLong sourceLen));
+/*
+ Decompresses the source buffer into the destination buffer. sourceLen is
+ the byte length of the source buffer. Upon entry, destLen is the total size
+ of the destination buffer, which must be large enough to hold the entire
+ uncompressed data. (The size of the uncompressed data must have been saved
+ previously by the compressor and transmitted to the decompressor by some
+ mechanism outside the scope of this compression library.) Upon exit, destLen
+ is the actual size of the uncompressed buffer.
+
+ uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+ enough memory, Z_BUF_ERROR if there was not enough room in the output
+ buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In
+ the case where there is not enough room, uncompress() will fill the output
+ buffer with the uncompressed data up to that point.
+*/
+
+ /* gzip file access functions */
+
+/*
+ This library supports reading and writing files in gzip (.gz) format with
+ an interface similar to that of stdio, using the functions that start with
+ "gz". The gzip format is different from the zlib format. gzip is a gzip
+ wrapper, documented in RFC 1952, wrapped around a deflate stream.
+*/
+
+typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */
+
+/*
+ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
+
+ Opens a gzip (.gz) file for reading or writing. The mode parameter is as
+ in fopen ("rb" or "wb") but can also include a compression level ("wb9") or
+ a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only
+ compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F'
+ for fixed code compression as in "wb9F". (See the description of
+ deflateInit2 for more information about the strategy parameter.) 'T' will
+ request transparent writing or appending with no compression and not using
+ the gzip format.
+
+ "a" can be used instead of "w" to request that the gzip stream that will
+ be written be appended to the file. "+" will result in an error, since
+ reading and writing to the same gzip file is not supported. The addition of
+ "x" when writing will create the file exclusively, which fails if the file
+ already exists. On systems that support it, the addition of "e" when
+ reading or writing will set the flag to close the file on an execve() call.
+
+ These functions, as well as gzip, will read and decode a sequence of gzip
+ streams in a file. The append function of gzopen() can be used to create
+ such a file. (Also see gzflush() for another way to do this.) When
+ appending, gzopen does not test whether the file begins with a gzip stream,
+ nor does it look for the end of the gzip streams to begin appending. gzopen
+ will simply append a gzip stream to the existing file.
+
+ gzopen can be used to read a file which is not in gzip format; in this
+ case gzread will directly read from the file without decompression. When
+ reading, this will be detected automatically by looking for the magic two-
+ byte gzip header.
+
+ gzopen returns NULL if the file could not be opened, if there was
+ insufficient memory to allocate the gzFile state, or if an invalid mode was
+ specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
+ errno can be checked to determine if the reason gzopen failed was that the
+ file could not be opened.
+*/
+
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
+/*
+ gzdopen associates a gzFile with the file descriptor fd. File descriptors
+ are obtained from calls like open, dup, creat, pipe or fileno (if the file
+ has been previously opened with fopen). The mode parameter is as in gzopen.
+
+ The next call of gzclose on the returned gzFile will also close the file
+ descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
+ fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
+ mode);. The duplicated descriptor should be saved to avoid a leak, since
+ gzdopen does not close fd if it fails. If you are using fileno() to get the
+ file descriptor from a FILE *, then you will have to use dup() to avoid
+ double-close()ing the file descriptor. Both gzclose() and fclose() will
+ close the associated file descriptor, so they need to have different file
+ descriptors.
+
+ gzdopen returns NULL if there was insufficient memory to allocate the
+ gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
+ provided, or '+' was provided), or if fd is -1. The file descriptor is not
+ used until the next gz* read, write, seek, or close operation, so gzdopen
+ will not detect if fd is invalid (unless fd is -1).
+*/
+
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
+/*
+ Set the internal buffer size used by this library's functions. The
+ default buffer size is 8192 bytes. This function must be called after
+ gzopen() or gzdopen(), and before any other calls that read or write the
+ file. The buffer memory allocation is always deferred to the first read or
+ write. Two buffers are allocated, either both of the specified size when
+ writing, or one of the specified size and the other twice that size when
+ reading. A larger buffer size of, for example, 64K or 128K bytes will
+ noticeably increase the speed of decompression (reading).
+
+ The new buffer size also affects the maximum length for gzprintf().
+
+ gzbuffer() returns 0 on success, or -1 on failure, such as being called
+ too late.
+*/
+
+ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
+/*
+ Dynamically update the compression level or strategy. See the description
+ of deflateInit2 for the meaning of these parameters.
+
+ gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
+ opened for writing.
+*/
+
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
+/*
+ Reads the given number of uncompressed bytes from the compressed file. If
+ the input file is not in gzip format, gzread copies the given number of
+ bytes into the buffer directly from the file.
+
+ After reaching the end of a gzip stream in the input, gzread will continue
+ to read, looking for another gzip stream. Any number of gzip streams may be
+ concatenated in the input file, and will all be decompressed by gzread().
+ If something other than a gzip stream is encountered after a gzip stream,
+ that remaining trailing garbage is ignored (and no error is returned).
+
+ gzread can be used to read a gzip file that is being concurrently written.
+ Upon reaching the end of the input, gzread will return with the available
+ data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then
+ gzclearerr can be used to clear the end of file indicator in order to permit
+ gzread to be tried again. Z_OK indicates that a gzip stream was completed
+ on the last gzread. Z_BUF_ERROR indicates that the input file ended in the
+ middle of a gzip stream. Note that gzread does not return -1 in the event
+ of an incomplete gzip stream. This error is deferred until gzclose(), which
+ will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip
+ stream. Alternatively, gzerror can be used before gzclose to detect this
+ case.
+
+ gzread returns the number of uncompressed bytes actually read, less than
+ len for end of file, or -1 for error.
+*/
+
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+ voidpc buf, unsigned len));
+/*
+ Writes the given number of uncompressed bytes into the compressed file.
+ gzwrite returns the number of uncompressed bytes written or 0 in case of
+ error.
+*/
+
+ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...));
+/*
+ Converts, formats, and writes the arguments to the compressed file under
+ control of the format string, as in fprintf. gzprintf returns the number of
+ uncompressed bytes actually written, or 0 in case of error. The number of
+ uncompressed bytes written is limited to 8191, or one less than the buffer
+ size given to gzbuffer(). The caller should assure that this limit is not
+ exceeded. If it is exceeded, then gzprintf() will return an error (0) with
+ nothing written. In this case, there may also be a buffer overflow with
+ unpredictable consequences, which is possible only if zlib was compiled with
+ the insecure functions sprintf() or vsprintf() because the secure snprintf()
+ or vsnprintf() functions were not available. This can be determined using
+ zlibCompileFlags().
+*/
+
+ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
+/*
+ Writes the given null-terminated string to the compressed file, excluding
+ the terminating null character.
+
+ gzputs returns the number of characters written, or -1 in case of error.
+*/
+
+ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
+/*
+ Reads bytes from the compressed file until len-1 characters are read, or a
+ newline character is read and transferred to buf, or an end-of-file
+ condition is encountered. If any characters are read or if len == 1, the
+ string is terminated with a null character. If no characters are read due
+ to an end-of-file or len < 1, then the buffer is left untouched.
+
+ gzgets returns buf which is a null-terminated string, or it returns NULL
+ for end-of-file or in case of error. If there was an error, the contents at
+ buf are indeterminate.
+*/
+
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
+/*
+ Writes c, converted to an unsigned char, into the compressed file. gzputc
+ returns the value that was written, or -1 in case of error.
+*/
+
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
+/*
+ Reads one byte from the compressed file. gzgetc returns this byte or -1
+ in case of end of file or error. This is implemented as a macro for speed.
+ As such, it does not do all of the checking the other functions do. I.e.
+ it does not check to see if file is NULL, nor whether the structure file
+ points to has been clobbered or not.
+*/
+
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
+/*
+ Push one character back onto the stream to be read as the first character
+ on the next read. At least one character of push-back is allowed.
+ gzungetc() returns the character pushed, or -1 on failure. gzungetc() will
+ fail if c is -1, and may fail if a character has been pushed but not read
+ yet. If gzungetc is used immediately after gzopen or gzdopen, at least the
+ output buffer size of pushed characters is allowed. (See gzbuffer above.)
+ The pushed character will be discarded if the stream is repositioned with
+ gzseek() or gzrewind().
+*/
+
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
+/*
+ Flushes all pending output into the compressed file. The parameter flush
+ is as in the deflate() function. The return value is the zlib error number
+ (see function gzerror below). gzflush is only permitted when writing.
+
+ If the flush parameter is Z_FINISH, the remaining data is written and the
+ gzip stream is completed in the output. If gzwrite() is called again, a new
+ gzip stream will be started in the output. gzread() is able to read such
+ concatented gzip streams.
+
+ gzflush should be called only when strictly necessary because it will
+ degrade compression if called too often.
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
+ z_off_t offset, int whence));
+
+ Sets the starting position for the next gzread or gzwrite on the given
+ compressed file. The offset represents a number of bytes in the
+ uncompressed data stream. The whence parameter is defined as in lseek(2);
+ the value SEEK_END is not supported.
+
+ If the file is opened for reading, this function is emulated but can be
+ extremely slow. If the file is opened for writing, only forward seeks are
+ supported; gzseek then compresses a sequence of zeroes up to the new
+ starting position.
+
+ gzseek returns the resulting offset location as measured in bytes from
+ the beginning of the uncompressed stream, or -1 in case of error, in
+ particular if the file is opened for writing and the new starting position
+ would be before the current position.
+*/
+
+ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
+/*
+ Rewinds the given file. This function is supported only for reading.
+
+ gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
+
+ Returns the starting position for the next gzread or gzwrite on the given
+ compressed file. This position represents a number of bytes in the
+ uncompressed data stream, and is zero when starting, even if appending or
+ reading a gzip stream from the middle of a file using gzdopen().
+
+ gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
+/*
+ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
+
+ Returns the current offset in the file being read or written. This offset
+ includes the count of bytes that precede the gzip stream, for example when
+ appending or when using gzdopen() for reading. When reading, the offset
+ does not include as yet unused buffered input. This information can be used
+ for a progress indicator. On error, gzoffset() returns -1.
+*/
+
+ZEXTERN int ZEXPORT gzeof OF((gzFile file));
+/*
+ Returns true (1) if the end-of-file indicator has been set while reading,
+ false (0) otherwise. Note that the end-of-file indicator is set only if the
+ read tried to go past the end of the input, but came up short. Therefore,
+ just like feof(), gzeof() may return false even if there is no more data to
+ read, in the event that the last read request was for the exact number of
+ bytes remaining in the input file. This will happen if the input file size
+ is an exact multiple of the buffer size.
+
+ If gzeof() returns true, then the read functions will return no more data,
+ unless the end-of-file indicator is reset by gzclearerr() and the input file
+ has grown since the previous end of file was detected.
+*/
+
+ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
+/*
+ Returns true (1) if file is being copied directly while reading, or false
+ (0) if file is a gzip stream being decompressed.
+
+ If the input file is empty, gzdirect() will return true, since the input
+ does not contain a gzip stream.
+
+ If gzdirect() is used immediately after gzopen() or gzdopen() it will
+ cause buffers to be allocated to allow reading the file to determine if it
+ is a gzip file. Therefore if gzbuffer() is used, it should be called before
+ gzdirect().
+
+ When writing, gzdirect() returns true (1) if transparent writing was
+ requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note:
+ gzdirect() is not needed when writing. Transparent writing must be
+ explicitly requested, so the application already knows the answer. When
+ linking statically, using gzdirect() will include all of the zlib code for
+ gzip file reading and decompression, which may not be desired.)
+*/
+
+ZEXTERN int ZEXPORT gzclose OF((gzFile file));
+/*
+ Flushes all pending output if necessary, closes the compressed file and
+ deallocates the (de)compression state. Note that once file is closed, you
+ cannot call gzerror with file, since its structures have been deallocated.
+ gzclose must not be called more than once on the same file, just as free
+ must not be called more than once on the same allocation.
+
+ gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
+ file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
+ last read ended in the middle of a gzip stream, or Z_OK on success.
+*/
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+/*
+ Same as gzclose(), but gzclose_r() is only for use when reading, and
+ gzclose_w() is only for use when writing or appending. The advantage to
+ using these instead of gzclose() is that they avoid linking in zlib
+ compression or decompression code that is not used when only reading or only
+ writing respectively. If gzclose() is used, then both compression and
+ decompression code will be included the application when linking to a static
+ zlib library.
+*/
+
+ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
+/*
+ Returns the error message for the last error which occurred on the given
+ compressed file. errnum is set to zlib error number. If an error occurred
+ in the file system and not in the compression library, errnum is set to
+ Z_ERRNO and the application may consult errno to get the exact error code.
+
+ The application must not modify the returned string. Future calls to
+ this function may invalidate the previously returned string. If file is
+ closed, then the string previously returned by gzerror will no longer be
+ available.
+
+ gzerror() should be used to distinguish errors from end-of-file for those
+ functions above that do not distinguish those cases in their return values.
+*/
+
+ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
+/*
+ Clears the error and end-of-file flags for file. This is analogous to the
+ clearerr() function in stdio. This is useful for continuing to read a gzip
+ file that is being written concurrently.
+*/
+
+#endif /* !Z_SOLO */
+
+ /* checksum functions */
+
+/*
+ These functions are not related to compression but are exported
+ anyway because they might be useful in applications using the compression
+ library.
+*/
+
+ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+/*
+ Update a running Adler-32 checksum with the bytes buf[0..len-1] and
+ return the updated checksum. If buf is Z_NULL, this function returns the
+ required initial value for the checksum.
+
+ An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+ much faster.
+
+ Usage example:
+
+ uLong adler = adler32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ adler = adler32(adler, buffer, length);
+ }
+ if (adler != original_adler) error();
+*/
+
+/*
+ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
+ z_off_t len2));
+
+ Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
+ and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
+ each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
+ seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note
+ that the z_off_t type (like off_t) is a signed integer. If len2 is
+ negative, the result has no meaning or utility.
+*/
+
+ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
+/*
+ Update a running CRC-32 with the bytes buf[0..len-1] and return the
+ updated CRC-32. If buf is Z_NULL, this function returns the required
+ initial value for the crc. Pre- and post-conditioning (one's complement) is
+ performed within this function so it shouldn't be done by the application.
+
+ Usage example:
+
+ uLong crc = crc32(0L, Z_NULL, 0);
+
+ while (read_buffer(buffer, length) != EOF) {
+ crc = crc32(crc, buffer, length);
+ }
+ if (crc != original_crc) error();
+*/
+
+/*
+ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
+
+ Combine two CRC-32 check values into one. For two sequences of bytes,
+ seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
+ calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
+ check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
+ len2.
+*/
+
+
+ /* various hacks, don't look :) */
+
+/* deflateInit and inflateInit are macros to allow checking the zlib version
+ * and the compiler's view of z_stream:
+ */
+ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
+ int windowBits, int memLevel,
+ int strategy, const char *version,
+ int stream_size));
+ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
+ const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
+ unsigned char FAR *window,
+ const char *version,
+ int stream_size));
+#define deflateInit(strm, level) \
+ deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
+#define inflateInit(strm) \
+ inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream))
+#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+ deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+ (strategy), ZLIB_VERSION, (int)sizeof(z_stream))
+#define inflateInit2(strm, windowBits) \
+ inflateInit2_((strm), (windowBits), ZLIB_VERSION, \
+ (int)sizeof(z_stream))
+#define inflateBackInit(strm, windowBits, window) \
+ inflateBackInit_((strm), (windowBits), (window), \
+ ZLIB_VERSION, (int)sizeof(z_stream))
+
+#ifndef Z_SOLO
+
+/* gzgetc() macro and its supporting function and exposed data structure. Note
+ * that the real internal state is much larger than the exposed structure.
+ * This abbreviated structure exposes just enough for the gzgetc() macro. The
+ * user should not mess with these exposed elements, since their names or
+ * behavior could change in the future, perhaps even capriciously. They can
+ * only be used by the gzgetc() macro. You have been warned.
+ */
+struct gzFile_s {
+ unsigned have;
+ unsigned char *next;
+ z_off64_t pos;
+};
+ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */
+#ifdef Z_PREFIX_SET
+# undef z_gzgetc
+# define z_gzgetc(g) \
+ ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
+#else
+# define gzgetc(g) \
+ ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g))
+#endif
+
+/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
+ * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
+ * both are true, the application gets the *64 functions, and the regular
+ * functions are changed to 64 bits) -- in case these are set on systems
+ * without large file support, _LFS64_LARGEFILE must also be true
+ */
+#ifdef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+ ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64)
+# ifdef Z_PREFIX_SET
+# define z_gzopen z_gzopen64
+# define z_gzseek z_gzseek64
+# define z_gztell z_gztell64
+# define z_gzoffset z_gzoffset64
+# define z_adler32_combine z_adler32_combine64
+# define z_crc32_combine z_crc32_combine64
+# else
+# define gzopen gzopen64
+# define gzseek gzseek64
+# define gztell gztell64
+# define gzoffset gzoffset64
+# define adler32_combine adler32_combine64
+# define crc32_combine crc32_combine64
+# endif
+# ifndef Z_LARGE64
+ ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+ ZEXTERN off64_t ZEXPORT gzseek64 OF((gzFile, off64_t, int));
+ ZEXTERN off64_t ZEXPORT gztell64 OF((gzFile));
+ ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, off64_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, off64_t));
+# endif
+#else
+ ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+ ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+ ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+ ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
+
+#else /* Z_SOLO */
+
+ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+
+#endif /* !Z_SOLO */
+
+/* hack for buggy compilers */
+#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
+ struct internal_state {int dummy;};
+#endif
+
+/* undocumented functions */
+ZEXTERN const char * ZEXPORT zError OF((int));
+ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp));
+ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void));
+ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int));
+ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp));
+ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp));
+#if defined(_WIN32) && !defined(Z_SOLO)
+ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path,
+ const char *mode));
+#endif
+#if defined(STDC) || defined(Z_HAVE_STDARG_H)
+# ifndef Z_SOLO
+ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file,
+ const char *format,
+ va_list va));
+# endif
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ZLIB_H */
diff --git a/core/src/fxcodec/fx_zlib/zlib_v128/zutil.h b/core/src/fxcodec/fx_zlib/zlib_v128/zutil.h
new file mode 100644
index 0000000000..582d1b5f3c
--- /dev/null
+++ b/core/src/fxcodec/fx_zlib/zlib_v128/zutil.h
@@ -0,0 +1,255 @@
+/* zutil.h -- internal interface and configuration of the compression library
+ * Copyright (C) 1995-2013 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+ part of the implementation of the compression library and is
+ subject to change. Applications should only use zlib.h.
+ */
+
+/* @(#) $Id$ */
+
+#ifndef ZUTIL_H
+#define ZUTIL_H
+
+#include "../../../../include/fxcrt/fx_system.h" /* XYQ 2007-5-22 */
+
+#ifdef HAVE_HIDDEN
+# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+# define ZLIB_INTERNAL
+#endif
+
+#include "zlib.h"
+
+#if defined(STDC) && !defined(Z_SOLO)
+# if !(defined(_WIN32_WCE) && defined(_MSC_VER))
+# include <stddef.h>
+# endif
+# include <string.h>
+# include <stdlib.h>
+#endif
+
+#ifdef Z_SOLO
+ typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */
+#endif
+
+#ifndef local
+# define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+typedef unsigned char uch;
+typedef uch FAR uchf;
+typedef unsigned short ush;
+typedef ush FAR ushf;
+typedef unsigned long ulg;
+
+extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
+/* (size given to avoid silly warnings with Visual C++) */
+
+#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
+
+#define ERR_RETURN(strm,err) \
+ return (strm->msg = ERR_MSG(err), (err))
+/* To be used only when the state is known to be valid */
+
+ /* common constants */
+
+#ifndef DEF_WBITS
+# define DEF_WBITS MAX_WBITS
+#endif
+/* default windowBits for decompression. MAX_WBITS is for compression only */
+
+#if MAX_MEM_LEVEL >= 8
+# define DEF_MEM_LEVEL 8
+#else
+# define DEF_MEM_LEVEL MAX_MEM_LEVEL
+#endif
+/* default memLevel */
+
+#define STORED_BLOCK 0
+#define STATIC_TREES 1
+#define DYN_TREES 2
+/* The three kinds of block type */
+
+#define MIN_MATCH 3
+#define MAX_MATCH 258
+/* The minimum and maximum match lengths */
+
+#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
+
+ /* target dependencies */
+
+#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
+# define OS_CODE 0x00
+# ifndef Z_SOLO
+# if defined(__TURBOC__) || defined(__BORLANDC__)
+# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
+ /* Allow compilation with ANSI keywords only enabled */
+ void _Cdecl farfree( void *block );
+ void *_Cdecl farmalloc( unsigned long nbytes );
+# else
+# include <alloc.h>
+# endif
+# else /* MSC or DJGPP */
+# include <malloc.h>
+# endif
+# endif
+#endif
+
+#ifdef AMIGA
+# define OS_CODE 0x01
+#endif
+
+#if defined(VAXC) || defined(VMS)
+# define OS_CODE 0x02
+# define F_OPEN(name, mode) \
+ FXSYS_fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
+#endif
+
+#if defined(ATARI) || defined(atarist)
+# define OS_CODE 0x05
+#endif
+
+#ifdef OS2
+# define OS_CODE 0x06
+# if defined(M_I86) && !defined(Z_SOLO)
+# include <malloc.h>
+# endif
+#endif
+
+#if defined(MACOS) || defined(TARGET_OS_MAC)
+# define OS_CODE 0x07
+# ifndef Z_SOLO
+# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
+# include <unix.h> /* for fdopen */
+# else
+# ifndef fdopen
+# define fdopen(fd,mode) NULL /* No fdopen() */
+# endif
+# endif
+# endif
+#endif
+
+#ifdef TOPS20
+# define OS_CODE 0x0a
+#endif
+
+#ifdef WIN32
+# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
+# define OS_CODE 0x0b
+# endif
+#endif
+
+#ifdef __50SERIES /* Prime/PRIMOS */
+# define OS_CODE 0x0f
+#endif
+
+#if defined(_BEOS_) || defined(RISCOS)
+# define fdopen(fd,mode) NULL /* No fdopen() */
+#endif
+
+#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
+# if defined(_WIN32_WCE)
+# define fdopen(fd,mode) NULL /* No fdopen() */
+# ifndef _PTRDIFF_T_DEFINED
+ typedef int ptrdiff_t;
+# define _PTRDIFF_T_DEFINED
+# endif
+# else
+# define fdopen(fd,type) _fdopen(fd,type)
+# endif
+#endif
+
+#if defined(__BORLANDC__) && !defined(MSDOS)
+ #pragma warn -8004
+ #pragma warn -8008
+ #pragma warn -8066
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_WIN32) && \
+ (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
+ ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+ ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#endif
+
+ /* common defaults */
+
+#ifndef OS_CODE
+# define OS_CODE 0x03 /* assume Unix */
+#endif
+
+#ifndef F_OPEN
+# define F_OPEN(name, mode) FXSYS_fopen((name), (mode))
+#endif
+
+ /* functions */
+
+#if defined(pyr) || defined(Z_SOLO)
+# define NO_MEMCPY
+#endif
+#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
+ /* Use our own functions for small and medium model with MSC <= 5.0.
+ * You may have to use the same strategy for Borland C (untested).
+ * The __SC__ check is for Symantec.
+ */
+# define NO_MEMCPY
+#endif
+#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
+# define HAVE_MEMCPY
+#endif
+#ifdef HAVE_MEMCPY
+# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
+# define zmemcpy _fmemcpy
+# define zmemcmp _fmemcmp
+# define zmemzero(dest, len) _fmemset(dest, 0, len)
+# else
+# define zmemcpy FXSYS_memcpy32
+# define zmemcmp FXSYS_memcmp32
+# define zmemzero(dest, len) FXSYS_memset32(dest, 0, len)
+# endif
+#else
+ void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
+ int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
+ void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
+#endif
+
+/* Diagnostic functions */
+#ifdef DEBUG
+# include <stdio.h>
+ extern int ZLIB_INTERNAL z_verbose;
+ extern void ZLIB_INTERNAL z_error OF((char *m));
+# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
+# define Trace(x) {if (z_verbose>=0) FXSYS_fprintf x ;}
+# define Tracev(x) {if (z_verbose>0) FXSYS_fprintf x ;}
+# define Tracevv(x) {if (z_verbose>1) FXSYS_fprintf x ;}
+# define Tracec(c,x) {if (z_verbose>0 && (c)) FXSYS_fprintf x ;}
+# define Tracecv(c,x) {if (z_verbose>1 && (c)) FXSYS_fprintf x ;}
+#else
+# define Assert(cond,msg)
+# define Trace(x)
+# define Tracev(x)
+# define Tracevv(x)
+# define Tracec(c,x)
+# define Tracecv(c,x)
+#endif
+
+#ifndef Z_SOLO
+ voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
+ unsigned size));
+ void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
+#endif
+
+#define ZALLOC(strm, items, size) \
+ (*((strm)->zalloc))((strm)->opaque, (items), (size))
+#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
+#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
+
+/* Reverse the bytes in a 32-bit value */
+#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+ (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+
+#endif /* ZUTIL_H */
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
new file mode 100644
index 0000000000..1664257411
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
new file mode 100644
index 0000000000..bb4e0bba9f
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
new file mode 100644
index 0000000000..ad4656f54d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithQe.h b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
new file mode 100644
index 0000000000..17bc18aa03
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.h b/core/src/fxcodec/jbig2/JBig2_BitStream.h
new file mode 100644
index 0000000000..8ed473a150
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_BitStream.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp
new file mode 100644
index 0000000000..856c0c3bda
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp
@@ -0,0 +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);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h
new file mode 100644
index 0000000000..369dba3512
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Context.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Define.h b/core/src/fxcodec/jbig2/JBig2_Define.h
new file mode 100644
index 0000000000..1019b6af5d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Define.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
new file mode 100644
index 0000000000..9aa714a228
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
new file mode 100644
index 0000000000..a89d0d62ef
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
new file mode 100644
index 0000000000..8199fe7c21
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
new file mode 100644
index 0000000000..38b2021d20
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
new file mode 100644
index 0000000000..af4a54988d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
new file mode 100644
index 0000000000..df051c28d4
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
new file mode 100644
index 0000000000..f6114364f1
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.cpp b/core/src/fxcodec/jbig2/JBig2_Image.cpp
new file mode 100644
index 0000000000..1719028176
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Image.cpp
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.h b/core/src/fxcodec/jbig2/JBig2_Image.h
new file mode 100644
index 0000000000..3c69f647e3
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Image.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_List.h b/core/src/fxcodec/jbig2/JBig2_List.h
new file mode 100644
index 0000000000..a737d9e112
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_List.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Module.h b/core/src/fxcodec/jbig2/JBig2_Module.h
new file mode 100644
index 0000000000..ccde5ade02
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Module.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.cpp b/core/src/fxcodec/jbig2/JBig2_Object.cpp
new file mode 100644
index 0000000000..3a94f0e65a
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Object.cpp
@@ -0,0 +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);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.h b/core/src/fxcodec/jbig2/JBig2_Object.h
new file mode 100644
index 0000000000..7fef1a21a9
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Object.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Page.h b/core/src/fxcodec/jbig2/JBig2_Page.h
new file mode 100644
index 0000000000..490705448c
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Page.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
new file mode 100644
index 0000000000..58c52a5848
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
@@ -0,0 +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);
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.h b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
new file mode 100644
index 0000000000..c8686507d9
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.cpp b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
new file mode 100644
index 0000000000..788207dc6e
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
@@ -0,0 +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);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.h b/core/src/fxcodec/jbig2/JBig2_Segment.h
new file mode 100644
index 0000000000..48c31b17bb
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
new file mode 100644
index 0000000000..8516c045c6
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
@@ -0,0 +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);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
new file mode 100644
index 0000000000..cb97a167d1
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/lcms2/include/fx_lcms2.h b/core/src/fxcodec/lcms2/include/fx_lcms2.h
new file mode 100644
index 0000000000..53038322d3
--- /dev/null
+++ b/core/src/fxcodec/lcms2/include/fx_lcms2.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h b/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h
new file mode 100644
index 0000000000..1103b7f698
--- /dev/null
+++ b/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2.h b/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2.h
new file mode 100644
index 0000000000..8595f70203
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2.h
@@ -0,0 +1,1882 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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.
+//
+//---------------------------------------------------------------------------------
+//
+// Version 2.6
+//
+
+#ifndef _lcms2_H
+
+// ********** Configuration toggles ****************************************
+
+// Uncomment this one if you are using big endian machines
+// #define CMS_USE_BIG_ENDIAN 1
+
+// Uncomment this one if your compiler/machine does NOT support the
+// "long long" type.
+// #define CMS_DONT_USE_INT64 1
+
+// Uncomment this if your compiler doesn't work with fast floor function
+// #define CMS_DONT_USE_FAST_FLOOR 1
+
+// Uncomment this line if you want lcms to use the black point tag in profile,
+// if commented, lcms will compute the black point by its own.
+// It is safer to leave it commented out
+// #define CMS_USE_PROFILE_BLACK_POINT_TAG 1
+
+// Uncomment this line if you are compiling as C++ and want a C++ API
+// #define CMS_USE_CPP_API
+
+// Uncomment this line if you need strict CGATS syntax. Makes CGATS files to
+// require "KEYWORD" on undefined identifiers, keep it comented out unless needed
+// #define CMS_STRICT_CGATS 1
+
+// Uncomment to get rid of the tables for "half" float support
+// #define CMS_NO_HALF_SUPPORT 1
+
+// Uncomment to get rid of pthreads/windows dependency
+// #define CMS_NO_PTHREADS 1
+
+// ********** End of configuration toggles ******************************
+
+// Needed for streams
+#include <stdio.h>
+
+// Needed for portability (C99 per 7.1.2)
+#include <limits.h>
+#include <time.h>
+#include <stddef.h>
+
+#ifndef CMS_USE_CPP_API
+# ifdef __cplusplus
+extern "C" {
+# endif
+#endif
+
+// Version/release
+#define LCMS_VERSION 2060
+
+// I will give the chance of redefining basic types for compilers that are not fully C99 compliant
+#ifndef CMS_BASIC_TYPES_ALREADY_DEFINED
+
+// Base types
+typedef unsigned char cmsUInt8Number; // That is guaranteed by the C99 spec
+typedef signed char cmsInt8Number; // That is guaranteed by the C99 spec
+
+#if CHAR_BIT != 8
+# error "Unable to find 8 bit type, unsupported compiler"
+#endif
+
+// IEEE float storage numbers
+typedef float cmsFloat32Number;
+typedef double cmsFloat64Number;
+
+// 16-bit base types
+#if (USHRT_MAX == 65535U)
+ typedef unsigned short cmsUInt16Number;
+#elif (UINT_MAX == 65535U)
+ typedef unsigned int cmsUInt16Number;
+#else
+# error "Unable to find 16 bits unsigned type, unsupported compiler"
+#endif
+
+#if (SHRT_MAX == 32767)
+ typedef short cmsInt16Number;
+#elif (INT_MAX == 32767)
+ typedef int cmsInt16Number;
+#else
+# error "Unable to find 16 bits signed type, unsupported compiler"
+#endif
+
+// 32-bit base type
+#if (UINT_MAX == 4294967295U)
+ typedef unsigned int cmsUInt32Number;
+#elif (ULONG_MAX == 4294967295U)
+ typedef unsigned long cmsUInt32Number;
+#else
+# error "Unable to find 32 bit unsigned type, unsupported compiler"
+#endif
+
+#if (INT_MAX == +2147483647)
+ typedef int cmsInt32Number;
+#elif (LONG_MAX == +2147483647)
+ typedef long cmsInt32Number;
+#else
+# error "Unable to find 32 bit signed type, unsupported compiler"
+#endif
+
+// 64-bit base types
+#ifndef CMS_DONT_USE_INT64
+# if (ULONG_MAX == 18446744073709551615U)
+ typedef unsigned long cmsUInt64Number;
+# elif (ULLONG_MAX == 18446744073709551615U)
+ typedef unsigned long long cmsUInt64Number;
+# else
+# define CMS_DONT_USE_INT64 1
+# endif
+# if (LONG_MAX == +9223372036854775807)
+ typedef long cmsInt64Number;
+# elif (LLONG_MAX == +9223372036854775807)
+ typedef long long cmsInt64Number;
+# else
+# define CMS_DONT_USE_INT64 1
+# endif
+#endif
+#endif
+
+// In the case 64 bit numbers are not supported by the compiler
+#ifdef CMS_DONT_USE_INT64
+ typedef cmsUInt32Number cmsUInt64Number[2];
+ typedef cmsInt32Number cmsInt64Number[2];
+#endif
+
+// Derivative types
+typedef cmsUInt32Number cmsSignature;
+typedef cmsUInt16Number cmsU8Fixed8Number;
+typedef cmsInt32Number cmsS15Fixed16Number;
+typedef cmsUInt32Number cmsU16Fixed16Number;
+
+// Boolean type, which will be using the native integer
+typedef int cmsBool;
+
+// Try to detect windows
+#if defined (_WIN32) || defined(_WIN64) || defined(WIN32) || defined(_WIN32_)
+# define CMS_IS_WINDOWS_ 1
+#endif
+
+#ifdef _MSC_VER
+# define CMS_IS_WINDOWS_ 1
+#endif
+
+#ifdef __BORLANDC__
+# define CMS_IS_WINDOWS_ 1
+#endif
+
+// Try to detect big endian platforms. This list can be endless, so only some checks are performed over here.
+// you can pass this toggle to the compiler by using -DCMS_USE_BIG_ENDIAN or something similar
+
+#if defined(__sgi__) || defined(__sgi) || defined(sparc)
+# define CMS_USE_BIG_ENDIAN 1
+#endif
+
+#if defined(__s390__) || defined(__s390x__)
+# define CMS_USE_BIG_ENDIAN 1
+#endif
+
+# ifdef TARGET_CPU_PPC
+# if TARGET_CPU_PPC
+# define CMS_USE_BIG_ENDIAN 1
+# endif
+# endif
+
+#if defined(__powerpc__) || defined(__ppc__) || defined(TARGET_CPU_PPC)
+# define CMS_USE_BIG_ENDIAN 1
+# if defined (__GNUC__) && defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN)
+# if __BYTE_ORDER == __LITTLE_ENDIAN
+// // Don't use big endian for PowerPC little endian mode
+# undef CMS_USE_BIG_ENDIAN
+# endif
+# endif
+#endif
+
+// WORDS_BIGENDIAN takes precedence
+#if defined(_HOST_BIG_ENDIAN) || defined(__BIG_ENDIAN__) || defined(WORDS_BIGENDIAN)
+# define CMS_USE_BIG_ENDIAN 1
+#endif
+
+#ifdef macintosh
+# ifdef __BIG_ENDIAN__
+# define CMS_USE_BIG_ENDIAN 1
+# endif
+# ifdef __LITTLE_ENDIAN__
+# undef CMS_USE_BIG_ENDIAN
+# endif
+#endif
+
+// Calling convention -- this is hardly platform and compiler dependent
+#ifdef CMS_IS_WINDOWS_
+# if defined(CMS_DLL) || defined(CMS_DLL_BUILD)
+# ifdef __BORLANDC__
+# define CMSEXPORT __stdcall _export
+# define CMSAPI
+# else
+# define CMSEXPORT _stdcall
+# ifdef CMS_DLL_BUILD
+# define CMSAPI __declspec(dllexport)
+# else
+# define CMSAPI __declspec(dllimport)
+# endif
+# endif
+# else
+# define CMSEXPORT
+# define CMSAPI
+# endif
+#else
+# define CMSEXPORT
+# define CMSAPI
+#endif
+
+#ifdef HasTHREADS
+# if HasTHREADS == 1
+# undef CMS_NO_PTHREADS
+# else
+# define CMS_NO_PTHREADS 1
+# endif
+#endif
+
+// Some common definitions
+#define cmsMAX_PATH 256
+
+#ifndef FALSE
+# define FALSE 0
+#endif
+#ifndef TRUE
+# define TRUE 1
+#endif
+
+// D50 XYZ normalized to Y=1.0
+#define cmsD50X 0.9642
+#define cmsD50Y 1.0
+#define cmsD50Z 0.8249
+
+// V4 perceptual black
+#define cmsPERCEPTUAL_BLACK_X 0.00336
+#define cmsPERCEPTUAL_BLACK_Y 0.0034731
+#define cmsPERCEPTUAL_BLACK_Z 0.00287
+
+// Definitions in ICC spec
+#define cmsMagicNumber 0x61637370 // 'acsp'
+#define lcmsSignature 0x6c636d73 // 'lcms'
+
+
+// Base ICC type definitions
+typedef enum {
+ cmsSigChromaticityType = 0x6368726D, // 'chrm'
+ cmsSigColorantOrderType = 0x636C726F, // 'clro'
+ cmsSigColorantTableType = 0x636C7274, // 'clrt'
+ cmsSigCrdInfoType = 0x63726469, // 'crdi'
+ cmsSigCurveType = 0x63757276, // 'curv'
+ cmsSigDataType = 0x64617461, // 'data'
+ cmsSigDictType = 0x64696374, // 'dict'
+ cmsSigDateTimeType = 0x6474696D, // 'dtim'
+ cmsSigDeviceSettingsType = 0x64657673, // 'devs'
+ cmsSigLut16Type = 0x6d667432, // 'mft2'
+ cmsSigLut8Type = 0x6d667431, // 'mft1'
+ cmsSigLutAtoBType = 0x6d414220, // 'mAB '
+ cmsSigLutBtoAType = 0x6d424120, // 'mBA '
+ cmsSigMeasurementType = 0x6D656173, // 'meas'
+ cmsSigMultiLocalizedUnicodeType = 0x6D6C7563, // 'mluc'
+ cmsSigMultiProcessElementType = 0x6D706574, // 'mpet'
+ cmsSigNamedColorType = 0x6E636f6C, // 'ncol' -- DEPRECATED!
+ cmsSigNamedColor2Type = 0x6E636C32, // 'ncl2'
+ cmsSigParametricCurveType = 0x70617261, // 'para'
+ cmsSigProfileSequenceDescType = 0x70736571, // 'pseq'
+ cmsSigProfileSequenceIdType = 0x70736964, // 'psid'
+ cmsSigResponseCurveSet16Type = 0x72637332, // 'rcs2'
+ cmsSigS15Fixed16ArrayType = 0x73663332, // 'sf32'
+ cmsSigScreeningType = 0x7363726E, // 'scrn'
+ cmsSigSignatureType = 0x73696720, // 'sig '
+ cmsSigTextType = 0x74657874, // 'text'
+ cmsSigTextDescriptionType = 0x64657363, // 'desc'
+ cmsSigU16Fixed16ArrayType = 0x75663332, // 'uf32'
+ cmsSigUcrBgType = 0x62666420, // 'bfd '
+ cmsSigUInt16ArrayType = 0x75693136, // 'ui16'
+ cmsSigUInt32ArrayType = 0x75693332, // 'ui32'
+ cmsSigUInt64ArrayType = 0x75693634, // 'ui64'
+ cmsSigUInt8ArrayType = 0x75693038, // 'ui08'
+ cmsSigVcgtType = 0x76636774, // 'vcgt'
+ cmsSigViewingConditionsType = 0x76696577, // 'view'
+ cmsSigXYZType = 0x58595A20 // 'XYZ '
+
+
+} cmsTagTypeSignature;
+
+// Base ICC tag definitions
+typedef enum {
+ cmsSigAToB0Tag = 0x41324230, // 'A2B0'
+ cmsSigAToB1Tag = 0x41324231, // 'A2B1'
+ cmsSigAToB2Tag = 0x41324232, // 'A2B2'
+ cmsSigBlueColorantTag = 0x6258595A, // 'bXYZ'
+ cmsSigBlueMatrixColumnTag = 0x6258595A, // 'bXYZ'
+ cmsSigBlueTRCTag = 0x62545243, // 'bTRC'
+ cmsSigBToA0Tag = 0x42324130, // 'B2A0'
+ cmsSigBToA1Tag = 0x42324131, // 'B2A1'
+ cmsSigBToA2Tag = 0x42324132, // 'B2A2'
+ cmsSigCalibrationDateTimeTag = 0x63616C74, // 'calt'
+ cmsSigCharTargetTag = 0x74617267, // 'targ'
+ cmsSigChromaticAdaptationTag = 0x63686164, // 'chad'
+ cmsSigChromaticityTag = 0x6368726D, // 'chrm'
+ cmsSigColorantOrderTag = 0x636C726F, // 'clro'
+ cmsSigColorantTableTag = 0x636C7274, // 'clrt'
+ cmsSigColorantTableOutTag = 0x636C6F74, // 'clot'
+ cmsSigColorimetricIntentImageStateTag = 0x63696973, // 'ciis'
+ cmsSigCopyrightTag = 0x63707274, // 'cprt'
+ cmsSigCrdInfoTag = 0x63726469, // 'crdi'
+ cmsSigDataTag = 0x64617461, // 'data'
+ cmsSigDateTimeTag = 0x6474696D, // 'dtim'
+ cmsSigDeviceMfgDescTag = 0x646D6E64, // 'dmnd'
+ cmsSigDeviceModelDescTag = 0x646D6464, // 'dmdd'
+ cmsSigDeviceSettingsTag = 0x64657673, // 'devs'
+ cmsSigDToB0Tag = 0x44324230, // 'D2B0'
+ cmsSigDToB1Tag = 0x44324231, // 'D2B1'
+ cmsSigDToB2Tag = 0x44324232, // 'D2B2'
+ cmsSigDToB3Tag = 0x44324233, // 'D2B3'
+ cmsSigBToD0Tag = 0x42324430, // 'B2D0'
+ cmsSigBToD1Tag = 0x42324431, // 'B2D1'
+ cmsSigBToD2Tag = 0x42324432, // 'B2D2'
+ cmsSigBToD3Tag = 0x42324433, // 'B2D3'
+ cmsSigGamutTag = 0x67616D74, // 'gamt'
+ cmsSigGrayTRCTag = 0x6b545243, // 'kTRC'
+ cmsSigGreenColorantTag = 0x6758595A, // 'gXYZ'
+ cmsSigGreenMatrixColumnTag = 0x6758595A, // 'gXYZ'
+ cmsSigGreenTRCTag = 0x67545243, // 'gTRC'
+ cmsSigLuminanceTag = 0x6C756d69, // 'lumi'
+ cmsSigMeasurementTag = 0x6D656173, // 'meas'
+ cmsSigMediaBlackPointTag = 0x626B7074, // 'bkpt'
+ cmsSigMediaWhitePointTag = 0x77747074, // 'wtpt'
+ cmsSigNamedColorTag = 0x6E636f6C, // 'ncol' // Deprecated by the ICC
+ cmsSigNamedColor2Tag = 0x6E636C32, // 'ncl2'
+ cmsSigOutputResponseTag = 0x72657370, // 'resp'
+ cmsSigPerceptualRenderingIntentGamutTag = 0x72696730, // 'rig0'
+ cmsSigPreview0Tag = 0x70726530, // 'pre0'
+ cmsSigPreview1Tag = 0x70726531, // 'pre1'
+ cmsSigPreview2Tag = 0x70726532, // 'pre2'
+ cmsSigProfileDescriptionTag = 0x64657363, // 'desc'
+ cmsSigProfileDescriptionMLTag = 0x6473636d, // 'dscm'
+ cmsSigProfileSequenceDescTag = 0x70736571, // 'pseq'
+ cmsSigProfileSequenceIdTag = 0x70736964, // 'psid'
+ cmsSigPs2CRD0Tag = 0x70736430, // 'psd0'
+ cmsSigPs2CRD1Tag = 0x70736431, // 'psd1'
+ cmsSigPs2CRD2Tag = 0x70736432, // 'psd2'
+ cmsSigPs2CRD3Tag = 0x70736433, // 'psd3'
+ cmsSigPs2CSATag = 0x70733273, // 'ps2s'
+ cmsSigPs2RenderingIntentTag = 0x70733269, // 'ps2i'
+ cmsSigRedColorantTag = 0x7258595A, // 'rXYZ'
+ cmsSigRedMatrixColumnTag = 0x7258595A, // 'rXYZ'
+ cmsSigRedTRCTag = 0x72545243, // 'rTRC'
+ cmsSigSaturationRenderingIntentGamutTag = 0x72696732, // 'rig2'
+ cmsSigScreeningDescTag = 0x73637264, // 'scrd'
+ cmsSigScreeningTag = 0x7363726E, // 'scrn'
+ cmsSigTechnologyTag = 0x74656368, // 'tech'
+ cmsSigUcrBgTag = 0x62666420, // 'bfd '
+ cmsSigViewingCondDescTag = 0x76756564, // 'vued'
+ cmsSigViewingConditionsTag = 0x76696577, // 'view'
+ cmsSigVcgtTag = 0x76636774, // 'vcgt'
+ cmsSigMetaTag = 0x6D657461 // 'meta'
+
+} cmsTagSignature;
+
+
+// ICC Technology tag
+typedef enum {
+ cmsSigDigitalCamera = 0x6463616D, // 'dcam'
+ cmsSigFilmScanner = 0x6673636E, // 'fscn'
+ cmsSigReflectiveScanner = 0x7273636E, // 'rscn'
+ cmsSigInkJetPrinter = 0x696A6574, // 'ijet'
+ cmsSigThermalWaxPrinter = 0x74776178, // 'twax'
+ cmsSigElectrophotographicPrinter = 0x6570686F, // 'epho'
+ cmsSigElectrostaticPrinter = 0x65737461, // 'esta'
+ cmsSigDyeSublimationPrinter = 0x64737562, // 'dsub'
+ cmsSigPhotographicPaperPrinter = 0x7270686F, // 'rpho'
+ cmsSigFilmWriter = 0x6670726E, // 'fprn'
+ cmsSigVideoMonitor = 0x7669646D, // 'vidm'
+ cmsSigVideoCamera = 0x76696463, // 'vidc'
+ cmsSigProjectionTelevision = 0x706A7476, // 'pjtv'
+ cmsSigCRTDisplay = 0x43525420, // 'CRT '
+ cmsSigPMDisplay = 0x504D4420, // 'PMD '
+ cmsSigAMDisplay = 0x414D4420, // 'AMD '
+ cmsSigPhotoCD = 0x4B504344, // 'KPCD'
+ cmsSigPhotoImageSetter = 0x696D6773, // 'imgs'
+ cmsSigGravure = 0x67726176, // 'grav'
+ cmsSigOffsetLithography = 0x6F666673, // 'offs'
+ cmsSigSilkscreen = 0x73696C6B, // 'silk'
+ cmsSigFlexography = 0x666C6578, // 'flex'
+ cmsSigMotionPictureFilmScanner = 0x6D706673, // 'mpfs'
+ cmsSigMotionPictureFilmRecorder = 0x6D706672, // 'mpfr'
+ cmsSigDigitalMotionPictureCamera = 0x646D7063, // 'dmpc'
+ cmsSigDigitalCinemaProjector = 0x64636A70 // 'dcpj'
+
+} cmsTechnologySignature;
+
+
+// ICC Color spaces
+typedef enum {
+ cmsSigXYZData = 0x58595A20, // 'XYZ '
+ cmsSigLabData = 0x4C616220, // 'Lab '
+ cmsSigLuvData = 0x4C757620, // 'Luv '
+ cmsSigYCbCrData = 0x59436272, // 'YCbr'
+ cmsSigYxyData = 0x59787920, // 'Yxy '
+ cmsSigRgbData = 0x52474220, // 'RGB '
+ cmsSigGrayData = 0x47524159, // 'GRAY'
+ cmsSigHsvData = 0x48535620, // 'HSV '
+ cmsSigHlsData = 0x484C5320, // 'HLS '
+ cmsSigCmykData = 0x434D594B, // 'CMYK'
+ cmsSigCmyData = 0x434D5920, // 'CMY '
+ cmsSigMCH1Data = 0x4D434831, // 'MCH1'
+ cmsSigMCH2Data = 0x4D434832, // 'MCH2'
+ cmsSigMCH3Data = 0x4D434833, // 'MCH3'
+ cmsSigMCH4Data = 0x4D434834, // 'MCH4'
+ cmsSigMCH5Data = 0x4D434835, // 'MCH5'
+ cmsSigMCH6Data = 0x4D434836, // 'MCH6'
+ cmsSigMCH7Data = 0x4D434837, // 'MCH7'
+ cmsSigMCH8Data = 0x4D434838, // 'MCH8'
+ cmsSigMCH9Data = 0x4D434839, // 'MCH9'
+ cmsSigMCHAData = 0x4D434841, // 'MCHA'
+ cmsSigMCHBData = 0x4D434842, // 'MCHB'
+ cmsSigMCHCData = 0x4D434843, // 'MCHC'
+ cmsSigMCHDData = 0x4D434844, // 'MCHD'
+ cmsSigMCHEData = 0x4D434845, // 'MCHE'
+ cmsSigMCHFData = 0x4D434846, // 'MCHF'
+ cmsSigNamedData = 0x6e6d636c, // 'nmcl'
+ cmsSig1colorData = 0x31434C52, // '1CLR'
+ cmsSig2colorData = 0x32434C52, // '2CLR'
+ cmsSig3colorData = 0x33434C52, // '3CLR'
+ cmsSig4colorData = 0x34434C52, // '4CLR'
+ cmsSig5colorData = 0x35434C52, // '5CLR'
+ cmsSig6colorData = 0x36434C52, // '6CLR'
+ cmsSig7colorData = 0x37434C52, // '7CLR'
+ cmsSig8colorData = 0x38434C52, // '8CLR'
+ cmsSig9colorData = 0x39434C52, // '9CLR'
+ cmsSig10colorData = 0x41434C52, // 'ACLR'
+ cmsSig11colorData = 0x42434C52, // 'BCLR'
+ cmsSig12colorData = 0x43434C52, // 'CCLR'
+ cmsSig13colorData = 0x44434C52, // 'DCLR'
+ cmsSig14colorData = 0x45434C52, // 'ECLR'
+ cmsSig15colorData = 0x46434C52, // 'FCLR'
+ cmsSigLuvKData = 0x4C75764B // 'LuvK'
+
+} cmsColorSpaceSignature;
+
+// ICC Profile Class
+typedef enum {
+ cmsSigInputClass = 0x73636E72, // 'scnr'
+ cmsSigDisplayClass = 0x6D6E7472, // 'mntr'
+ cmsSigOutputClass = 0x70727472, // 'prtr'
+ cmsSigLinkClass = 0x6C696E6B, // 'link'
+ cmsSigAbstractClass = 0x61627374, // 'abst'
+ cmsSigColorSpaceClass = 0x73706163, // 'spac'
+ cmsSigNamedColorClass = 0x6e6d636c // 'nmcl'
+
+} cmsProfileClassSignature;
+
+// ICC Platforms
+typedef enum {
+ cmsSigMacintosh = 0x4150504C, // 'APPL'
+ cmsSigMicrosoft = 0x4D534654, // 'MSFT'
+ cmsSigSolaris = 0x53554E57, // 'SUNW'
+ cmsSigSGI = 0x53474920, // 'SGI '
+ cmsSigTaligent = 0x54474E54, // 'TGNT'
+ cmsSigUnices = 0x2A6E6978 // '*nix' // From argyll -- Not official
+
+} cmsPlatformSignature;
+
+// Reference gamut
+#define cmsSigPerceptualReferenceMediumGamut 0x70726d67 //'prmg'
+
+// For cmsSigColorimetricIntentImageStateTag
+#define cmsSigSceneColorimetryEstimates 0x73636F65 //'scoe'
+#define cmsSigSceneAppearanceEstimates 0x73617065 //'sape'
+#define cmsSigFocalPlaneColorimetryEstimates 0x66706365 //'fpce'
+#define cmsSigReflectionHardcopyOriginalColorimetry 0x72686F63 //'rhoc'
+#define cmsSigReflectionPrintOutputColorimetry 0x72706F63 //'rpoc'
+
+// Multi process elements types
+typedef enum {
+ cmsSigCurveSetElemType = 0x63767374, //'cvst'
+ cmsSigMatrixElemType = 0x6D617466, //'matf'
+ cmsSigCLutElemType = 0x636C7574, //'clut'
+
+ cmsSigBAcsElemType = 0x62414353, // 'bACS'
+ cmsSigEAcsElemType = 0x65414353, // 'eACS'
+
+ // Custom from here, not in the ICC Spec
+ cmsSigXYZ2LabElemType = 0x6C327820, // 'l2x '
+ cmsSigLab2XYZElemType = 0x78326C20, // 'x2l '
+ cmsSigNamedColorElemType = 0x6E636C20, // 'ncl '
+ cmsSigLabV2toV4 = 0x32203420, // '2 4 '
+ cmsSigLabV4toV2 = 0x34203220, // '4 2 '
+
+ // Identities
+ cmsSigIdentityElemType = 0x69646E20, // 'idn '
+
+ // Float to floatPCS
+ cmsSigLab2FloatPCS = 0x64326C20, // 'd2l '
+ cmsSigFloatPCS2Lab = 0x6C326420, // 'l2d '
+ cmsSigXYZ2FloatPCS = 0x64327820, // 'd2x '
+ cmsSigFloatPCS2XYZ = 0x78326420 // 'x2d '
+
+} cmsStageSignature;
+
+// Types of CurveElements
+typedef enum {
+
+ cmsSigFormulaCurveSeg = 0x70617266, // 'parf'
+ cmsSigSampledCurveSeg = 0x73616D66, // 'samf'
+ cmsSigSegmentedCurve = 0x63757266 // 'curf'
+
+} cmsCurveSegSignature;
+
+// Used in ResponseCurveType
+#define cmsSigStatusA 0x53746141 //'StaA'
+#define cmsSigStatusE 0x53746145 //'StaE'
+#define cmsSigStatusI 0x53746149 //'StaI'
+#define cmsSigStatusT 0x53746154 //'StaT'
+#define cmsSigStatusM 0x5374614D //'StaM'
+#define cmsSigDN 0x444E2020 //'DN '
+#define cmsSigDNP 0x444E2050 //'DN P'
+#define cmsSigDNN 0x444E4E20 //'DNN '
+#define cmsSigDNNP 0x444E4E50 //'DNNP'
+
+// Device attributes, currently defined values correspond to the low 4 bytes
+// of the 8 byte attribute quantity
+#define cmsReflective 0
+#define cmsTransparency 1
+#define cmsGlossy 0
+#define cmsMatte 2
+
+// Common structures in ICC tags
+typedef struct {
+ cmsUInt32Number len;
+ cmsUInt32Number flag;
+ cmsUInt8Number data[1];
+
+} cmsICCData;
+
+// ICC date time
+typedef struct {
+ cmsUInt16Number year;
+ cmsUInt16Number month;
+ cmsUInt16Number day;
+ cmsUInt16Number hours;
+ cmsUInt16Number minutes;
+ cmsUInt16Number seconds;
+
+} cmsDateTimeNumber;
+
+// ICC XYZ
+typedef struct {
+ cmsS15Fixed16Number X;
+ cmsS15Fixed16Number Y;
+ cmsS15Fixed16Number Z;
+
+} cmsEncodedXYZNumber;
+
+
+// Profile ID as computed by MD5 algorithm
+typedef union {
+ cmsUInt8Number ID8[16];
+ cmsUInt16Number ID16[8];
+ cmsUInt32Number ID32[4];
+
+} cmsProfileID;
+
+
+// ----------------------------------------------------------------------------------------------
+// ICC profile internal base types. Strictly, shouldn't be declared in this header, but maybe
+// somebody want to use this info for accessing profile header directly, so here it is.
+
+// Profile header -- it is 32-bit aligned, so no issues are expected on alignment
+typedef struct {
+ cmsUInt32Number size; // Profile size in bytes
+ cmsSignature cmmId; // CMM for this profile
+ cmsUInt32Number version; // Format version number
+ cmsProfileClassSignature deviceClass; // Type of profile
+ cmsColorSpaceSignature colorSpace; // Color space of data
+ cmsColorSpaceSignature pcs; // PCS, XYZ or Lab only
+ cmsDateTimeNumber date; // Date profile was created
+ cmsSignature magic; // Magic Number to identify an ICC profile
+ cmsPlatformSignature platform; // Primary Platform
+ cmsUInt32Number flags; // Various bit settings
+ cmsSignature manufacturer; // Device manufacturer
+ cmsUInt32Number model; // Device model number
+ cmsUInt64Number attributes; // Device attributes
+ cmsUInt32Number renderingIntent;// Rendering intent
+ cmsEncodedXYZNumber illuminant; // Profile illuminant
+ cmsSignature creator; // Profile creator
+ cmsProfileID profileID; // Profile ID using MD5
+ cmsInt8Number reserved[28]; // Reserved for future use
+
+} cmsICCHeader;
+
+// ICC base tag
+typedef struct {
+ cmsTagTypeSignature sig;
+ cmsInt8Number reserved[4];
+
+} cmsTagBase;
+
+// A tag entry in directory
+typedef struct {
+ cmsTagSignature sig; // The tag signature
+ cmsUInt32Number offset; // Start of tag
+ cmsUInt32Number size; // Size in bytes
+
+} cmsTagEntry;
+
+// ----------------------------------------------------------------------------------------------
+
+// Little CMS specific typedefs
+
+typedef void* cmsHANDLE ; // Generic handle
+typedef void* cmsHPROFILE; // Opaque typedefs to hide internals
+typedef void* cmsHTRANSFORM;
+
+#define cmsMAXCHANNELS 16 // Maximum number of channels in ICC profiles
+
+// Format of pixel is defined by one cmsUInt32Number, using bit fields as follows
+//
+// 2 1 0
+// 3 2 10987 6 5 4 3 2 1 098 7654 321
+// A O TTTTT U Y F P X S EEE CCCC BBB
+//
+// A: Floating point -- With this flag we can differentiate 16 bits as float and as int
+// O: Optimized -- previous optimization already returns the final 8-bit value
+// T: Pixeltype
+// F: Flavor 0=MinIsBlack(Chocolate) 1=MinIsWhite(Vanilla)
+// P: Planar? 0=Chunky, 1=Planar
+// X: swap 16 bps endianess?
+// S: Do swap? ie, BGR, KYMC
+// E: Extra samples
+// C: Channels (Samples per pixel)
+// B: bytes per sample
+// Y: Swap first - changes ABGR to BGRA and KCMY to CMYK
+
+#define FLOAT_SH(a) ((a) << 22)
+#define OPTIMIZED_SH(s) ((s) << 21)
+#define COLORSPACE_SH(s) ((s) << 16)
+#define SWAPFIRST_SH(s) ((s) << 14)
+#define FLAVOR_SH(s) ((s) << 13)
+#define PLANAR_SH(p) ((p) << 12)
+#define ENDIAN16_SH(e) ((e) << 11)
+#define DOSWAP_SH(e) ((e) << 10)
+#define EXTRA_SH(e) ((e) << 7)
+#define CHANNELS_SH(c) ((c) << 3)
+#define BYTES_SH(b) (b)
+
+// These macros unpack format specifiers into integers
+#define T_FLOAT(a) (((a)>>22)&1)
+#define T_OPTIMIZED(o) (((o)>>21)&1)
+#define T_COLORSPACE(s) (((s)>>16)&31)
+#define T_SWAPFIRST(s) (((s)>>14)&1)
+#define T_FLAVOR(s) (((s)>>13)&1)
+#define T_PLANAR(p) (((p)>>12)&1)
+#define T_ENDIAN16(e) (((e)>>11)&1)
+#define T_DOSWAP(e) (((e)>>10)&1)
+#define T_EXTRA(e) (((e)>>7)&7)
+#define T_CHANNELS(c) (((c)>>3)&15)
+#define T_BYTES(b) ((b)&7)
+
+
+// Pixel types
+#define PT_ANY 0 // Don't check colorspace
+ // 1 & 2 are reserved
+#define PT_GRAY 3
+#define PT_RGB 4
+#define PT_CMY 5
+#define PT_CMYK 6
+#define PT_YCbCr 7
+#define PT_YUV 8 // Lu'v'
+#define PT_XYZ 9
+#define PT_Lab 10
+#define PT_YUVK 11 // Lu'v'K
+#define PT_HSV 12
+#define PT_HLS 13
+#define PT_Yxy 14
+
+#define PT_MCH1 15
+#define PT_MCH2 16
+#define PT_MCH3 17
+#define PT_MCH4 18
+#define PT_MCH5 19
+#define PT_MCH6 20
+#define PT_MCH7 21
+#define PT_MCH8 22
+#define PT_MCH9 23
+#define PT_MCH10 24
+#define PT_MCH11 25
+#define PT_MCH12 26
+#define PT_MCH13 27
+#define PT_MCH14 28
+#define PT_MCH15 29
+
+#define PT_LabV2 30 // Identical to PT_Lab, but using the V2 old encoding
+
+// Some (not all!) representations
+
+#ifndef TYPE_RGB_8 // TYPE_RGB_8 is a very common identifier, so don't include ours
+ // if user has it already defined.
+
+#define TYPE_GRAY_8 (COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(1))
+#define TYPE_GRAY_8_REV (COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(1)|FLAVOR_SH(1))
+#define TYPE_GRAY_16 (COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(2))
+#define TYPE_GRAY_16_REV (COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(2)|FLAVOR_SH(1))
+#define TYPE_GRAY_16_SE (COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_GRAYA_8 (COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(1))
+#define TYPE_GRAYA_16 (COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(2))
+#define TYPE_GRAYA_16_SE (COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_GRAYA_8_PLANAR (COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_GRAYA_16_PLANAR (COLORSPACE_SH(PT_GRAY)|EXTRA_SH(1)|CHANNELS_SH(1)|BYTES_SH(2)|PLANAR_SH(1))
+
+#define TYPE_RGB_8 (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_RGB_8_PLANAR (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_BGR_8 (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_BGR_8_PLANAR (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|PLANAR_SH(1))
+#define TYPE_RGB_16 (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_RGB_16_PLANAR (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_RGB_16_SE (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_BGR_16 (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_BGR_16_PLANAR (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|PLANAR_SH(1))
+#define TYPE_BGR_16_SE (COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+
+#define TYPE_RGBA_8 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_RGBA_8_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_RGBA_16 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_RGBA_16_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_RGBA_16_SE (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+#define TYPE_ARGB_8 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_ARGB_8_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|SWAPFIRST_SH(1)|PLANAR_SH(1))
+#define TYPE_ARGB_16 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|SWAPFIRST_SH(1))
+
+#define TYPE_ABGR_8 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_ABGR_8_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|PLANAR_SH(1))
+#define TYPE_ABGR_16 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_ABGR_16_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|PLANAR_SH(1))
+#define TYPE_ABGR_16_SE (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+
+#define TYPE_BGRA_8 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_BGRA_8_PLANAR (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1)|PLANAR_SH(1))
+#define TYPE_BGRA_16 (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_BGRA_16_SE (COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1))
+
+#define TYPE_CMY_8 (COLORSPACE_SH(PT_CMY)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_CMY_8_PLANAR (COLORSPACE_SH(PT_CMY)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_CMY_16 (COLORSPACE_SH(PT_CMY)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_CMY_16_PLANAR (COLORSPACE_SH(PT_CMY)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_CMY_16_SE (COLORSPACE_SH(PT_CMY)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+#define TYPE_CMYK_8 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1))
+#define TYPE_CMYKA_8 (COLORSPACE_SH(PT_CMYK)|EXTRA_SH(1)|CHANNELS_SH(4)|BYTES_SH(1))
+#define TYPE_CMYK_8_REV (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1)|FLAVOR_SH(1))
+#define TYPE_YUVK_8 TYPE_CMYK_8_REV
+#define TYPE_CMYK_8_PLANAR (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_CMYK_16 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2))
+#define TYPE_CMYK_16_REV (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|FLAVOR_SH(1))
+#define TYPE_YUVK_16 TYPE_CMYK_16_REV
+#define TYPE_CMYK_16_PLANAR (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_CMYK_16_SE (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+#define TYPE_KYMC_8 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC_16 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC_16_SE (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+
+#define TYPE_KCMY_8 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_KCMY_8_REV (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(1)|FLAVOR_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_KCMY_16 (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|SWAPFIRST_SH(1))
+#define TYPE_KCMY_16_REV (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|FLAVOR_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_KCMY_16_SE (COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2)|ENDIAN16_SH(1)|SWAPFIRST_SH(1))
+
+#define TYPE_CMYK5_8 (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(1))
+#define TYPE_CMYK5_16 (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(2))
+#define TYPE_CMYK5_16_SE (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC5_8 (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC5_16 (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC5_16_SE (COLORSPACE_SH(PT_MCH5)|CHANNELS_SH(5)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK6_8 (COLORSPACE_SH(PT_MCH6)|CHANNELS_SH(6)|BYTES_SH(1))
+#define TYPE_CMYK6_8_PLANAR (COLORSPACE_SH(PT_MCH6)|CHANNELS_SH(6)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_CMYK6_16 (COLORSPACE_SH(PT_MCH6)|CHANNELS_SH(6)|BYTES_SH(2))
+#define TYPE_CMYK6_16_PLANAR (COLORSPACE_SH(PT_MCH6)|CHANNELS_SH(6)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_CMYK6_16_SE (COLORSPACE_SH(PT_MCH6)|CHANNELS_SH(6)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_CMYK7_8 (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(1))
+#define TYPE_CMYK7_16 (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(2))
+#define TYPE_CMYK7_16_SE (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC7_8 (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC7_16 (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC7_16_SE (COLORSPACE_SH(PT_MCH7)|CHANNELS_SH(7)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK8_8 (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(1))
+#define TYPE_CMYK8_16 (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(2))
+#define TYPE_CMYK8_16_SE (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC8_8 (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC8_16 (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC8_16_SE (COLORSPACE_SH(PT_MCH8)|CHANNELS_SH(8)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK9_8 (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(1))
+#define TYPE_CMYK9_16 (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(2))
+#define TYPE_CMYK9_16_SE (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC9_8 (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC9_16 (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC9_16_SE (COLORSPACE_SH(PT_MCH9)|CHANNELS_SH(9)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK10_8 (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(1))
+#define TYPE_CMYK10_16 (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(2))
+#define TYPE_CMYK10_16_SE (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC10_8 (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC10_16 (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC10_16_SE (COLORSPACE_SH(PT_MCH10)|CHANNELS_SH(10)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK11_8 (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(1))
+#define TYPE_CMYK11_16 (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(2))
+#define TYPE_CMYK11_16_SE (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC11_8 (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC11_16 (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC11_16_SE (COLORSPACE_SH(PT_MCH11)|CHANNELS_SH(11)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+#define TYPE_CMYK12_8 (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(1))
+#define TYPE_CMYK12_16 (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(2))
+#define TYPE_CMYK12_16_SE (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(2)|ENDIAN16_SH(1))
+#define TYPE_KYMC12_8 (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(1)|DOSWAP_SH(1))
+#define TYPE_KYMC12_16 (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_KYMC12_16_SE (COLORSPACE_SH(PT_MCH12)|CHANNELS_SH(12)|BYTES_SH(2)|DOSWAP_SH(1)|ENDIAN16_SH(1))
+
+// Colorimetric
+#define TYPE_XYZ_16 (COLORSPACE_SH(PT_XYZ)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_Lab_8 (COLORSPACE_SH(PT_Lab)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_LabV2_8 (COLORSPACE_SH(PT_LabV2)|CHANNELS_SH(3)|BYTES_SH(1))
+
+#define TYPE_ALab_8 (COLORSPACE_SH(PT_Lab)|CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_ALabV2_8 (COLORSPACE_SH(PT_LabV2)|CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_Lab_16 (COLORSPACE_SH(PT_Lab)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_LabV2_16 (COLORSPACE_SH(PT_LabV2)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_Yxy_16 (COLORSPACE_SH(PT_Yxy)|CHANNELS_SH(3)|BYTES_SH(2))
+
+// YCbCr
+#define TYPE_YCbCr_8 (COLORSPACE_SH(PT_YCbCr)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_YCbCr_8_PLANAR (COLORSPACE_SH(PT_YCbCr)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_YCbCr_16 (COLORSPACE_SH(PT_YCbCr)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_YCbCr_16_PLANAR (COLORSPACE_SH(PT_YCbCr)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_YCbCr_16_SE (COLORSPACE_SH(PT_YCbCr)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+// YUV
+#define TYPE_YUV_8 (COLORSPACE_SH(PT_YUV)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_YUV_8_PLANAR (COLORSPACE_SH(PT_YUV)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_YUV_16 (COLORSPACE_SH(PT_YUV)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_YUV_16_PLANAR (COLORSPACE_SH(PT_YUV)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_YUV_16_SE (COLORSPACE_SH(PT_YUV)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+// HLS
+#define TYPE_HLS_8 (COLORSPACE_SH(PT_HLS)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_HLS_8_PLANAR (COLORSPACE_SH(PT_HLS)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_HLS_16 (COLORSPACE_SH(PT_HLS)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_HLS_16_PLANAR (COLORSPACE_SH(PT_HLS)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_HLS_16_SE (COLORSPACE_SH(PT_HLS)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+// HSV
+#define TYPE_HSV_8 (COLORSPACE_SH(PT_HSV)|CHANNELS_SH(3)|BYTES_SH(1))
+#define TYPE_HSV_8_PLANAR (COLORSPACE_SH(PT_HSV)|CHANNELS_SH(3)|BYTES_SH(1)|PLANAR_SH(1))
+#define TYPE_HSV_16 (COLORSPACE_SH(PT_HSV)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_HSV_16_PLANAR (COLORSPACE_SH(PT_HSV)|CHANNELS_SH(3)|BYTES_SH(2)|PLANAR_SH(1))
+#define TYPE_HSV_16_SE (COLORSPACE_SH(PT_HSV)|CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1))
+
+// Named color index. Only 16 bits allowed (don't check colorspace)
+#define TYPE_NAMED_COLOR_INDEX (CHANNELS_SH(1)|BYTES_SH(2))
+
+// Float formatters.
+#define TYPE_XYZ_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_XYZ)|CHANNELS_SH(3)|BYTES_SH(4))
+#define TYPE_Lab_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_Lab)|CHANNELS_SH(3)|BYTES_SH(4))
+#define TYPE_LabA_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_Lab)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(4))
+#define TYPE_GRAY_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(4))
+#define TYPE_RGB_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(4))
+
+#define TYPE_RGBA_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(4))
+#define TYPE_ARGB_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(4)|SWAPFIRST_SH(1))
+#define TYPE_BGR_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(4)|DOSWAP_SH(1))
+#define TYPE_BGRA_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(4)|DOSWAP_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_ABGR_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(4)|DOSWAP_SH(1))
+
+#define TYPE_CMYK_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(4))
+
+// Floating point formatters.
+// NOTE THAT 'BYTES' FIELD IS SET TO ZERO ON DLB because 8 bytes overflows the bitfield
+#define TYPE_XYZ_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_XYZ)|CHANNELS_SH(3)|BYTES_SH(0))
+#define TYPE_Lab_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_Lab)|CHANNELS_SH(3)|BYTES_SH(0))
+#define TYPE_GRAY_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(0))
+#define TYPE_RGB_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(0))
+#define TYPE_BGR_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(0)|DOSWAP_SH(1))
+#define TYPE_CMYK_DBL (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(0))
+
+// IEEE 754-2008 "half"
+#define TYPE_GRAY_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_GRAY)|CHANNELS_SH(1)|BYTES_SH(2))
+#define TYPE_RGB_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_RGBA_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_CMYK_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_CMYK)|CHANNELS_SH(4)|BYTES_SH(2))
+
+#define TYPE_RGBA_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2))
+#define TYPE_ARGB_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|SWAPFIRST_SH(1))
+#define TYPE_BGR_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1))
+#define TYPE_BGRA_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|EXTRA_SH(1)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1)|SWAPFIRST_SH(1))
+#define TYPE_ABGR_HALF_FLT (FLOAT_SH(1)|COLORSPACE_SH(PT_RGB)|CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1))
+
+#endif
+
+// Colorspaces
+typedef struct {
+ cmsFloat64Number X;
+ cmsFloat64Number Y;
+ cmsFloat64Number Z;
+
+ } cmsCIEXYZ;
+
+typedef struct {
+ cmsFloat64Number x;
+ cmsFloat64Number y;
+ cmsFloat64Number Y;
+
+ } cmsCIExyY;
+
+typedef struct {
+ cmsFloat64Number L;
+ cmsFloat64Number a;
+ cmsFloat64Number b;
+
+ } cmsCIELab;
+
+typedef struct {
+ cmsFloat64Number L;
+ cmsFloat64Number C;
+ cmsFloat64Number h;
+
+ } cmsCIELCh;
+
+typedef struct {
+ cmsFloat64Number J;
+ cmsFloat64Number C;
+ cmsFloat64Number h;
+
+ } cmsJCh;
+
+typedef struct {
+ cmsCIEXYZ Red;
+ cmsCIEXYZ Green;
+ cmsCIEXYZ Blue;
+
+ } cmsCIEXYZTRIPLE;
+
+typedef struct {
+ cmsCIExyY Red;
+ cmsCIExyY Green;
+ cmsCIExyY Blue;
+
+ } cmsCIExyYTRIPLE;
+
+// Illuminant types for structs below
+#define cmsILLUMINANT_TYPE_UNKNOWN 0x0000000
+#define cmsILLUMINANT_TYPE_D50 0x0000001
+#define cmsILLUMINANT_TYPE_D65 0x0000002
+#define cmsILLUMINANT_TYPE_D93 0x0000003
+#define cmsILLUMINANT_TYPE_F2 0x0000004
+#define cmsILLUMINANT_TYPE_D55 0x0000005
+#define cmsILLUMINANT_TYPE_A 0x0000006
+#define cmsILLUMINANT_TYPE_E 0x0000007
+#define cmsILLUMINANT_TYPE_F8 0x0000008
+
+typedef struct {
+ cmsUInt32Number Observer; // 0 = unknown, 1=CIE 1931, 2=CIE 1964
+ cmsCIEXYZ Backing; // Value of backing
+ cmsUInt32Number Geometry; // 0=unknown, 1=45/0, 0/45 2=0d, d/0
+ cmsFloat64Number Flare; // 0..1.0
+ cmsUInt32Number IlluminantType;
+
+ } cmsICCMeasurementConditions;
+
+typedef struct {
+ cmsCIEXYZ IlluminantXYZ; // Not the same struct as CAM02,
+ cmsCIEXYZ SurroundXYZ; // This is for storing the tag
+ cmsUInt32Number IlluminantType; // viewing condition
+
+ } cmsICCViewingConditions;
+
+// Support of non-standard functions --------------------------------------------------------------------------------------
+
+CMSAPI int CMSEXPORT cmsstrcasecmp(const char* s1, const char* s2);
+CMSAPI long int CMSEXPORT cmsfilelength(FILE* f);
+
+
+// Context handling --------------------------------------------------------------------------------------------------------
+
+// Each context holds its owns globals and its own plug-ins. There is a global context with the id = 0 for lecacy compatibility
+// though using the global context is not recomended. Proper context handling makes lcms more thread-safe.
+
+typedef struct _cmsContext_struct* cmsContext;
+
+CMSAPI cmsContext CMSEXPORT cmsCreateContext(void* Plugin, void* UserData);
+CMSAPI void CMSEXPORT cmsDeleteContext(cmsContext ContexID);
+CMSAPI cmsContext CMSEXPORT cmsDupContext(cmsContext ContextID, void* NewUserData);
+CMSAPI void* CMSEXPORT cmsGetContextUserData(cmsContext ContextID);
+
+// Plug-In registering --------------------------------------------------------------------------------------------------
+
+CMSAPI cmsBool CMSEXPORT cmsPlugin(void* Plugin);
+CMSAPI cmsBool CMSEXPORT cmsPluginTHR(cmsContext ContextID, void* Plugin);
+CMSAPI void CMSEXPORT cmsUnregisterPlugins(void);
+CMSAPI void CMSEXPORT cmsUnregisterPluginsTHR(cmsContext ContextID);
+
+// Error logging ----------------------------------------------------------------------------------------------------------
+
+// There is no error handling at all. When a function fails, it returns proper value.
+// For example, all create functions does return NULL on failure. Other may return FALSE.
+// It may be interesting, for the developer, to know why the function is failing.
+// for that reason, lcms2 does offer a logging function. This function will get
+// an ENGLISH string with some clues on what is going wrong. You can show this
+// info to the end user if you wish, or just create some sort of log on disk.
+// The logging function should NOT terminate the program, as this obviously can leave
+// unfreed resources. It is the programmer's responsibility to check each function
+// return code to make sure it didn't fail.
+
+#define cmsERROR_UNDEFINED 0
+#define cmsERROR_FILE 1
+#define cmsERROR_RANGE 2
+#define cmsERROR_INTERNAL 3
+#define cmsERROR_NULL 4
+#define cmsERROR_READ 5
+#define cmsERROR_SEEK 6
+#define cmsERROR_WRITE 7
+#define cmsERROR_UNKNOWN_EXTENSION 8
+#define cmsERROR_COLORSPACE_CHECK 9
+#define cmsERROR_ALREADY_DEFINED 10
+#define cmsERROR_BAD_SIGNATURE 11
+#define cmsERROR_CORRUPTION_DETECTED 12
+#define cmsERROR_NOT_SUITABLE 13
+
+// Error logger is called with the ContextID when a message is raised. This gives the
+// chance to know which thread is responsible of the warning and any environment associated
+// with it. Non-multithreading applications may safely ignore this parameter.
+// Note that under certain special circumstances, ContextID may be NULL.
+typedef void (* cmsLogErrorHandlerFunction)(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
+
+// Allows user to set any specific logger
+CMSAPI void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn);
+CMSAPI void CMSEXPORT cmsSetLogErrorHandlerTHR(cmsContext ContextID, cmsLogErrorHandlerFunction Fn);
+
+// Conversions --------------------------------------------------------------------------------------------------------------
+
+// Returns pointers to constant structs
+CMSAPI const cmsCIEXYZ* CMSEXPORT cmsD50_XYZ(void);
+CMSAPI const cmsCIExyY* CMSEXPORT cmsD50_xyY(void);
+
+// Colorimetric space conversions
+CMSAPI void CMSEXPORT cmsXYZ2xyY(cmsCIExyY* Dest, const cmsCIEXYZ* Source);
+CMSAPI void CMSEXPORT cmsxyY2XYZ(cmsCIEXYZ* Dest, const cmsCIExyY* Source);
+CMSAPI void CMSEXPORT cmsXYZ2Lab(const cmsCIEXYZ* WhitePoint, cmsCIELab* Lab, const cmsCIEXYZ* xyz);
+CMSAPI void CMSEXPORT cmsLab2XYZ(const cmsCIEXYZ* WhitePoint, cmsCIEXYZ* xyz, const cmsCIELab* Lab);
+CMSAPI void CMSEXPORT cmsLab2LCh(cmsCIELCh*LCh, const cmsCIELab* Lab);
+CMSAPI void CMSEXPORT cmsLCh2Lab(cmsCIELab* Lab, const cmsCIELCh* LCh);
+
+// Encoding /Decoding on PCS
+CMSAPI void CMSEXPORT cmsLabEncoded2Float(cmsCIELab* Lab, const cmsUInt16Number wLab[3]);
+CMSAPI void CMSEXPORT cmsLabEncoded2FloatV2(cmsCIELab* Lab, const cmsUInt16Number wLab[3]);
+CMSAPI void CMSEXPORT cmsFloat2LabEncoded(cmsUInt16Number wLab[3], const cmsCIELab* Lab);
+CMSAPI void CMSEXPORT cmsFloat2LabEncodedV2(cmsUInt16Number wLab[3], const cmsCIELab* Lab);
+CMSAPI void CMSEXPORT cmsXYZEncoded2Float(cmsCIEXYZ* fxyz, const cmsUInt16Number XYZ[3]);
+CMSAPI void CMSEXPORT cmsFloat2XYZEncoded(cmsUInt16Number XYZ[3], const cmsCIEXYZ* fXYZ);
+
+// DeltaE metrics
+CMSAPI cmsFloat64Number CMSEXPORT cmsDeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2);
+CMSAPI cmsFloat64Number CMSEXPORT cmsCIE94DeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2);
+CMSAPI cmsFloat64Number CMSEXPORT cmsBFDdeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2);
+CMSAPI cmsFloat64Number CMSEXPORT cmsCMCdeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2, cmsFloat64Number l, cmsFloat64Number c);
+CMSAPI cmsFloat64Number CMSEXPORT cmsCIE2000DeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2, cmsFloat64Number Kl, cmsFloat64Number Kc, cmsFloat64Number Kh);
+
+// Temperature <-> Chromaticity (Black body)
+CMSAPI cmsBool CMSEXPORT cmsWhitePointFromTemp(cmsCIExyY* WhitePoint, cmsFloat64Number TempK);
+CMSAPI cmsBool CMSEXPORT cmsTempFromWhitePoint(cmsFloat64Number* TempK, const cmsCIExyY* WhitePoint);
+
+// Chromatic adaptation
+CMSAPI cmsBool CMSEXPORT cmsAdaptToIlluminant(cmsCIEXYZ* Result, const cmsCIEXYZ* SourceWhitePt,
+ const cmsCIEXYZ* Illuminant,
+ const cmsCIEXYZ* Value);
+
+// CIECAM02 ---------------------------------------------------------------------------------------------------
+
+// Viewing conditions. Please note those are CAM model viewing conditions, and not the ICC tag viewing
+// conditions, which I'm naming cmsICCViewingConditions to make differences evident. Unfortunately, the tag
+// cannot deal with surround La, Yb and D value so is basically useless to store CAM02 viewing conditions.
+
+
+#define AVG_SURROUND 1
+#define DIM_SURROUND 2
+#define DARK_SURROUND 3
+#define CUTSHEET_SURROUND 4
+
+#define D_CALCULATE (-1)
+
+typedef struct {
+ cmsCIEXYZ whitePoint;
+ cmsFloat64Number Yb;
+ cmsFloat64Number La;
+ int surround;
+ cmsFloat64Number D_value;
+
+ } cmsViewingConditions;
+
+CMSAPI cmsHANDLE CMSEXPORT cmsCIECAM02Init(cmsContext ContextID, const cmsViewingConditions* pVC);
+CMSAPI void CMSEXPORT cmsCIECAM02Done(cmsHANDLE hModel);
+CMSAPI void CMSEXPORT cmsCIECAM02Forward(cmsHANDLE hModel, const cmsCIEXYZ* pIn, cmsJCh* pOut);
+CMSAPI void CMSEXPORT cmsCIECAM02Reverse(cmsHANDLE hModel, const cmsJCh* pIn, cmsCIEXYZ* pOut);
+
+
+// Tone curves -----------------------------------------------------------------------------------------
+
+// This describes a curve segment. For a table of supported types, see the manual. User can increase the number of
+// available types by using a proper plug-in. Parametric segments allow 10 parameters at most
+
+typedef struct {
+ cmsFloat32Number x0, x1; // Domain; for x0 < x <= x1
+ cmsInt32Number Type; // Parametric type, Type == 0 means sampled segment. Negative values are reserved
+ cmsFloat64Number Params[10]; // Parameters if Type != 0
+ cmsUInt32Number nGridPoints; // Number of grid points if Type == 0
+ cmsFloat32Number* SampledPoints; // Points to an array of floats if Type == 0
+
+} cmsCurveSegment;
+
+// The internal representation is none of your business.
+typedef struct _cms_curve_struct cmsToneCurve;
+
+CMSAPI cmsToneCurve* CMSEXPORT cmsBuildSegmentedToneCurve(cmsContext ContextID, cmsInt32Number nSegments, const cmsCurveSegment Segments[]);
+CMSAPI cmsToneCurve* CMSEXPORT cmsBuildParametricToneCurve(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[]);
+CMSAPI cmsToneCurve* CMSEXPORT cmsBuildGamma(cmsContext ContextID, cmsFloat64Number Gamma);
+CMSAPI cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurve16(cmsContext ContextID, cmsInt32Number nEntries, const cmsUInt16Number values[]);
+CMSAPI cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurveFloat(cmsContext ContextID, cmsUInt32Number nEntries, const cmsFloat32Number values[]);
+CMSAPI void CMSEXPORT cmsFreeToneCurve(cmsToneCurve* Curve);
+CMSAPI void CMSEXPORT cmsFreeToneCurveTriple(cmsToneCurve* Curve[3]);
+CMSAPI cmsToneCurve* CMSEXPORT cmsDupToneCurve(const cmsToneCurve* Src);
+CMSAPI cmsToneCurve* CMSEXPORT cmsReverseToneCurve(const cmsToneCurve* InGamma);
+CMSAPI cmsToneCurve* CMSEXPORT cmsReverseToneCurveEx(cmsInt32Number nResultSamples, const cmsToneCurve* InGamma);
+CMSAPI cmsToneCurve* CMSEXPORT cmsJoinToneCurve(cmsContext ContextID, const cmsToneCurve* X, const cmsToneCurve* Y, cmsUInt32Number nPoints);
+CMSAPI cmsBool CMSEXPORT cmsSmoothToneCurve(cmsToneCurve* Tab, cmsFloat64Number lambda);
+CMSAPI cmsFloat32Number CMSEXPORT cmsEvalToneCurveFloat(const cmsToneCurve* Curve, cmsFloat32Number v);
+CMSAPI cmsUInt16Number CMSEXPORT cmsEvalToneCurve16(const cmsToneCurve* Curve, cmsUInt16Number v);
+CMSAPI cmsBool CMSEXPORT cmsIsToneCurveMultisegment(const cmsToneCurve* InGamma);
+CMSAPI cmsBool CMSEXPORT cmsIsToneCurveLinear(const cmsToneCurve* Curve);
+CMSAPI cmsBool CMSEXPORT cmsIsToneCurveMonotonic(const cmsToneCurve* t);
+CMSAPI cmsBool CMSEXPORT cmsIsToneCurveDescending(const cmsToneCurve* t);
+CMSAPI cmsInt32Number CMSEXPORT cmsGetToneCurveParametricType(const cmsToneCurve* t);
+CMSAPI cmsFloat64Number CMSEXPORT cmsEstimateGamma(const cmsToneCurve* t, cmsFloat64Number Precision);
+
+// Tone curve tabular estimation
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetToneCurveEstimatedTableEntries(const cmsToneCurve* t);
+CMSAPI const cmsUInt16Number* CMSEXPORT cmsGetToneCurveEstimatedTable(const cmsToneCurve* t);
+
+
+// Implements pipelines of multi-processing elements -------------------------------------------------------------
+
+// Nothing to see here, move along
+typedef struct _cmsPipeline_struct cmsPipeline;
+typedef struct _cmsStage_struct cmsStage;
+
+// Those are hi-level pipelines
+CMSAPI cmsPipeline* CMSEXPORT cmsPipelineAlloc(cmsContext ContextID, cmsUInt32Number InputChannels, cmsUInt32Number OutputChannels);
+CMSAPI void CMSEXPORT cmsPipelineFree(cmsPipeline* lut);
+CMSAPI cmsPipeline* CMSEXPORT cmsPipelineDup(const cmsPipeline* Orig);
+
+CMSAPI cmsContext CMSEXPORT cmsGetPipelineContextID(const cmsPipeline* lut);
+CMSAPI cmsUInt32Number CMSEXPORT cmsPipelineInputChannels(const cmsPipeline* lut);
+CMSAPI cmsUInt32Number CMSEXPORT cmsPipelineOutputChannels(const cmsPipeline* lut);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsPipelineStageCount(const cmsPipeline* lut);
+CMSAPI cmsStage* CMSEXPORT cmsPipelineGetPtrToFirstStage(const cmsPipeline* lut);
+CMSAPI cmsStage* CMSEXPORT cmsPipelineGetPtrToLastStage(const cmsPipeline* lut);
+
+CMSAPI void CMSEXPORT cmsPipelineEval16(const cmsUInt16Number In[], cmsUInt16Number Out[], const cmsPipeline* lut);
+CMSAPI void CMSEXPORT cmsPipelineEvalFloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsPipeline* lut);
+CMSAPI cmsBool CMSEXPORT cmsPipelineEvalReverseFloat(cmsFloat32Number Target[], cmsFloat32Number Result[], cmsFloat32Number Hint[], const cmsPipeline* lut);
+CMSAPI cmsBool CMSEXPORT cmsPipelineCat(cmsPipeline* l1, const cmsPipeline* l2);
+CMSAPI cmsBool CMSEXPORT cmsPipelineSetSaveAs8bitsFlag(cmsPipeline* lut, cmsBool On);
+
+// Where to place/locate the stages in the pipeline chain
+typedef enum { cmsAT_BEGIN, cmsAT_END } cmsStageLoc;
+
+CMSAPI int CMSEXPORT cmsPipelineInsertStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage* mpe);
+CMSAPI void CMSEXPORT cmsPipelineUnlinkStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe);
+
+// This function is quite useful to analyze the structure of a Pipeline and retrieve the Stage elements
+// that conform the Pipeline. It should be called with the Pipeline, the number of expected elements and
+// then a list of expected types followed with a list of double pointers to Stage elements. If
+// the function founds a match with current pipeline, it fills the pointers and returns TRUE
+// if not, returns FALSE without touching anything.
+CMSAPI cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(const cmsPipeline* Lut, cmsUInt32Number n, ...);
+
+// Matrix has double precision and CLUT has only float precision. That is because an ICC profile can encode
+// matrices with far more precision that CLUTS
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocIdentity(cmsContext ContextID, cmsUInt32Number nChannels);
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocToneCurves(cmsContext ContextID, cmsUInt32Number nChannels, cmsToneCurve* const Curves[]);
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocMatrix(cmsContext ContextID, cmsUInt32Number Rows, cmsUInt32Number Cols, const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset);
+
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocCLut16bit(cmsContext ContextID, cmsUInt32Number nGridPoints, cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsUInt16Number* Table);
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocCLutFloat(cmsContext ContextID, cmsUInt32Number nGridPoints, cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table);
+
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsUInt16Number* Table);
+CMSAPI cmsStage* CMSEXPORT cmsStageAllocCLutFloatGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table);
+
+CMSAPI cmsStage* CMSEXPORT cmsStageDup(cmsStage* mpe);
+CMSAPI void CMSEXPORT cmsStageFree(cmsStage* mpe);
+CMSAPI cmsStage* CMSEXPORT cmsStageNext(const cmsStage* mpe);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsStageInputChannels(const cmsStage* mpe);
+CMSAPI cmsUInt32Number CMSEXPORT cmsStageOutputChannels(const cmsStage* mpe);
+CMSAPI cmsStageSignature CMSEXPORT cmsStageType(const cmsStage* mpe);
+CMSAPI void* CMSEXPORT cmsStageData(const cmsStage* mpe);
+
+// Sampling
+typedef cmsInt32Number (* cmsSAMPLER16) (register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register void * Cargo);
+
+typedef cmsInt32Number (* cmsSAMPLERFLOAT)(register const cmsFloat32Number In[],
+ register cmsFloat32Number Out[],
+ register void * Cargo);
+
+// Use this flag to prevent changes being written to destination
+#define SAMPLER_INSPECT 0x01000000
+
+// For CLUT only
+CMSAPI cmsBool CMSEXPORT cmsStageSampleCLut16bit(cmsStage* mpe, cmsSAMPLER16 Sampler, void* Cargo, cmsUInt32Number dwFlags);
+CMSAPI cmsBool CMSEXPORT cmsStageSampleCLutFloat(cmsStage* mpe, cmsSAMPLERFLOAT Sampler, void* Cargo, cmsUInt32Number dwFlags);
+
+// Slicers
+CMSAPI cmsBool CMSEXPORT cmsSliceSpace16(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
+ cmsSAMPLER16 Sampler, void * Cargo);
+
+CMSAPI cmsBool CMSEXPORT cmsSliceSpaceFloat(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
+ cmsSAMPLERFLOAT Sampler, void * Cargo);
+
+// Multilocalized Unicode management ---------------------------------------------------------------------------------------
+
+typedef struct _cms_MLU_struct cmsMLU;
+
+#define cmsNoLanguage "\0\0"
+#define cmsNoCountry "\0\0"
+
+CMSAPI cmsMLU* CMSEXPORT cmsMLUalloc(cmsContext ContextID, cmsUInt32Number nItems);
+CMSAPI void CMSEXPORT cmsMLUfree(cmsMLU* mlu);
+CMSAPI cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu);
+
+CMSAPI cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ const char* ASCIIString);
+CMSAPI cmsBool CMSEXPORT cmsMLUsetWide(cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ const wchar_t* WideString);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ char* Buffer, cmsUInt32Number BufferSize);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsMLUgetWide(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ wchar_t* Buffer, cmsUInt32Number BufferSize);
+
+CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ char ObtainedLanguage[3], char ObtainedCountry[3]);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu);
+
+CMSAPI cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
+ cmsUInt32Number idx,
+ char LanguageCode[3],
+ char CountryCode[3]);
+
+// Undercolorremoval & black generation -------------------------------------------------------------------------------------
+
+typedef struct {
+ cmsToneCurve* Ucr;
+ cmsToneCurve* Bg;
+ cmsMLU* Desc;
+
+} cmsUcrBg;
+
+// Screening ----------------------------------------------------------------------------------------------------------------
+
+#define cmsPRINTER_DEFAULT_SCREENS 0x0001
+#define cmsFREQUENCE_UNITS_LINES_CM 0x0000
+#define cmsFREQUENCE_UNITS_LINES_INCH 0x0002
+
+#define cmsSPOT_UNKNOWN 0
+#define cmsSPOT_PRINTER_DEFAULT 1
+#define cmsSPOT_ROUND 2
+#define cmsSPOT_DIAMOND 3
+#define cmsSPOT_ELLIPSE 4
+#define cmsSPOT_LINE 5
+#define cmsSPOT_SQUARE 6
+#define cmsSPOT_CROSS 7
+
+typedef struct {
+ cmsFloat64Number Frequency;
+ cmsFloat64Number ScreenAngle;
+ cmsUInt32Number SpotShape;
+
+} cmsScreeningChannel;
+
+typedef struct {
+ cmsUInt32Number Flag;
+ cmsUInt32Number nChannels;
+ cmsScreeningChannel Channels[cmsMAXCHANNELS];
+
+} cmsScreening;
+
+
+// Named color -----------------------------------------------------------------------------------------------------------------
+
+typedef struct _cms_NAMEDCOLORLIST_struct cmsNAMEDCOLORLIST;
+
+CMSAPI cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID,
+ cmsUInt32Number n,
+ cmsUInt32Number ColorantCount,
+ const char* Prefix, const char* Suffix);
+
+CMSAPI void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v);
+CMSAPI cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v);
+CMSAPI cmsBool CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* v, const char* Name,
+ cmsUInt16Number PCS[3],
+ cmsUInt16Number Colorant[cmsMAXCHANNELS]);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* v);
+CMSAPI cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* v, const char* Name);
+
+CMSAPI cmsBool CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor,
+ char* Name,
+ char* Prefix,
+ char* Suffix,
+ cmsUInt16Number* PCS,
+ cmsUInt16Number* Colorant);
+
+// Retrieve named color list from transform
+CMSAPI cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform);
+
+// Profile sequence -----------------------------------------------------------------------------------------------------
+
+// Profile sequence descriptor. Some fields come from profile sequence descriptor tag, others
+// come from Profile Sequence Identifier Tag
+typedef struct {
+
+ cmsSignature deviceMfg;
+ cmsSignature deviceModel;
+ cmsUInt64Number attributes;
+ cmsTechnologySignature technology;
+ cmsProfileID ProfileID;
+ cmsMLU* Manufacturer;
+ cmsMLU* Model;
+ cmsMLU* Description;
+
+} cmsPSEQDESC;
+
+typedef struct {
+
+ cmsUInt32Number n;
+ cmsContext ContextID;
+ cmsPSEQDESC* seq;
+
+} cmsSEQ;
+
+CMSAPI cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n);
+CMSAPI cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq);
+CMSAPI void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq);
+
+// Dictionaries --------------------------------------------------------------------------------------------------------
+
+typedef struct _cmsDICTentry_struct {
+
+ struct _cmsDICTentry_struct* Next;
+
+ cmsMLU *DisplayName;
+ cmsMLU *DisplayValue;
+ wchar_t* Name;
+ wchar_t* Value;
+
+} cmsDICTentry;
+
+CMSAPI cmsHANDLE CMSEXPORT cmsDictAlloc(cmsContext ContextID);
+CMSAPI void CMSEXPORT cmsDictFree(cmsHANDLE hDict);
+CMSAPI cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict);
+
+CMSAPI cmsBool CMSEXPORT cmsDictAddEntry(cmsHANDLE hDict, const wchar_t* Name, const wchar_t* Value, const cmsMLU *DisplayName, const cmsMLU *DisplayValue);
+CMSAPI const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict);
+CMSAPI const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e);
+
+// Access to Profile data ----------------------------------------------------------------------------------------------
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID);
+
+CMSAPI cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile);
+CMSAPI cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile);
+CMSAPI cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n);
+CMSAPI cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig);
+
+// Read and write pre-formatted data
+CMSAPI void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig);
+CMSAPI cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data);
+CMSAPI cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest);
+CMSAPI cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig);
+
+// Read and write raw data
+CMSAPI cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* Buffer, cmsUInt32Number BufferSize);
+CMSAPI cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size);
+
+// Access header data
+#define cmsEmbeddedProfileFalse 0x00000000
+#define cmsEmbeddedProfileTrue 0x00000001
+#define cmsUseAnywhere 0x00000000
+#define cmsUseWithEmbeddedDataOnly 0x00000002
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags);
+CMSAPI void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID);
+CMSAPI cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile);
+
+CMSAPI void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetHeaderCreator(cmsHPROFILE hProfile);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model);
+CMSAPI void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags);
+CMSAPI void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID);
+CMSAPI void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent);
+
+CMSAPI cmsColorSpaceSignature
+ CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs);
+CMSAPI cmsColorSpaceSignature
+ CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig);
+CMSAPI cmsProfileClassSignature
+ CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig);
+CMSAPI void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version);
+CMSAPI cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile);
+CMSAPI void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version);
+
+// How profiles may be used
+#define LCMS_USED_AS_INPUT 0
+#define LCMS_USED_AS_OUTPUT 1
+#define LCMS_USED_AS_PROOF 2
+
+CMSAPI cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection);
+CMSAPI cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile);
+CMSAPI cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection);
+
+// Translate form/to our notation to ICC
+CMSAPI cmsColorSpaceSignature CMSEXPORT _cmsICCcolorSpace(int OurNotation);
+CMSAPI int CMSEXPORT _cmsLCMScolorSpace(cmsColorSpaceSignature ProfileSpace);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsChannelsOf(cmsColorSpaceSignature ColorSpace);
+
+// Build a suitable formatter for the colorspace of this profile
+CMSAPI cmsUInt32Number CMSEXPORT cmsFormatterForColorspaceOfProfile(cmsHPROFILE hProfile, cmsUInt32Number nBytes, cmsBool lIsFloat);
+CMSAPI cmsUInt32Number CMSEXPORT cmsFormatterForPCSOfProfile(cmsHPROFILE hProfile, cmsUInt32Number nBytes, cmsBool lIsFloat);
+
+
+// Localized info
+typedef enum {
+ cmsInfoDescription = 0,
+ cmsInfoManufacturer = 1,
+ cmsInfoModel = 2,
+ cmsInfoCopyright = 3
+} cmsInfoType;
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ wchar_t* Buffer, cmsUInt32Number BufferSize);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ char* Buffer, cmsUInt32Number BufferSize);
+
+// IO handlers ----------------------------------------------------------------------------------------------------------
+
+typedef struct _cms_io_handler cmsIOHANDLER;
+
+CMSAPI cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode);
+CMSAPI cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream);
+CMSAPI cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode);
+CMSAPI cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID);
+CMSAPI cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io);
+
+// MD5 message digest --------------------------------------------------------------------------------------------------
+
+CMSAPI cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile);
+CMSAPI cmsBool CMSEXPORT cmsMD5computeIDExt(const void* buf, unsigned long size, unsigned char ProfileID[16]);
+
+// Profile high level funtions ------------------------------------------------------------------------------------------
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *ICCProfile, const char *sAccess);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char* sAccess);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char* sAccess);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void * MemPtr, cmsUInt32Number dwSize);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void * MemPtr, cmsUInt32Number dwSize);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io);
+CMSAPI cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandler2THR(cmsContext ContextID, cmsIOHANDLER* io, cmsBool write);
+CMSAPI cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile);
+
+CMSAPI cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName);
+CMSAPI cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream);
+CMSAPI cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded);
+CMSAPI cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io);
+
+// Predefined virtual profiles ------------------------------------------------------------------------------------------
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateRGBProfileTHR(cmsContext ContextID,
+ const cmsCIExyY* WhitePoint,
+ const cmsCIExyYTRIPLE* Primaries,
+ cmsToneCurve* const TransferFunction[3]);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateRGBProfile(const cmsCIExyY* WhitePoint,
+ const cmsCIExyYTRIPLE* Primaries,
+ cmsToneCurve* const TransferFunction[3]);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateGrayProfileTHR(cmsContext ContextID,
+ const cmsCIExyY* WhitePoint,
+ const cmsToneCurve* TransferFunction);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateGrayProfile(const cmsCIExyY* WhitePoint,
+ const cmsToneCurve* TransferFunction);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLinkTHR(cmsContext ContextID,
+ cmsColorSpaceSignature ColorSpace,
+ cmsToneCurve* const TransferFunctions[]);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLink(cmsColorSpaceSignature ColorSpace,
+ cmsToneCurve* const TransferFunctions[]);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLinkTHR(cmsContext ContextID,
+ cmsColorSpaceSignature ColorSpace, cmsFloat64Number Limit);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLink(cmsColorSpaceSignature ColorSpace, cmsFloat64Number Limit);
+
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLab2ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLab2Profile(const cmsCIExyY* WhitePoint);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLab4ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateLab4Profile(const cmsCIExyY* WhitePoint);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateXYZProfileTHR(cmsContext ContextID);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateXYZProfile(void);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfileTHR(cmsContext ContextID);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfile(void);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfileTHR(cmsContext ContextID,
+ int nLUTPoints,
+ cmsFloat64Number Bright,
+ cmsFloat64Number Contrast,
+ cmsFloat64Number Hue,
+ cmsFloat64Number Saturation,
+ int TempSrc,
+ int TempDest);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfile(int nLUTPoints,
+ cmsFloat64Number Bright,
+ cmsFloat64Number Contrast,
+ cmsFloat64Number Hue,
+ cmsFloat64Number Saturation,
+ int TempSrc,
+ int TempDest);
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateNULLProfileTHR(cmsContext ContextID);
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateNULLProfile(void);
+
+// Converts a transform to a devicelink profile
+CMSAPI cmsHPROFILE CMSEXPORT cmsTransform2DeviceLink(cmsHTRANSFORM hTransform, cmsFloat64Number Version, cmsUInt32Number dwFlags);
+
+// Intents ----------------------------------------------------------------------------------------------
+
+// ICC Intents
+#define INTENT_PERCEPTUAL 0
+#define INTENT_RELATIVE_COLORIMETRIC 1
+#define INTENT_SATURATION 2
+#define INTENT_ABSOLUTE_COLORIMETRIC 3
+
+// Non-ICC intents
+#define INTENT_PRESERVE_K_ONLY_PERCEPTUAL 10
+#define INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC 11
+#define INTENT_PRESERVE_K_ONLY_SATURATION 12
+#define INTENT_PRESERVE_K_PLANE_PERCEPTUAL 13
+#define INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC 14
+#define INTENT_PRESERVE_K_PLANE_SATURATION 15
+
+// Call with NULL as parameters to get the intent count
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetSupportedIntents(cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetSupportedIntentsTHR(cmsContext ContextID, cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions);
+
+// Flags
+
+#define cmsFLAGS_NOCACHE 0x0040 // Inhibit 1-pixel cache
+#define cmsFLAGS_NOOPTIMIZE 0x0100 // Inhibit optimizations
+#define cmsFLAGS_NULLTRANSFORM 0x0200 // Don't transform anyway
+
+// Proofing flags
+#define cmsFLAGS_GAMUTCHECK 0x1000 // Out of Gamut alarm
+#define cmsFLAGS_SOFTPROOFING 0x4000 // Do softproofing
+
+// Misc
+#define cmsFLAGS_BLACKPOINTCOMPENSATION 0x2000
+#define cmsFLAGS_NOWHITEONWHITEFIXUP 0x0004 // Don't fix scum dot
+#define cmsFLAGS_HIGHRESPRECALC 0x0400 // Use more memory to give better accurancy
+#define cmsFLAGS_LOWRESPRECALC 0x0800 // Use less memory to minimize resouces
+
+// For devicelink creation
+#define cmsFLAGS_8BITS_DEVICELINK 0x0008 // Create 8 bits devicelinks
+#define cmsFLAGS_GUESSDEVICECLASS 0x0020 // Guess device class (for transform2devicelink)
+#define cmsFLAGS_KEEP_SEQUENCE 0x0080 // Keep profile sequence for devicelink creation
+
+// Specific to a particular optimizations
+#define cmsFLAGS_FORCE_CLUT 0x0002 // Force CLUT optimization
+#define cmsFLAGS_CLUT_POST_LINEARIZATION 0x0001 // create postlinearization tables if possible
+#define cmsFLAGS_CLUT_PRE_LINEARIZATION 0x0010 // create prelinearization tables if possible
+
+// Fine-tune control over number of gridpoints
+#define cmsFLAGS_GRIDPOINTS(n) (((n) & 0xFF) << 16)
+
+// CRD special
+#define cmsFLAGS_NODEFAULTRESOURCEDEF 0x01000000
+
+// Transforms ---------------------------------------------------------------------------------------------------
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateTransformTHR(cmsContext ContextID,
+ cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags);
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateTransform(cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags);
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateProofingTransformTHR(cmsContext ContextID,
+ cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsHPROFILE Proofing,
+ cmsUInt32Number Intent,
+ cmsUInt32Number ProofingIntent,
+ cmsUInt32Number dwFlags);
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateProofingTransform(cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsHPROFILE Proofing,
+ cmsUInt32Number Intent,
+ cmsUInt32Number ProofingIntent,
+ cmsUInt32Number dwFlags);
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateMultiprofileTransformTHR(cmsContext ContextID,
+ cmsHPROFILE hProfiles[],
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags);
+
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateMultiprofileTransform(cmsHPROFILE hProfiles[],
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags);
+
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateExtendedTransform(cmsContext ContextID,
+ cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsUInt32Number Intents[],
+ cmsFloat64Number AdaptationStates[],
+ cmsHPROFILE hGamutProfile,
+ cmsUInt32Number nGamutPCSposition,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number dwFlags);
+
+CMSAPI void CMSEXPORT cmsDeleteTransform(cmsHTRANSFORM hTransform);
+
+CMSAPI void CMSEXPORT cmsDoTransform(cmsHTRANSFORM Transform,
+ const void * InputBuffer,
+ void * OutputBuffer,
+ cmsUInt32Number Size);
+
+CMSAPI void CMSEXPORT cmsDoTransformStride(cmsHTRANSFORM Transform,
+ const void * InputBuffer,
+ void * OutputBuffer,
+ cmsUInt32Number Size,
+ cmsUInt32Number Stride);
+
+
+CMSAPI void CMSEXPORT cmsSetAlarmCodes(const cmsUInt16Number NewAlarm[cmsMAXCHANNELS]);
+CMSAPI void CMSEXPORT cmsGetAlarmCodes(cmsUInt16Number NewAlarm[cmsMAXCHANNELS]);
+
+
+CMSAPI void CMSEXPORT cmsSetAlarmCodesTHR(cmsContext ContextID,
+ const cmsUInt16Number AlarmCodes[cmsMAXCHANNELS]);
+CMSAPI void CMSEXPORT cmsGetAlarmCodesTHR(cmsContext ContextID,
+ cmsUInt16Number AlarmCodes[cmsMAXCHANNELS]);
+
+
+
+// Adaptation state for absolute colorimetric intent
+CMSAPI cmsFloat64Number CMSEXPORT cmsSetAdaptationState(cmsFloat64Number d);
+CMSAPI cmsFloat64Number CMSEXPORT cmsSetAdaptationStateTHR(cmsContext ContextID, cmsFloat64Number d);
+
+
+
+// Grab the ContextID from an open transform. Returns NULL if a NULL transform is passed
+CMSAPI cmsContext CMSEXPORT cmsGetTransformContextID(cmsHTRANSFORM hTransform);
+
+// Grab the input/output formats
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetTransformInputFormat(cmsHTRANSFORM hTransform);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetTransformOutputFormat(cmsHTRANSFORM hTransform);
+
+// For backwards compatibility
+CMSAPI cmsBool CMSEXPORT cmsChangeBuffersFormat(cmsHTRANSFORM hTransform,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat);
+
+
+
+// PostScript ColorRenderingDictionary and ColorSpaceArray ----------------------------------------------------
+
+typedef enum { cmsPS_RESOURCE_CSA, cmsPS_RESOURCE_CRD } cmsPSResourceType;
+
+// lcms2 unified method to access postscript color resources
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetPostScriptColorResource(cmsContext ContextID,
+ cmsPSResourceType Type,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags,
+ cmsIOHANDLER* io);
+
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetPostScriptCSA(cmsContext ContextID, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags, void* Buffer, cmsUInt32Number dwBufferLen);
+CMSAPI cmsUInt32Number CMSEXPORT cmsGetPostScriptCRD(cmsContext ContextID, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags, void* Buffer, cmsUInt32Number dwBufferLen);
+
+
+// IT8.7 / CGATS.17-200x handling -----------------------------------------------------------------------------
+
+CMSAPI cmsHANDLE CMSEXPORT cmsIT8Alloc(cmsContext ContextID);
+CMSAPI void CMSEXPORT cmsIT8Free(cmsHANDLE hIT8);
+
+// Tables
+CMSAPI cmsUInt32Number CMSEXPORT cmsIT8TableCount(cmsHANDLE hIT8);
+CMSAPI cmsInt32Number CMSEXPORT cmsIT8SetTable(cmsHANDLE hIT8, cmsUInt32Number nTable);
+
+// Persistence
+CMSAPI cmsHANDLE CMSEXPORT cmsIT8LoadFromFile(cmsContext ContextID, const char* cFileName);
+CMSAPI cmsHANDLE CMSEXPORT cmsIT8LoadFromMem(cmsContext ContextID, void *Ptr, cmsUInt32Number len);
+// CMSAPI cmsHANDLE CMSEXPORT cmsIT8LoadFromIOhandler(cmsContext ContextID, cmsIOHANDLER* io);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SaveToFile(cmsHANDLE hIT8, const char* cFileName);
+CMSAPI cmsBool CMSEXPORT cmsIT8SaveToMem(cmsHANDLE hIT8, void *MemPtr, cmsUInt32Number* BytesNeeded);
+
+// Properties
+CMSAPI const char* CMSEXPORT cmsIT8GetSheetType(cmsHANDLE hIT8);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetSheetType(cmsHANDLE hIT8, const char* Type);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetComment(cmsHANDLE hIT8, const char* cComment);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetPropertyStr(cmsHANDLE hIT8, const char* cProp, const char *Str);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetPropertyDbl(cmsHANDLE hIT8, const char* cProp, cmsFloat64Number Val);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetPropertyHex(cmsHANDLE hIT8, const char* cProp, cmsUInt32Number Val);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetPropertyMulti(cmsHANDLE hIT8, const char* Key, const char* SubKey, const char *Buffer);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetPropertyUncooked(cmsHANDLE hIT8, const char* Key, const char* Buffer);
+
+
+CMSAPI const char* CMSEXPORT cmsIT8GetProperty(cmsHANDLE hIT8, const char* cProp);
+CMSAPI cmsFloat64Number CMSEXPORT cmsIT8GetPropertyDbl(cmsHANDLE hIT8, const char* cProp);
+CMSAPI const char* CMSEXPORT cmsIT8GetPropertyMulti(cmsHANDLE hIT8, const char* Key, const char *SubKey);
+CMSAPI cmsUInt32Number CMSEXPORT cmsIT8EnumProperties(cmsHANDLE hIT8, char ***PropertyNames);
+CMSAPI cmsUInt32Number CMSEXPORT cmsIT8EnumPropertyMulti(cmsHANDLE hIT8, const char* cProp, const char ***SubpropertyNames);
+
+// Datasets
+CMSAPI const char* CMSEXPORT cmsIT8GetDataRowCol(cmsHANDLE hIT8, int row, int col);
+CMSAPI cmsFloat64Number CMSEXPORT cmsIT8GetDataRowColDbl(cmsHANDLE hIT8, int row, int col);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetDataRowCol(cmsHANDLE hIT8, int row, int col,
+ const char* Val);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetDataRowColDbl(cmsHANDLE hIT8, int row, int col,
+ cmsFloat64Number Val);
+
+CMSAPI const char* CMSEXPORT cmsIT8GetData(cmsHANDLE hIT8, const char* cPatch, const char* cSample);
+
+
+CMSAPI cmsFloat64Number CMSEXPORT cmsIT8GetDataDbl(cmsHANDLE hIT8, const char* cPatch, const char* cSample);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetData(cmsHANDLE hIT8, const char* cPatch,
+ const char* cSample,
+ const char *Val);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetDataDbl(cmsHANDLE hIT8, const char* cPatch,
+ const char* cSample,
+ cmsFloat64Number Val);
+
+CMSAPI int CMSEXPORT cmsIT8FindDataFormat(cmsHANDLE hIT8, const char* cSample);
+CMSAPI cmsBool CMSEXPORT cmsIT8SetDataFormat(cmsHANDLE hIT8, int n, const char *Sample);
+CMSAPI int CMSEXPORT cmsIT8EnumDataFormat(cmsHANDLE hIT8, char ***SampleNames);
+
+CMSAPI const char* CMSEXPORT cmsIT8GetPatchName(cmsHANDLE hIT8, int nPatch, char* buffer);
+CMSAPI int CMSEXPORT cmsIT8GetPatchByName(cmsHANDLE hIT8, const char *cPatch);
+
+// The LABEL extension
+CMSAPI int CMSEXPORT cmsIT8SetTableByLabel(cmsHANDLE hIT8, const char* cSet, const char* cField, const char* ExpectedType);
+
+CMSAPI cmsBool CMSEXPORT cmsIT8SetIndexColumn(cmsHANDLE hIT8, const char* cSample);
+
+// Formatter for double
+CMSAPI void CMSEXPORT cmsIT8DefineDblFormat(cmsHANDLE hIT8, const char* Formatter);
+
+// Gamut boundary description routines ------------------------------------------------------------------------------
+
+CMSAPI cmsHANDLE CMSEXPORT cmsGBDAlloc(cmsContext ContextID);
+CMSAPI void CMSEXPORT cmsGBDFree(cmsHANDLE hGBD);
+CMSAPI cmsBool CMSEXPORT cmsGDBAddPoint(cmsHANDLE hGBD, const cmsCIELab* Lab);
+CMSAPI cmsBool CMSEXPORT cmsGDBCompute(cmsHANDLE hGDB, cmsUInt32Number dwFlags);
+CMSAPI cmsBool CMSEXPORT cmsGDBCheckPoint(cmsHANDLE hGBD, const cmsCIELab* Lab);
+
+// Feature detection ----------------------------------------------------------------------------------------------
+
+// Estimate the black point
+CMSAPI cmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags);
+CMSAPI cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags);
+
+// Estimate total area coverage
+CMSAPI cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsHPROFILE hProfile);
+
+
+// Poor man's gamut mapping
+CMSAPI cmsBool CMSEXPORT cmsDesaturateLab(cmsCIELab* Lab,
+ double amax, double amin,
+ double bmax, double bmin);
+
+#ifndef CMS_USE_CPP_API
+# ifdef __cplusplus
+ }
+# endif
+#endif
+
+#define _lcms2_H
+#endif
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2_plugin.h b/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2_plugin.h
new file mode 100644
index 0000000000..0c95d1f73c
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/include/lcms2_plugin.h
@@ -0,0 +1,637 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2011 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.
+//
+//---------------------------------------------------------------------------------
+//
+// This is the plug-in header file. Normal LittleCMS clients should not use it.
+// It is provided for plug-in writters that may want to access the support
+// functions to do low level operations. All plug-in related structures
+// are defined here. Including this file forces to include the standard API too.
+
+#ifndef _lcms_plugin_H
+
+// Deal with Microsoft's attempt at deprecating C standard runtime functions
+#ifdef _MSC_VER
+# if (_MSC_VER >= 1400)
+# ifndef _CRT_SECURE_NO_DEPRECATE
+# define _CRT_SECURE_NO_DEPRECATE
+# endif
+# ifndef _CRT_SECURE_NO_WARNINGS
+# define _CRT_SECURE_NO_WARNINGS
+# endif
+# endif
+#endif
+
+#ifndef _lcms2_H
+#include "lcms2.h"
+#endif
+
+// We need some standard C functions.
+#include <stdlib.h>
+#include <math.h>
+#include <stdarg.h>
+#include <memory.h>
+#include <string.h>
+
+
+#ifndef CMS_USE_CPP_API
+# ifdef __cplusplus
+extern "C" {
+# endif
+#endif
+
+// Vector & Matrix operations -----------------------------------------------------------------------
+
+// Axis of the matrix/array. No specific meaning at all.
+#define VX 0
+#define VY 1
+#define VZ 2
+
+// Vectors
+typedef struct {
+ cmsFloat64Number n[3];
+
+ } cmsVEC3;
+
+// 3x3 Matrix
+typedef struct {
+ cmsVEC3 v[3];
+
+ } cmsMAT3;
+
+CMSAPI void CMSEXPORT _cmsVEC3init(cmsVEC3* r, cmsFloat64Number x, cmsFloat64Number y, cmsFloat64Number z);
+CMSAPI void CMSEXPORT _cmsVEC3minus(cmsVEC3* r, const cmsVEC3* a, const cmsVEC3* b);
+CMSAPI void CMSEXPORT _cmsVEC3cross(cmsVEC3* r, const cmsVEC3* u, const cmsVEC3* v);
+CMSAPI cmsFloat64Number CMSEXPORT _cmsVEC3dot(const cmsVEC3* u, const cmsVEC3* v);
+CMSAPI cmsFloat64Number CMSEXPORT _cmsVEC3length(const cmsVEC3* a);
+CMSAPI cmsFloat64Number CMSEXPORT _cmsVEC3distance(const cmsVEC3* a, const cmsVEC3* b);
+
+CMSAPI void CMSEXPORT _cmsMAT3identity(cmsMAT3* a);
+CMSAPI cmsBool CMSEXPORT _cmsMAT3isIdentity(const cmsMAT3* a);
+CMSAPI void CMSEXPORT _cmsMAT3per(cmsMAT3* r, const cmsMAT3* a, const cmsMAT3* b);
+CMSAPI cmsBool CMSEXPORT _cmsMAT3inverse(const cmsMAT3* a, cmsMAT3* b);
+CMSAPI cmsBool CMSEXPORT _cmsMAT3solve(cmsVEC3* x, cmsMAT3* a, cmsVEC3* b);
+CMSAPI void CMSEXPORT _cmsMAT3eval(cmsVEC3* r, const cmsMAT3* a, const cmsVEC3* v);
+
+
+// Error logging -------------------------------------------------------------------------------------
+
+CMSAPI void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...);
+
+// Memory management ----------------------------------------------------------------------------------
+
+CMSAPI void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size);
+CMSAPI void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size);
+CMSAPI void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size);
+CMSAPI void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number NewSize);
+CMSAPI void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr);
+CMSAPI void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size);
+
+// I/O handler ----------------------------------------------------------------------------------
+
+struct _cms_io_handler {
+
+ void* stream; // Associated stream, which is implemented differently depending on media.
+
+ cmsContext ContextID;
+ cmsUInt32Number UsedSpace;
+ cmsUInt32Number ReportedSize;
+ char PhysicalFile[cmsMAX_PATH];
+
+ cmsUInt32Number (* Read)(struct _cms_io_handler* iohandler, void *Buffer,
+ cmsUInt32Number size,
+ cmsUInt32Number count);
+ cmsBool (* Seek)(struct _cms_io_handler* iohandler, cmsUInt32Number offset);
+ cmsBool (* Close)(struct _cms_io_handler* iohandler);
+ cmsUInt32Number (* Tell)(struct _cms_io_handler* iohandler);
+ cmsBool (* Write)(struct _cms_io_handler* iohandler, cmsUInt32Number size,
+ const void* Buffer);
+};
+
+// Endianess adjust functions
+CMSAPI cmsUInt16Number CMSEXPORT _cmsAdjustEndianess16(cmsUInt16Number Word);
+CMSAPI cmsUInt32Number CMSEXPORT _cmsAdjustEndianess32(cmsUInt32Number Value);
+CMSAPI void CMSEXPORT _cmsAdjustEndianess64(cmsUInt64Number* Result, cmsUInt64Number* QWord);
+
+// Helper IO functions
+CMSAPI cmsBool CMSEXPORT _cmsReadUInt8Number(cmsIOHANDLER* io, cmsUInt8Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsReadUInt16Number(cmsIOHANDLER* io, cmsUInt16Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsReadUInt32Number(cmsIOHANDLER* io, cmsUInt32Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsReadFloat32Number(cmsIOHANDLER* io, cmsFloat32Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsReadUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsRead15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsReadXYZNumber(cmsIOHANDLER* io, cmsCIEXYZ* XYZ);
+CMSAPI cmsBool CMSEXPORT _cmsReadUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, cmsUInt16Number* Array);
+
+CMSAPI cmsBool CMSEXPORT _cmsWriteUInt8Number(cmsIOHANDLER* io, cmsUInt8Number n);
+CMSAPI cmsBool CMSEXPORT _cmsWriteUInt16Number(cmsIOHANDLER* io, cmsUInt16Number n);
+CMSAPI cmsBool CMSEXPORT _cmsWriteUInt32Number(cmsIOHANDLER* io, cmsUInt32Number n);
+CMSAPI cmsBool CMSEXPORT _cmsWriteFloat32Number(cmsIOHANDLER* io, cmsFloat32Number n);
+CMSAPI cmsBool CMSEXPORT _cmsWriteUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n);
+CMSAPI cmsBool CMSEXPORT _cmsWrite15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number n);
+CMSAPI cmsBool CMSEXPORT _cmsWriteXYZNumber(cmsIOHANDLER* io, const cmsCIEXYZ* XYZ);
+CMSAPI cmsBool CMSEXPORT _cmsWriteUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, const cmsUInt16Number* Array);
+
+// ICC base tag
+typedef struct {
+ cmsTagTypeSignature sig;
+ cmsInt8Number reserved[4];
+
+} _cmsTagBase;
+
+// Type base helper functions
+CMSAPI cmsTagTypeSignature CMSEXPORT _cmsReadTypeBase(cmsIOHANDLER* io);
+CMSAPI cmsBool CMSEXPORT _cmsWriteTypeBase(cmsIOHANDLER* io, cmsTagTypeSignature sig);
+
+// Alignment functions
+CMSAPI cmsBool CMSEXPORT _cmsReadAlignment(cmsIOHANDLER* io);
+CMSAPI cmsBool CMSEXPORT _cmsWriteAlignment(cmsIOHANDLER* io);
+
+// To deal with text streams. 2K at most
+CMSAPI cmsBool CMSEXPORT _cmsIOPrintf(cmsIOHANDLER* io, const char* frm, ...);
+
+// Fixed point helper functions
+CMSAPI cmsFloat64Number CMSEXPORT _cms8Fixed8toDouble(cmsUInt16Number fixed8);
+CMSAPI cmsUInt16Number CMSEXPORT _cmsDoubleTo8Fixed8(cmsFloat64Number val);
+
+CMSAPI cmsFloat64Number CMSEXPORT _cms15Fixed16toDouble(cmsS15Fixed16Number fix32);
+CMSAPI cmsS15Fixed16Number CMSEXPORT _cmsDoubleTo15Fixed16(cmsFloat64Number v);
+
+// Date/time helper functions
+CMSAPI void CMSEXPORT _cmsEncodeDateTimeNumber(cmsDateTimeNumber *Dest, const struct tm *Source);
+CMSAPI void CMSEXPORT _cmsDecodeDateTimeNumber(const cmsDateTimeNumber *Source, struct tm *Dest);
+
+//----------------------------------------------------------------------------------------------------------
+
+// Shared callbacks for user data
+typedef void (* _cmsFreeUserDataFn)(cmsContext ContextID, void* Data);
+typedef void* (* _cmsDupUserDataFn)(cmsContext ContextID, const void* Data);
+
+//----------------------------------------------------------------------------------------------------------
+
+// Plug-in foundation
+#define cmsPluginMagicNumber 0x61637070 // 'acpp'
+
+#define cmsPluginMemHandlerSig 0x6D656D48 // 'memH'
+#define cmsPluginInterpolationSig 0x696E7048 // 'inpH'
+#define cmsPluginParametricCurveSig 0x70617248 // 'parH'
+#define cmsPluginFormattersSig 0x66726D48 // 'frmH
+#define cmsPluginTagTypeSig 0x74797048 // 'typH'
+#define cmsPluginTagSig 0x74616748 // 'tagH'
+#define cmsPluginRenderingIntentSig 0x696E7448 // 'intH'
+#define cmsPluginMultiProcessElementSig 0x6D706548 // 'mpeH'
+#define cmsPluginOptimizationSig 0x6F707448 // 'optH'
+#define cmsPluginTransformSig 0x7A666D48 // 'xfmH'
+#define cmsPluginMutexSig 0x6D747A48 // 'mtxH'
+
+typedef struct _cmsPluginBaseStruct {
+
+ cmsUInt32Number Magic; // 'acpp' signature
+ cmsUInt32Number ExpectedVersion; // Expected version of LittleCMS
+ cmsUInt32Number Type; // Type of plug-in
+ struct _cmsPluginBaseStruct* Next; // For multiple plugin definition. NULL for end of list.
+
+} cmsPluginBase;
+
+// Maximum number of types in a plugin array
+#define MAX_TYPES_IN_LCMS_PLUGIN 20
+
+//----------------------------------------------------------------------------------------------------------
+
+// Memory handler. Each new plug-in type replaces current behaviour
+
+typedef void* (* _cmsMallocFnPtrType)(cmsContext ContextID, cmsUInt32Number size);
+typedef void (* _cmsFreeFnPtrType)(cmsContext ContextID, void *Ptr);
+typedef void* (* _cmsReallocFnPtrType)(cmsContext ContextID, void* Ptr, cmsUInt32Number NewSize);
+
+typedef void* (* _cmsMalloZerocFnPtrType)(cmsContext ContextID, cmsUInt32Number size);
+typedef void* (* _cmsCallocFnPtrType)(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size);
+typedef void* (* _cmsDupFnPtrType)(cmsContext ContextID, const void* Org, cmsUInt32Number size);
+
+typedef struct {
+
+ cmsPluginBase base;
+
+ // Required
+ _cmsMallocFnPtrType MallocPtr;
+ _cmsFreeFnPtrType FreePtr;
+ _cmsReallocFnPtrType ReallocPtr;
+
+ // Optional
+ _cmsMalloZerocFnPtrType MallocZeroPtr;
+ _cmsCallocFnPtrType CallocPtr;
+ _cmsDupFnPtrType DupPtr;
+
+} cmsPluginMemHandler;
+
+
+// ------------------------------------------------------------------------------------------------------------------
+
+// Interpolation. 16 bits and floating point versions.
+struct _cms_interp_struc;
+
+// Interpolation callbacks
+
+// 16 bits forward interpolation. This function performs precision-limited linear interpolation
+// and is supposed to be quite fast. Implementation may be tetrahedral or trilinear, and plug-ins may
+// choose to implement any other interpolation algorithm.
+typedef void (* _cmsInterpFn16)(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const struct _cms_interp_struc* p);
+
+// Floating point forward interpolation. Full precision interpolation using floats. This is not a
+// time critical function. Implementation may be tetrahedral or trilinear, and plug-ins may
+// choose to implement any other interpolation algorithm.
+typedef void (* _cmsInterpFnFloat)(cmsFloat32Number const Input[],
+ cmsFloat32Number Output[],
+ const struct _cms_interp_struc* p);
+
+
+
+// This type holds a pointer to an interpolator that can be either 16 bits or float
+typedef union {
+ _cmsInterpFn16 Lerp16; // Forward interpolation in 16 bits
+ _cmsInterpFnFloat LerpFloat; // Forward interpolation in floating point
+} cmsInterpFunction;
+
+// Flags for interpolator selection
+#define CMS_LERP_FLAGS_16BITS 0x0000 // The default
+#define CMS_LERP_FLAGS_FLOAT 0x0001 // Requires different implementation
+#define CMS_LERP_FLAGS_TRILINEAR 0x0100 // Hint only
+
+
+#define MAX_INPUT_DIMENSIONS 8
+
+typedef struct _cms_interp_struc { // Used on all interpolations. Supplied by lcms2 when calling the interpolation function
+
+ cmsContext ContextID; // The calling thread
+
+ cmsUInt32Number dwFlags; // Keep original flags
+ cmsUInt32Number nInputs; // != 1 only in 3D interpolation
+ cmsUInt32Number nOutputs; // != 1 only in 3D interpolation
+
+ cmsUInt32Number nSamples[MAX_INPUT_DIMENSIONS]; // Valid on all kinds of tables
+ cmsUInt32Number Domain[MAX_INPUT_DIMENSIONS]; // Domain = nSamples - 1
+
+ cmsUInt32Number opta[MAX_INPUT_DIMENSIONS]; // Optimization for 3D CLUT. This is the number of nodes premultiplied for each
+ // dimension. For example, in 7 nodes, 7, 7^2 , 7^3, 7^4, etc. On non-regular
+ // Samplings may vary according of the number of nodes for each dimension.
+
+ const void *Table; // Points to the actual interpolation table
+ cmsInterpFunction Interpolation; // Points to the function to do the interpolation
+
+ } cmsInterpParams;
+
+// Interpolators factory
+typedef cmsInterpFunction (* cmsInterpFnFactory)(cmsUInt32Number nInputChannels, cmsUInt32Number nOutputChannels, cmsUInt32Number dwFlags);
+
+// The plug-in
+typedef struct {
+ cmsPluginBase base;
+
+ // Points to a user-supplied function which implements the factory
+ cmsInterpFnFactory InterpolatorsFactory;
+
+} cmsPluginInterpolation;
+
+//----------------------------------------------------------------------------------------------------------
+
+// Parametric curves. A negative type means same function but analytically inverted. Max. number of params is 10
+
+// Evaluator callback for user-suplied parametric curves. May implement more than one type
+typedef cmsFloat64Number (* cmsParametricCurveEvaluator)(cmsInt32Number Type, const cmsFloat64Number Params[10], cmsFloat64Number R);
+
+// Plug-in may implement an arbitrary number of parametric curves
+typedef struct {
+ cmsPluginBase base;
+
+ cmsUInt32Number nFunctions; // Number of supported functions
+ cmsUInt32Number FunctionTypes[MAX_TYPES_IN_LCMS_PLUGIN]; // The identification types
+ cmsUInt32Number ParameterCount[MAX_TYPES_IN_LCMS_PLUGIN]; // Number of parameters for each function
+
+ cmsParametricCurveEvaluator Evaluator; // The evaluator
+
+} cmsPluginParametricCurves;
+//----------------------------------------------------------------------------------------------------------
+
+// Formatters. This plug-in adds new handlers, replacing them if they already exist. Formatters dealing with
+// cmsFloat32Number (bps = 4) or double (bps = 0) types are requested via FormatterFloat callback. Others come across
+// Formatter16 callback
+
+struct _cmstransform_struct;
+
+typedef cmsUInt8Number* (* cmsFormatter16)(register struct _cmstransform_struct* CMMcargo,
+ register cmsUInt16Number Values[],
+ register cmsUInt8Number* Buffer,
+ register cmsUInt32Number Stride);
+
+typedef cmsUInt8Number* (* cmsFormatterFloat)(struct _cmstransform_struct* CMMcargo,
+ cmsFloat32Number Values[],
+ cmsUInt8Number* Buffer,
+ cmsUInt32Number Stride);
+
+// This type holds a pointer to a formatter that can be either 16 bits or cmsFloat32Number
+typedef union {
+ cmsFormatter16 Fmt16;
+ cmsFormatterFloat FmtFloat;
+
+} cmsFormatter;
+
+#define CMS_PACK_FLAGS_16BITS 0x0000
+#define CMS_PACK_FLAGS_FLOAT 0x0001
+
+typedef enum { cmsFormatterInput=0, cmsFormatterOutput=1 } cmsFormatterDirection;
+
+typedef cmsFormatter (* cmsFormatterFactory)(cmsUInt32Number Type, // Specific type, i.e. TYPE_RGB_8
+ cmsFormatterDirection Dir,
+ cmsUInt32Number dwFlags); // precision
+
+// Plug-in may implement an arbitrary number of formatters
+typedef struct {
+ cmsPluginBase base;
+ cmsFormatterFactory FormattersFactory;
+
+} cmsPluginFormatters;
+
+//----------------------------------------------------------------------------------------------------------
+
+// Tag type handler. Each type is free to return anything it wants, and it is up to the caller to
+// know in advance what is the type contained in the tag.
+typedef struct _cms_typehandler_struct {
+
+ cmsTagTypeSignature Signature; // The signature of the type
+
+ // Allocates and reads items
+ void * (* ReadPtr)(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ cmsUInt32Number* nItems,
+ cmsUInt32Number SizeOfTag);
+
+ // Writes n Items
+ cmsBool (* WritePtr)(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Ptr,
+ cmsUInt32Number nItems);
+
+ // Duplicate an item or array of items
+ void* (* DupPtr)(struct _cms_typehandler_struct* self,
+ const void *Ptr,
+ cmsUInt32Number n);
+
+ // Free all resources
+ void (* FreePtr)(struct _cms_typehandler_struct* self,
+ void *Ptr);
+
+ // Additional parameters used by the calling thread
+ cmsContext ContextID;
+ cmsUInt32Number ICCVersion;
+
+} cmsTagTypeHandler;
+
+// Each plug-in implements a single type
+typedef struct {
+ cmsPluginBase base;
+ cmsTagTypeHandler Handler;
+
+} cmsPluginTagType;
+
+//----------------------------------------------------------------------------------------------------------
+
+// This is the tag plugin, which identifies tags. For writing, a pointer to function is provided.
+// This function should return the desired type for this tag, given the version of profile
+// and the data being serialized.
+typedef struct {
+
+ cmsUInt32Number ElemCount; // If this tag needs an array, how many elements should keep
+
+ // For reading.
+ cmsUInt32Number nSupportedTypes; // In how many types this tag can come (MAX_TYPES_IN_LCMS_PLUGIN maximum)
+ cmsTagTypeSignature SupportedTypes[MAX_TYPES_IN_LCMS_PLUGIN];
+
+ // For writting
+ cmsTagTypeSignature (* DecideType)(cmsFloat64Number ICCVersion, const void *Data);
+
+} cmsTagDescriptor;
+
+// Plug-in implements a single tag
+typedef struct {
+ cmsPluginBase base;
+
+ cmsTagSignature Signature;
+ cmsTagDescriptor Descriptor;
+
+} cmsPluginTag;
+
+//----------------------------------------------------------------------------------------------------------
+
+// Custom intents. This function should join all profiles specified in the array in
+// a single LUT. Any custom intent in the chain redirects to custom function. If more than
+// one custom intent is found, the one located first is invoked. Usually users should use only one
+// custom intent, so mixing custom intents in same multiprofile transform is not supported.
+
+typedef cmsPipeline* (* cmsIntentFn)( cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+
+// Each plug-in defines a single intent number.
+typedef struct {
+ cmsPluginBase base;
+ cmsUInt32Number Intent;
+ cmsIntentFn Link;
+ char Description[256];
+
+} cmsPluginRenderingIntent;
+
+
+// The default ICC intents (perceptual, saturation, rel.col and abs.col)
+CMSAPI cmsPipeline* CMSEXPORT _cmsDefaultICCintents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+
+//----------------------------------------------------------------------------------------------------------
+
+// Pipelines, Multi Process Elements.
+
+typedef void (* _cmsStageEvalFn) (const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage* mpe);
+typedef void*(* _cmsStageDupElemFn) (cmsStage* mpe);
+typedef void (* _cmsStageFreeElemFn) (cmsStage* mpe);
+
+
+// This function allocates a generic MPE
+CMSAPI cmsStage* CMSEXPORT _cmsStageAllocPlaceholder(cmsContext ContextID,
+ cmsStageSignature Type,
+ cmsUInt32Number InputChannels,
+ cmsUInt32Number OutputChannels,
+ _cmsStageEvalFn EvalPtr, // Points to fn that evaluates the element (always in floating point)
+ _cmsStageDupElemFn DupElemPtr, // Points to a fn that duplicates the stage
+ _cmsStageFreeElemFn FreePtr, // Points to a fn that sets the element free
+ void* Data); // A generic pointer to whatever memory needed by the element
+typedef struct {
+ cmsPluginBase base;
+ cmsTagTypeHandler Handler;
+
+} cmsPluginMultiProcessElement;
+
+
+// Data kept in "Element" member of cmsStage
+
+// Curves
+typedef struct {
+ cmsUInt32Number nCurves;
+ cmsToneCurve** TheCurves;
+
+} _cmsStageToneCurvesData;
+
+// Matrix
+typedef struct {
+ cmsFloat64Number* Double; // floating point for the matrix
+ cmsFloat64Number* Offset; // The offset
+
+} _cmsStageMatrixData;
+
+// CLUT
+typedef struct {
+
+ union { // Can have only one of both representations at same time
+ cmsUInt16Number* T; // Points to the table 16 bits table
+ cmsFloat32Number* TFloat; // Points to the cmsFloat32Number table
+
+ } Tab;
+
+ cmsInterpParams* Params;
+ cmsUInt32Number nEntries;
+ cmsBool HasFloatValues;
+
+} _cmsStageCLutData;
+
+
+//----------------------------------------------------------------------------------------------------------
+// Optimization. Using this plug-in, additional optimization strategies may be implemented.
+// The function should return TRUE if any optimization is done on the LUT, this terminates
+// the optimization search. Or FALSE if it is unable to optimize and want to give a chance
+// to the rest of optimizers.
+
+typedef void (* _cmsOPTeval16Fn)(register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register const void* Data);
+
+
+typedef cmsBool (* _cmsOPToptimizeFn)(cmsPipeline** Lut,
+ cmsUInt32Number Intent,
+ cmsUInt32Number* InputFormat,
+ cmsUInt32Number* OutputFormat,
+ cmsUInt32Number* dwFlags);
+
+// This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional
+// duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality.
+
+CMSAPI void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsPipeline* Lut,
+ _cmsOPTeval16Fn Eval16,
+ void* PrivateData,
+ _cmsFreeUserDataFn FreePrivateDataFn,
+ _cmsDupUserDataFn DupPrivateDataFn);
+
+typedef struct {
+ cmsPluginBase base;
+
+ // Optimize entry point
+ _cmsOPToptimizeFn OptimizePtr;
+
+} cmsPluginOptimization;
+
+//----------------------------------------------------------------------------------------------------------
+// Full xform
+typedef void (* _cmsTransformFn)(struct _cmstransform_struct *CMMcargo,
+ const void* InputBuffer,
+ void* OutputBuffer,
+ cmsUInt32Number Size,
+ cmsUInt32Number Stride);
+
+typedef cmsBool (* _cmsTransformFactory)(_cmsTransformFn* xform,
+ void** UserData,
+ _cmsFreeUserDataFn* FreePrivateDataFn,
+ cmsPipeline** Lut,
+ cmsUInt32Number* InputFormat,
+ cmsUInt32Number* OutputFormat,
+ cmsUInt32Number* dwFlags);
+
+
+// Retrieve user data as specified by the factory
+CMSAPI void CMSEXPORT _cmsSetTransformUserData(struct _cmstransform_struct *CMMcargo, void* ptr, _cmsFreeUserDataFn FreePrivateDataFn);
+CMSAPI void * CMSEXPORT _cmsGetTransformUserData(struct _cmstransform_struct *CMMcargo);
+
+
+// Retrieve formatters
+CMSAPI void CMSEXPORT _cmsGetTransformFormatters16 (struct _cmstransform_struct *CMMcargo, cmsFormatter16* FromInput, cmsFormatter16* ToOutput);
+CMSAPI void CMSEXPORT _cmsGetTransformFormattersFloat(struct _cmstransform_struct *CMMcargo, cmsFormatterFloat* FromInput, cmsFormatterFloat* ToOutput);
+
+typedef struct {
+ cmsPluginBase base;
+
+ // Transform entry point
+ _cmsTransformFactory Factory;
+
+} cmsPluginTransform;
+
+//----------------------------------------------------------------------------------------------------------
+// Mutex
+
+typedef void* (* _cmsCreateMutexFnPtrType)(cmsContext ContextID);
+typedef void (* _cmsDestroyMutexFnPtrType)(cmsContext ContextID, void* mtx);
+typedef cmsBool (* _cmsLockMutexFnPtrType)(cmsContext ContextID, void* mtx);
+typedef void (* _cmsUnlockMutexFnPtrType)(cmsContext ContextID, void* mtx);
+
+typedef struct {
+ cmsPluginBase base;
+
+ _cmsCreateMutexFnPtrType CreateMutexPtr;
+ _cmsDestroyMutexFnPtrType DestroyMutexPtr;
+ _cmsLockMutexFnPtrType LockMutexPtr;
+ _cmsUnlockMutexFnPtrType UnlockMutexPtr;
+
+} cmsPluginMutex;
+
+CMSAPI void* CMSEXPORT _cmsCreateMutex(cmsContext ContextID);
+CMSAPI void CMSEXPORT _cmsDestroyMutex(cmsContext ContextID, void* mtx);
+CMSAPI cmsBool CMSEXPORT _cmsLockMutex(cmsContext ContextID, void* mtx);
+CMSAPI void CMSEXPORT _cmsUnlockMutex(cmsContext ContextID, void* mtx);
+
+
+#ifndef CMS_USE_CPP_API
+# ifdef __cplusplus
+ }
+# endif
+#endif
+
+#define _lcms_plugin_H
+#endif
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscam02.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscam02.c
new file mode 100644
index 0000000000..9d874aa205
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscam02.c
@@ -0,0 +1,486 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// CIECAM 02 appearance model. Many thanks to Jordi Vilar for the debugging.
+
+// ---------- Implementation --------------------------------------------
+
+typedef struct {
+
+ cmsFloat64Number XYZ[3];
+ cmsFloat64Number RGB[3];
+ cmsFloat64Number RGBc[3];
+ cmsFloat64Number RGBp[3];
+ cmsFloat64Number RGBpa[3];
+ cmsFloat64Number a, b, h, e, H, A, J, Q, s, t, C, M;
+ cmsFloat64Number abC[2];
+ cmsFloat64Number abs[2];
+ cmsFloat64Number abM[2];
+
+} CAM02COLOR;
+
+typedef struct {
+
+ CAM02COLOR adoptedWhite;
+ cmsFloat64Number LA, Yb;
+ cmsFloat64Number F, c, Nc;
+ cmsUInt32Number surround;
+ cmsFloat64Number n, Nbb, Ncb, z, FL, D;
+
+ cmsContext ContextID;
+
+} cmsCIECAM02;
+
+
+static
+cmsFloat64Number compute_n(cmsCIECAM02* pMod)
+{
+ return (pMod -> Yb / pMod -> adoptedWhite.XYZ[1]);
+}
+
+static
+cmsFloat64Number compute_z(cmsCIECAM02* pMod)
+{
+ return (1.48 + pow(pMod -> n, 0.5));
+}
+
+static
+cmsFloat64Number computeNbb(cmsCIECAM02* pMod)
+{
+ return (0.725 * pow((1.0 / pMod -> n), 0.2));
+}
+
+static
+cmsFloat64Number computeFL(cmsCIECAM02* pMod)
+{
+ cmsFloat64Number k, FL;
+
+ k = 1.0 / ((5.0 * pMod->LA) + 1.0);
+ FL = 0.2 * pow(k, 4.0) * (5.0 * pMod->LA) + 0.1 *
+ (pow((1.0 - pow(k, 4.0)), 2.0)) *
+ (pow((5.0 * pMod->LA), (1.0 / 3.0)));
+
+ return FL;
+}
+
+static
+cmsFloat64Number computeD(cmsCIECAM02* pMod)
+{
+ cmsFloat64Number D;
+
+ D = pMod->F - (1.0/3.6)*(exp(((-pMod ->LA-42) / 92.0)));
+
+ return D;
+}
+
+
+static
+CAM02COLOR XYZtoCAT02(CAM02COLOR clr)
+{
+ clr.RGB[0] = (clr.XYZ[0] * 0.7328) + (clr.XYZ[1] * 0.4296) + (clr.XYZ[2] * -0.1624);
+ clr.RGB[1] = (clr.XYZ[0] * -0.7036) + (clr.XYZ[1] * 1.6975) + (clr.XYZ[2] * 0.0061);
+ clr.RGB[2] = (clr.XYZ[0] * 0.0030) + (clr.XYZ[1] * 0.0136) + (clr.XYZ[2] * 0.9834);
+
+ return clr;
+}
+
+static
+CAM02COLOR ChromaticAdaptation(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+ cmsUInt32Number i;
+
+ for (i = 0; i < 3; i++) {
+ clr.RGBc[i] = ((pMod -> adoptedWhite.XYZ[1] *
+ (pMod->D / pMod -> adoptedWhite.RGB[i])) +
+ (1.0 - pMod->D)) * clr.RGB[i];
+ }
+
+ return clr;
+}
+
+
+static
+CAM02COLOR CAT02toHPE(CAM02COLOR clr)
+{
+ cmsFloat64Number M[9];
+
+ M[0] =(( 0.38971 * 1.096124) + (0.68898 * 0.454369) + (-0.07868 * -0.009628));
+ M[1] =(( 0.38971 * -0.278869) + (0.68898 * 0.473533) + (-0.07868 * -0.005698));
+ M[2] =(( 0.38971 * 0.182745) + (0.68898 * 0.072098) + (-0.07868 * 1.015326));
+ M[3] =((-0.22981 * 1.096124) + (1.18340 * 0.454369) + ( 0.04641 * -0.009628));
+ M[4] =((-0.22981 * -0.278869) + (1.18340 * 0.473533) + ( 0.04641 * -0.005698));
+ M[5] =((-0.22981 * 0.182745) + (1.18340 * 0.072098) + ( 0.04641 * 1.015326));
+ M[6] =(-0.009628);
+ M[7] =(-0.005698);
+ M[8] =( 1.015326);
+
+ clr.RGBp[0] = (clr.RGBc[0] * M[0]) + (clr.RGBc[1] * M[1]) + (clr.RGBc[2] * M[2]);
+ clr.RGBp[1] = (clr.RGBc[0] * M[3]) + (clr.RGBc[1] * M[4]) + (clr.RGBc[2] * M[5]);
+ clr.RGBp[2] = (clr.RGBc[0] * M[6]) + (clr.RGBc[1] * M[7]) + (clr.RGBc[2] * M[8]);
+
+ return clr;
+}
+
+static
+CAM02COLOR NonlinearCompression(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+ cmsUInt32Number i;
+ cmsFloat64Number temp;
+
+ for (i = 0; i < 3; i++) {
+ if (clr.RGBp[i] < 0) {
+
+ temp = pow((-1.0 * pMod->FL * clr.RGBp[i] / 100.0), 0.42);
+ clr.RGBpa[i] = (-1.0 * 400.0 * temp) / (temp + 27.13) + 0.1;
+ }
+ else {
+ temp = pow((pMod->FL * clr.RGBp[i] / 100.0), 0.42);
+ clr.RGBpa[i] = (400.0 * temp) / (temp + 27.13) + 0.1;
+ }
+ }
+
+ clr.A = (((2.0 * clr.RGBpa[0]) + clr.RGBpa[1] +
+ (clr.RGBpa[2] / 20.0)) - 0.305) * pMod->Nbb;
+
+ return clr;
+}
+
+static
+CAM02COLOR ComputeCorrelates(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+ cmsFloat64Number a, b, temp, e, t, r2d, d2r;
+
+ a = clr.RGBpa[0] - (12.0 * clr.RGBpa[1] / 11.0) + (clr.RGBpa[2] / 11.0);
+ b = (clr.RGBpa[0] + clr.RGBpa[1] - (2.0 * clr.RGBpa[2])) / 9.0;
+
+ r2d = (180.0 / 3.141592654);
+ if (a == 0) {
+ if (b == 0) clr.h = 0;
+ else if (b > 0) clr.h = 90;
+ else clr.h = 270;
+ }
+ else if (a > 0) {
+ temp = b / a;
+ if (b > 0) clr.h = (r2d * atan(temp));
+ else if (b == 0) clr.h = 0;
+ else clr.h = (r2d * atan(temp)) + 360;
+ }
+ else {
+ temp = b / a;
+ clr.h = (r2d * atan(temp)) + 180;
+ }
+
+ d2r = (3.141592654 / 180.0);
+ e = ((12500.0 / 13.0) * pMod->Nc * pMod->Ncb) *
+ (cos((clr.h * d2r + 2.0)) + 3.8);
+
+ if (clr.h < 20.14) {
+ temp = ((clr.h + 122.47)/1.2) + ((20.14 - clr.h)/0.8);
+ clr.H = 300 + (100*((clr.h + 122.47)/1.2)) / temp;
+ }
+ else if (clr.h < 90.0) {
+ temp = ((clr.h - 20.14)/0.8) + ((90.00 - clr.h)/0.7);
+ clr.H = (100*((clr.h - 20.14)/0.8)) / temp;
+ }
+ else if (clr.h < 164.25) {
+ temp = ((clr.h - 90.00)/0.7) + ((164.25 - clr.h)/1.0);
+ clr.H = 100 + ((100*((clr.h - 90.00)/0.7)) / temp);
+ }
+ else if (clr.h < 237.53) {
+ temp = ((clr.h - 164.25)/1.0) + ((237.53 - clr.h)/1.2);
+ clr.H = 200 + ((100*((clr.h - 164.25)/1.0)) / temp);
+ }
+ else {
+ temp = ((clr.h - 237.53)/1.2) + ((360 - clr.h + 20.14)/0.8);
+ clr.H = 300 + ((100*((clr.h - 237.53)/1.2)) / temp);
+ }
+
+ clr.J = 100.0 * pow((clr.A / pMod->adoptedWhite.A),
+ (pMod->c * pMod->z));
+
+ clr.Q = (4.0 / pMod->c) * pow((clr.J / 100.0), 0.5) *
+ (pMod->adoptedWhite.A + 4.0) * pow(pMod->FL, 0.25);
+
+ t = (e * pow(((a * a) + (b * b)), 0.5)) /
+ (clr.RGBpa[0] + clr.RGBpa[1] +
+ ((21.0 / 20.0) * clr.RGBpa[2]));
+
+ clr.C = pow(t, 0.9) * pow((clr.J / 100.0), 0.5) *
+ pow((1.64 - pow(0.29, pMod->n)), 0.73);
+
+ clr.M = clr.C * pow(pMod->FL, 0.25);
+ clr.s = 100.0 * pow((clr.M / clr.Q), 0.5);
+
+ return clr;
+}
+
+
+static
+CAM02COLOR InverseCorrelates(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+
+ cmsFloat64Number t, e, p1, p2, p3, p4, p5, hr, d2r;
+ d2r = 3.141592654 / 180.0;
+
+ t = pow( (clr.C / (pow((clr.J / 100.0), 0.5) *
+ (pow((1.64 - pow(0.29, pMod->n)), 0.73)))),
+ (1.0 / 0.9) );
+ e = ((12500.0 / 13.0) * pMod->Nc * pMod->Ncb) *
+ (cos((clr.h * d2r + 2.0)) + 3.8);
+
+ clr.A = pMod->adoptedWhite.A * pow(
+ (clr.J / 100.0),
+ (1.0 / (pMod->c * pMod->z)));
+
+ p1 = e / t;
+ p2 = (clr.A / pMod->Nbb) + 0.305;
+ p3 = 21.0 / 20.0;
+
+ hr = clr.h * d2r;
+
+ if (fabs(sin(hr)) >= fabs(cos(hr))) {
+ p4 = p1 / sin(hr);
+ clr.b = (p2 * (2.0 + p3) * (460.0 / 1403.0)) /
+ (p4 + (2.0 + p3) * (220.0 / 1403.0) *
+ (cos(hr) / sin(hr)) - (27.0 / 1403.0) +
+ p3 * (6300.0 / 1403.0));
+ clr.a = clr.b * (cos(hr) / sin(hr));
+ }
+ else {
+ p5 = p1 / cos(hr);
+ clr.a = (p2 * (2.0 + p3) * (460.0 / 1403.0)) /
+ (p5 + (2.0 + p3) * (220.0 / 1403.0) -
+ ((27.0 / 1403.0) - p3 * (6300.0 / 1403.0)) *
+ (sin(hr) / cos(hr)));
+ clr.b = clr.a * (sin(hr) / cos(hr));
+ }
+
+ clr.RGBpa[0] = ((460.0 / 1403.0) * p2) +
+ ((451.0 / 1403.0) * clr.a) +
+ ((288.0 / 1403.0) * clr.b);
+ clr.RGBpa[1] = ((460.0 / 1403.0) * p2) -
+ ((891.0 / 1403.0) * clr.a) -
+ ((261.0 / 1403.0) * clr.b);
+ clr.RGBpa[2] = ((460.0 / 1403.0) * p2) -
+ ((220.0 / 1403.0) * clr.a) -
+ ((6300.0 / 1403.0) * clr.b);
+
+ return clr;
+}
+
+static
+CAM02COLOR InverseNonlinearity(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+ cmsUInt32Number i;
+ cmsFloat64Number c1;
+
+ for (i = 0; i < 3; i++) {
+ if ((clr.RGBpa[i] - 0.1) < 0) c1 = -1;
+ else c1 = 1;
+ clr.RGBp[i] = c1 * (100.0 / pMod->FL) *
+ pow(((27.13 * fabs(clr.RGBpa[i] - 0.1)) /
+ (400.0 - fabs(clr.RGBpa[i] - 0.1))),
+ (1.0 / 0.42));
+ }
+
+ return clr;
+}
+
+static
+CAM02COLOR HPEtoCAT02(CAM02COLOR clr)
+{
+ cmsFloat64Number M[9];
+
+ M[0] = (( 0.7328 * 1.910197) + (0.4296 * 0.370950));
+ M[1] = (( 0.7328 * -1.112124) + (0.4296 * 0.629054));
+ M[2] = (( 0.7328 * 0.201908) + (0.4296 * 0.000008) - 0.1624);
+ M[3] = ((-0.7036 * 1.910197) + (1.6975 * 0.370950));
+ M[4] = ((-0.7036 * -1.112124) + (1.6975 * 0.629054));
+ M[5] = ((-0.7036 * 0.201908) + (1.6975 * 0.000008) + 0.0061);
+ M[6] = (( 0.0030 * 1.910197) + (0.0136 * 0.370950));
+ M[7] = (( 0.0030 * -1.112124) + (0.0136 * 0.629054));
+ M[8] = (( 0.0030 * 0.201908) + (0.0136 * 0.000008) + 0.9834);;
+
+ clr.RGBc[0] = (clr.RGBp[0] * M[0]) + (clr.RGBp[1] * M[1]) + (clr.RGBp[2] * M[2]);
+ clr.RGBc[1] = (clr.RGBp[0] * M[3]) + (clr.RGBp[1] * M[4]) + (clr.RGBp[2] * M[5]);
+ clr.RGBc[2] = (clr.RGBp[0] * M[6]) + (clr.RGBp[1] * M[7]) + (clr.RGBp[2] * M[8]);
+ return clr;
+}
+
+
+static
+CAM02COLOR InverseChromaticAdaptation(CAM02COLOR clr, cmsCIECAM02* pMod)
+{
+ cmsUInt32Number i;
+ for (i = 0; i < 3; i++) {
+ clr.RGB[i] = clr.RGBc[i] /
+ ((pMod->adoptedWhite.XYZ[1] * pMod->D / pMod->adoptedWhite.RGB[i]) + 1.0 - pMod->D);
+ }
+ return clr;
+}
+
+
+static
+CAM02COLOR CAT02toXYZ(CAM02COLOR clr)
+{
+ clr.XYZ[0] = (clr.RGB[0] * 1.096124) + (clr.RGB[1] * -0.278869) + (clr.RGB[2] * 0.182745);
+ clr.XYZ[1] = (clr.RGB[0] * 0.454369) + (clr.RGB[1] * 0.473533) + (clr.RGB[2] * 0.072098);
+ clr.XYZ[2] = (clr.RGB[0] * -0.009628) + (clr.RGB[1] * -0.005698) + (clr.RGB[2] * 1.015326);
+
+ return clr;
+}
+
+
+cmsHANDLE CMSEXPORT cmsCIECAM02Init(cmsContext ContextID, const cmsViewingConditions* pVC)
+{
+ cmsCIECAM02* lpMod;
+
+ _cmsAssert(pVC != NULL);
+
+ if((lpMod = (cmsCIECAM02*) _cmsMallocZero(ContextID, sizeof(cmsCIECAM02))) == NULL) {
+ return NULL;
+ }
+
+ lpMod ->ContextID = ContextID;
+
+ lpMod ->adoptedWhite.XYZ[0] = pVC ->whitePoint.X;
+ lpMod ->adoptedWhite.XYZ[1] = pVC ->whitePoint.Y;
+ lpMod ->adoptedWhite.XYZ[2] = pVC ->whitePoint.Z;
+
+ lpMod -> LA = pVC ->La;
+ lpMod -> Yb = pVC ->Yb;
+ lpMod -> D = pVC ->D_value;
+ lpMod -> surround = pVC ->surround;
+
+ switch (lpMod -> surround) {
+
+
+ case CUTSHEET_SURROUND:
+ lpMod->F = 0.8;
+ lpMod->c = 0.41;
+ lpMod->Nc = 0.8;
+ break;
+
+ case DARK_SURROUND:
+ lpMod -> F = 0.8;
+ lpMod -> c = 0.525;
+ lpMod -> Nc = 0.8;
+ break;
+
+ case DIM_SURROUND:
+ lpMod -> F = 0.9;
+ lpMod -> c = 0.59;
+ lpMod -> Nc = 0.95;
+ break;
+
+ default:
+ // Average surround
+ lpMod -> F = 1.0;
+ lpMod -> c = 0.69;
+ lpMod -> Nc = 1.0;
+ }
+
+ lpMod -> n = compute_n(lpMod);
+ lpMod -> z = compute_z(lpMod);
+ lpMod -> Nbb = computeNbb(lpMod);
+ lpMod -> FL = computeFL(lpMod);
+
+ if (lpMod -> D == D_CALCULATE) {
+ lpMod -> D = computeD(lpMod);
+ }
+
+ lpMod -> Ncb = lpMod -> Nbb;
+
+ lpMod -> adoptedWhite = XYZtoCAT02(lpMod -> adoptedWhite);
+ lpMod -> adoptedWhite = ChromaticAdaptation(lpMod -> adoptedWhite, lpMod);
+ lpMod -> adoptedWhite = CAT02toHPE(lpMod -> adoptedWhite);
+ lpMod -> adoptedWhite = NonlinearCompression(lpMod -> adoptedWhite, lpMod);
+
+ return (cmsHANDLE) lpMod;
+
+}
+
+void CMSEXPORT cmsCIECAM02Done(cmsHANDLE hModel)
+{
+ cmsCIECAM02* lpMod = (cmsCIECAM02*) hModel;
+
+ if (lpMod) _cmsFree(lpMod ->ContextID, lpMod);
+}
+
+
+void CMSEXPORT cmsCIECAM02Forward(cmsHANDLE hModel, const cmsCIEXYZ* pIn, cmsJCh* pOut)
+{
+ CAM02COLOR clr;
+ cmsCIECAM02* lpMod = (cmsCIECAM02*) hModel;
+
+ _cmsAssert(lpMod != NULL);
+ _cmsAssert(pIn != NULL);
+ _cmsAssert(pOut != NULL);
+
+ memset(&clr, 0, sizeof(clr));
+
+ clr.XYZ[0] = pIn ->X;
+ clr.XYZ[1] = pIn ->Y;
+ clr.XYZ[2] = pIn ->Z;
+
+ clr = XYZtoCAT02(clr);
+ clr = ChromaticAdaptation(clr, lpMod);
+ clr = CAT02toHPE(clr);
+ clr = NonlinearCompression(clr, lpMod);
+ clr = ComputeCorrelates(clr, lpMod);
+
+ pOut ->J = clr.J;
+ pOut ->C = clr.C;
+ pOut ->h = clr.h;
+}
+
+void CMSEXPORT cmsCIECAM02Reverse(cmsHANDLE hModel, const cmsJCh* pIn, cmsCIEXYZ* pOut)
+{
+ CAM02COLOR clr;
+ cmsCIECAM02* lpMod = (cmsCIECAM02*) hModel;
+
+ _cmsAssert(lpMod != NULL);
+ _cmsAssert(pIn != NULL);
+ _cmsAssert(pOut != NULL);
+
+ memset(&clr, 0, sizeof(clr));
+
+ clr.J = pIn -> J;
+ clr.C = pIn -> C;
+ clr.h = pIn -> h;
+
+ clr = InverseCorrelates(clr, lpMod);
+ clr = InverseNonlinearity(clr, lpMod);
+ clr = HPEtoCAT02(clr);
+ clr = InverseChromaticAdaptation(clr, lpMod);
+ clr = CAT02toXYZ(clr);
+
+ pOut ->X = clr.XYZ[0];
+ pOut ->Y = clr.XYZ[1];
+ pOut ->Z = clr.XYZ[2];
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscgats.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscgats.c
new file mode 100644
index 0000000000..90174351e3
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscgats.c
@@ -0,0 +1,2775 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+
+// IT8.7 / CGATS.17-200x handling -----------------------------------------------------------------------------
+
+
+#define MAXID 128 // Max length of identifier
+#define MAXSTR 1024 // Max length of string
+#define MAXTABLES 255 // Max Number of tables in a single stream
+#define MAXINCLUDE 20 // Max number of nested includes
+
+#define DEFAULT_DBL_FORMAT "%.10g" // Double formatting
+
+#ifdef CMS_IS_WINDOWS_
+//sunliang.liu modified 2010426 for wince error
+# ifndef _WIN32_WCE
+# include <io.h>
+# endif
+# define DIR_CHAR '\\'
+#else
+# define DIR_CHAR '/'
+#endif
+
+
+// Symbols
+typedef enum {
+
+ SNONE,
+ SINUM, // Integer
+ SDNUM, // Real
+ SIDENT, // Identifier
+ SSTRING, // string
+ SCOMMENT, // comment
+ SEOLN, // End of line
+ SEOF, // End of stream
+ SSYNERROR, // Syntax error found on stream
+
+ // Keywords
+
+ SBEGIN_DATA,
+ SBEGIN_DATA_FORMAT,
+ SEND_DATA,
+ SEND_DATA_FORMAT,
+ SKEYWORD,
+ SDATA_FORMAT_ID,
+ SINCLUDE
+
+ } SYMBOL;
+
+
+// How to write the value
+typedef enum {
+
+ WRITE_UNCOOKED,
+ WRITE_STRINGIFY,
+ WRITE_HEXADECIMAL,
+ WRITE_BINARY,
+ WRITE_PAIR
+
+ } WRITEMODE;
+
+// Linked list of variable names
+typedef struct _KeyVal {
+
+ struct _KeyVal* Next;
+ char* Keyword; // Name of variable
+ struct _KeyVal* NextSubkey; // If key is a dictionary, points to the next item
+ char* Subkey; // If key is a dictionary, points to the subkey name
+ char* Value; // Points to value
+ WRITEMODE WriteAs; // How to write the value
+
+ } KEYVALUE;
+
+
+// Linked list of memory chunks (Memory sink)
+typedef struct _OwnedMem {
+
+ struct _OwnedMem* Next;
+ void * Ptr; // Point to value
+
+ } OWNEDMEM;
+
+// Suballocator
+typedef struct _SubAllocator {
+
+ cmsUInt8Number* Block;
+ cmsUInt32Number BlockSize;
+ cmsUInt32Number Used;
+
+ } SUBALLOCATOR;
+
+// Table. Each individual table can hold properties and rows & cols
+typedef struct _Table {
+
+ char SheetType[MAXSTR]; // The first row of the IT8 (the type)
+
+ int nSamples, nPatches; // Cols, Rows
+ int SampleID; // Pos of ID
+
+ KEYVALUE* HeaderList; // The properties
+
+ char** DataFormat; // The binary stream descriptor
+ char** Data; // The binary stream
+
+ } TABLE;
+
+// File stream being parsed
+typedef struct _FileContext {
+ char FileName[cmsMAX_PATH]; // File name if being readed from file
+ FILE* Stream; // File stream or NULL if holded in memory
+ } FILECTX;
+
+// This struct hold all information about an open IT8 handler.
+typedef struct {
+
+
+ cmsUInt32Number TablesCount; // How many tables in this stream
+ cmsUInt32Number nTable; // The actual table
+
+ TABLE Tab[MAXTABLES];
+
+ // Memory management
+ OWNEDMEM* MemorySink; // The storage backend
+ SUBALLOCATOR Allocator; // String suballocator -- just to keep it fast
+
+ // Parser state machine
+ SYMBOL sy; // Current symbol
+ int ch; // Current character
+
+ int inum; // integer value
+ cmsFloat64Number dnum; // real value
+ char id[MAXID]; // identifier
+ char str[MAXSTR]; // string
+
+ // Allowed keywords & datasets. They have visibility on whole stream
+ KEYVALUE* ValidKeywords;
+ KEYVALUE* ValidSampleID;
+
+ char* Source; // Points to loc. being parsed
+ int lineno; // line counter for error reporting
+
+ FILECTX* FileStack[MAXINCLUDE]; // Stack of files being parsed
+ int IncludeSP; // Include Stack Pointer
+
+ char* MemoryBlock; // The stream if holded in memory
+
+ char DoubleFormatter[MAXID];// Printf-like 'cmsFloat64Number' formatter
+
+ cmsContext ContextID; // The threading context
+
+ } cmsIT8;
+
+
+// The stream for save operations
+typedef struct {
+
+ FILE* stream; // For save-to-file behaviour
+
+ cmsUInt8Number* Base;
+ cmsUInt8Number* Ptr; // For save-to-mem behaviour
+ cmsUInt32Number Used;
+ cmsUInt32Number Max;
+
+ } SAVESTREAM;
+
+
+// ------------------------------------------------------ cmsIT8 parsing routines
+
+
+// A keyword
+typedef struct {
+
+ const char *id;
+ SYMBOL sy;
+
+ } KEYWORD;
+
+// The keyword->symbol translation table. Sorting is required.
+static const KEYWORD TabKeys[] = {
+
+ {"$INCLUDE", SINCLUDE}, // This is an extension!
+ {".INCLUDE", SINCLUDE}, // This is an extension!
+
+ {"BEGIN_DATA", SBEGIN_DATA },
+ {"BEGIN_DATA_FORMAT", SBEGIN_DATA_FORMAT },
+ {"DATA_FORMAT_IDENTIFIER", SDATA_FORMAT_ID},
+ {"END_DATA", SEND_DATA},
+ {"END_DATA_FORMAT", SEND_DATA_FORMAT},
+ {"KEYWORD", SKEYWORD}
+ };
+
+#define NUMKEYS (sizeof(TabKeys)/sizeof(KEYWORD))
+
+// Predefined properties
+
+// A property
+typedef struct {
+ const char *id; // The identifier
+ WRITEMODE as; // How is supposed to be written
+ } PROPERTY;
+
+static PROPERTY PredefinedProperties[] = {
+
+ {"NUMBER_OF_FIELDS", WRITE_UNCOOKED}, // Required - NUMBER OF FIELDS
+ {"NUMBER_OF_SETS", WRITE_UNCOOKED}, // Required - NUMBER OF SETS
+ {"ORIGINATOR", WRITE_STRINGIFY}, // Required - Identifies the specific system, organization or individual that created the data file.
+ {"FILE_DESCRIPTOR", WRITE_STRINGIFY}, // Required - Describes the purpose or contents of the data file.
+ {"CREATED", WRITE_STRINGIFY}, // Required - Indicates date of creation of the data file.
+ {"DESCRIPTOR", WRITE_STRINGIFY}, // Required - Describes the purpose or contents of the data file.
+ {"DIFFUSE_GEOMETRY", WRITE_STRINGIFY}, // The diffuse geometry used. Allowed values are "sphere" or "opal".
+ {"MANUFACTURER", WRITE_STRINGIFY},
+ {"MANUFACTURE", WRITE_STRINGIFY}, // Some broken Fuji targets does store this value
+ {"PROD_DATE", WRITE_STRINGIFY}, // Identifies year and month of production of the target in the form yyyy:mm.
+ {"SERIAL", WRITE_STRINGIFY}, // Uniquely identifies individual physical target.
+
+ {"MATERIAL", WRITE_STRINGIFY}, // Identifies the material on which the target was produced using a code
+ // uniquely identifying th e material. This is intend ed to be used for IT8.7
+ // physical targets only (i.e . IT8.7/1 a nd IT8.7/2).
+
+ {"INSTRUMENTATION", WRITE_STRINGIFY}, // Used to report the specific instrumentation used (manufacturer and
+ // model number) to generate the data reported. This data will often
+ // provide more information about the particular data collected than an
+ // extensive list of specific details. This is particularly important for
+ // spectral data or data derived from spectrophotometry.
+
+ {"MEASUREMENT_SOURCE", WRITE_STRINGIFY}, // Illumination used for spectral measurements. This data helps provide
+ // a guide to the potential for issues of paper fluorescence, etc.
+
+ {"PRINT_CONDITIONS", WRITE_STRINGIFY}, // Used to define the characteristics of the printed sheet being reported.
+ // Where standard conditions have been defined (e.g., SWOP at nominal)
+ // named conditions may suffice. Otherwise, detailed information is
+ // needed.
+
+ {"SAMPLE_BACKING", WRITE_STRINGIFY}, // Identifies the backing material used behind the sample during
+ // measurement. Allowed values are “black? “white? or {"na".
+
+ {"CHISQ_DOF", WRITE_STRINGIFY}, // Degrees of freedom associated with the Chi squared statistic
+
+ // below properties are new in recent specs:
+
+ {"MEASUREMENT_GEOMETRY", WRITE_STRINGIFY}, // The type of measurement, either reflection or transmission, should be indicated
+ // along with details of the geometry and the aperture size and shape. For example,
+ // for transmission measurements it is important to identify 0/diffuse, diffuse/0,
+ // opal or integrating sphere, etc. For reflection it is important to identify 0/45,
+ // 45/0, sphere (specular included or excluded), etc.
+
+ {"FILTER", WRITE_STRINGIFY}, // Identifies the use of physical filter(s) during measurement. Typically used to
+ // denote the use of filters such as none, D65, Red, Green or Blue.
+
+ {"POLARIZATION", WRITE_STRINGIFY}, // Identifies the use of a physical polarization filter during measurement. Allowed
+ // values are {"yes? “white? “none?or “na?
+
+ {"WEIGHTING_FUNCTION", WRITE_PAIR}, // Indicates such functions as: the CIE standard observer functions used in the
+ // calculation of various data parameters (2 degree and 10 degree), CIE standard
+ // illuminant functions used in the calculation of various data parameters (e.g., D50,
+ // D65, etc.), density status response, etc. If used there shall be at least one
+ // name-value pair following the WEIGHTING_FUNCTION tag/keyword. The first attribute
+ // in the set shall be {"name" and shall identify the particular parameter used.
+ // The second shall be {"value" and shall provide the value associated with that name.
+ // For ASCII data, a string containing the Name and Value attribute pairs shall follow
+ // the weighting function keyword. A semi-colon separates attribute pairs from each
+ // other and within the attribute the name and value are separated by a comma.
+
+ {"COMPUTATIONAL_PARAMETER", WRITE_PAIR}, // Parameter that is used in computing a value from measured data. Name is the name
+ // of the calculation, parameter is the name of the parameter used in the calculation
+ // and value is the value of the parameter.
+
+ {"TARGET_TYPE", WRITE_STRINGIFY}, // The type of target being measured, e.g. IT8.7/1, IT8.7/3, user defined, etc.
+
+ {"COLORANT", WRITE_STRINGIFY}, // Identifies the colorant(s) used in creating the target.
+
+ {"TABLE_DESCRIPTOR", WRITE_STRINGIFY}, // Describes the purpose or contents of a data table.
+
+ {"TABLE_NAME", WRITE_STRINGIFY} // Provides a short name for a data table.
+};
+
+#define NUMPREDEFINEDPROPS (sizeof(PredefinedProperties)/sizeof(PROPERTY))
+
+
+// Predefined sample types on dataset
+static const char* PredefinedSampleID[] = {
+ "SAMPLE_ID", // Identifies sample that data represents
+ "STRING", // Identifies label, or other non-machine readable value.
+ // Value must begin and end with a " symbol
+
+ "CMYK_C", // Cyan component of CMYK data expressed as a percentage
+ "CMYK_M", // Magenta component of CMYK data expressed as a percentage
+ "CMYK_Y", // Yellow component of CMYK data expressed as a percentage
+ "CMYK_K", // Black component of CMYK data expressed as a percentage
+ "D_RED", // Red filter density
+ "D_GREEN", // Green filter density
+ "D_BLUE", // Blue filter density
+ "D_VIS", // Visual filter density
+ "D_MAJOR_FILTER", // Major filter d ensity
+ "RGB_R", // Red component of RGB data
+ "RGB_G", // Green component of RGB data
+ "RGB_B", // Blue com ponent of RGB data
+ "SPECTRAL_NM", // Wavelength of measurement expressed in nanometers
+ "SPECTRAL_PCT", // Percentage reflectance/transmittance
+ "SPECTRAL_DEC", // Reflectance/transmittance
+ "XYZ_X", // X component of tristimulus data
+ "XYZ_Y", // Y component of tristimulus data
+ "XYZ_Z", // Z component of tristimulus data
+ "XYY_X" // x component of chromaticity data
+ "XYY_Y", // y component of chromaticity data
+ "XYY_CAPY", // Y component of tristimulus data
+ "LAB_L", // L* component of Lab data
+ "LAB_A", // a* component of Lab data
+ "LAB_B", // b* component of Lab data
+ "LAB_C", // C*ab component of Lab data
+ "LAB_H", // hab component of Lab data
+ "LAB_DE", // CIE dE
+ "LAB_DE_94", // CIE dE using CIE 94
+ "LAB_DE_CMC", // dE using CMC
+ "LAB_DE_2000", // CIE dE using CIE DE 2000
+ "MEAN_DE", // Mean Delta E (LAB_DE) of samples compared to batch average
+ // (Used for data files for ANSI IT8.7/1 and IT8.7/2 targets)
+ "STDEV_X", // Standard deviation of X (tristimulus data)
+ "STDEV_Y", // Standard deviation of Y (tristimulus data)
+ "STDEV_Z", // Standard deviation of Z (tristimulus data)
+ "STDEV_L", // Standard deviation of L*
+ "STDEV_A", // Standard deviation of a*
+ "STDEV_B", // Standard deviation of b*
+ "STDEV_DE", // Standard deviation of CIE dE
+ "CHI_SQD_PAR"}; // The average of the standard deviations of L*, a* and b*. It is
+ // used to derive an estimate of the chi-squared parameter which is
+ // recommended as the predictor of the variability of dE
+
+#define NUMPREDEFINEDSAMPLEID (sizeof(PredefinedSampleID)/sizeof(char *))
+
+//Forward declaration of some internal functions
+static void* AllocChunk(cmsIT8* it8, cmsUInt32Number size);
+
+// Checks whatever c is a separator
+static
+cmsBool isseparator(int c)
+{
+ return (c == ' ') || (c == '\t') ;
+}
+
+// Checks whatever c is a valid identifier char
+static
+cmsBool ismiddle(int c)
+{
+ return (!isseparator(c) && (c != '#') && (c !='\"') && (c != '\'') && (c > 32) && (c < 127));
+}
+
+// Checks whatsever c is a valid identifier middle char.
+static
+cmsBool isidchar(int c)
+{
+ return isalnum(c) || ismiddle(c);
+}
+
+// Checks whatsever c is a valid identifier first char.
+static
+cmsBool isfirstidchar(int c)
+{
+ return !isdigit(c) && ismiddle(c);
+}
+
+// Guess whether the supplied path looks like an absolute path
+static
+cmsBool isabsolutepath(const char *path)
+{
+ char ThreeChars[4];
+
+ if(path == NULL)
+ return FALSE;
+ if (path[0] == 0)
+ return FALSE;
+
+ strncpy(ThreeChars, path, 3);
+ ThreeChars[3] = 0;
+
+ if(ThreeChars[0] == DIR_CHAR)
+ return TRUE;
+
+#ifdef CMS_IS_WINDOWS_
+ if (isalpha((int) ThreeChars[0]) && ThreeChars[1] == ':')
+ return TRUE;
+#endif
+ return FALSE;
+}
+
+
+// Makes a file path based on a given reference path
+// NOTE: this function doesn't check if the path exists or even if it's legal
+static
+cmsBool BuildAbsolutePath(const char *relPath, const char *basePath, char *buffer, cmsUInt32Number MaxLen)
+{
+ char *tail;
+ cmsUInt32Number len;
+
+ // Already absolute?
+ if (isabsolutepath(relPath)) {
+
+ strncpy(buffer, relPath, MaxLen);
+ buffer[MaxLen-1] = 0;
+ return TRUE;
+ }
+
+ // No, search for last
+ strncpy(buffer, basePath, MaxLen);
+ buffer[MaxLen-1] = 0;
+
+ tail = strrchr(buffer, DIR_CHAR);
+ if (tail == NULL) return FALSE; // Is not absolute and has no separators??
+
+ len = (cmsUInt32Number) (tail - buffer);
+ if (len >= MaxLen) return FALSE;
+
+ // No need to assure zero terminator over here
+ strncpy(tail + 1, relPath, MaxLen - len);
+
+ return TRUE;
+}
+
+
+// Make sure no exploit is being even tried
+static
+const char* NoMeta(const char* str)
+{
+ if (strchr(str, '%') != NULL)
+ return "**** CORRUPTED FORMAT STRING ***";
+
+ return str;
+}
+
+// Syntax error
+static
+cmsBool SynError(cmsIT8* it8, const char *Txt, ...)
+{
+ char Buffer[256], ErrMsg[1024];
+ va_list args;
+
+ va_start(args, Txt);
+ vsnprintf(Buffer, 255, Txt, args);
+ Buffer[255] = 0;
+ va_end(args);
+
+ snprintf(ErrMsg, 1023, "%s: Line %d, %s", it8->FileStack[it8 ->IncludeSP]->FileName, it8->lineno, Buffer);
+ ErrMsg[1023] = 0;
+ it8->sy = SSYNERROR;
+ cmsSignalError(it8 ->ContextID, cmsERROR_CORRUPTION_DETECTED, "%s", ErrMsg);
+ return FALSE;
+}
+
+// Check if current symbol is same as specified. issue an error else.
+static
+cmsBool Check(cmsIT8* it8, SYMBOL sy, const char* Err)
+{
+ if (it8 -> sy != sy)
+ return SynError(it8, NoMeta(Err));
+ return TRUE;
+}
+
+// Read Next character from stream
+static
+void NextCh(cmsIT8* it8)
+{
+ if (it8 -> FileStack[it8 ->IncludeSP]->Stream) {
+
+ it8 ->ch = fgetc(it8 ->FileStack[it8 ->IncludeSP]->Stream);
+
+ if (feof(it8 -> FileStack[it8 ->IncludeSP]->Stream)) {
+
+ if (it8 ->IncludeSP > 0) {
+
+ fclose(it8 ->FileStack[it8->IncludeSP--]->Stream);
+ it8 -> ch = ' '; // Whitespace to be ignored
+
+ } else
+ it8 ->ch = 0; // EOF
+ }
+ }
+ else {
+ it8->ch = *it8->Source;
+ if (it8->ch) it8->Source++;
+ }
+}
+
+
+// Try to see if current identifier is a keyword, if so return the referred symbol
+static
+SYMBOL BinSrchKey(const char *id)
+{
+ int l = 1;
+ int r = NUMKEYS;
+ int x, res;
+
+ while (r >= l)
+ {
+ x = (l+r)/2;
+ res = cmsstrcasecmp(id, TabKeys[x-1].id);
+ if (res == 0) return TabKeys[x-1].sy;
+ if (res < 0) r = x - 1;
+ else l = x + 1;
+ }
+
+ return SNONE;
+}
+
+
+// 10 ^n
+static
+cmsFloat64Number xpow10(int n)
+{
+ return pow(10, (cmsFloat64Number) n);
+}
+
+
+// Reads a Real number, tries to follow from integer number
+static
+void ReadReal(cmsIT8* it8, int inum)
+{
+ it8->dnum = (cmsFloat64Number) inum;
+
+ while (isdigit(it8->ch)) {
+
+ it8->dnum = it8->dnum * 10.0 + (it8->ch - '0');
+ NextCh(it8);
+ }
+
+ if (it8->ch == '.') { // Decimal point
+
+ cmsFloat64Number frac = 0.0; // fraction
+ int prec = 0; // precision
+
+ NextCh(it8); // Eats dec. point
+
+ while (isdigit(it8->ch)) {
+
+ frac = frac * 10.0 + (it8->ch - '0');
+ prec++;
+ NextCh(it8);
+ }
+
+ it8->dnum = it8->dnum + (frac / xpow10(prec));
+ }
+
+ // Exponent, example 34.00E+20
+ if (toupper(it8->ch) == 'E') {
+
+ int e;
+ int sgn;
+
+ NextCh(it8); sgn = 1;
+
+ if (it8->ch == '-') {
+
+ sgn = -1; NextCh(it8);
+ }
+ else
+ if (it8->ch == '+') {
+
+ sgn = +1;
+ NextCh(it8);
+ }
+
+ e = 0;
+ while (isdigit(it8->ch)) {
+
+ if ((cmsFloat64Number) e * 10L < INT_MAX)
+ e = e * 10 + (it8->ch - '0');
+
+ NextCh(it8);
+ }
+
+ e = sgn*e;
+ it8 -> dnum = it8 -> dnum * xpow10(e);
+ }
+}
+
+// Parses a float number
+// This can not call directly atof because it uses locale dependant
+// parsing, while CCMX files always use . as decimal separator
+static
+cmsFloat64Number ParseFloatNumber(const char *Buffer)
+{
+ cmsFloat64Number dnum = 0.0;
+ int sign = 1;
+
+ // keep safe
+ if (Buffer == NULL) return 0.0;
+
+ if (*Buffer == '-' || *Buffer == '+') {
+
+ sign = (*Buffer == '-') ? -1 : 1;
+ Buffer++;
+ }
+
+
+ while (*Buffer && isdigit((int) *Buffer)) {
+
+ dnum = dnum * 10.0 + (*Buffer - '0');
+ if (*Buffer) Buffer++;
+ }
+
+ if (*Buffer == '.') {
+
+ cmsFloat64Number frac = 0.0; // fraction
+ int prec = 0; // precission
+
+ if (*Buffer) Buffer++;
+
+ while (*Buffer && isdigit((int) *Buffer)) {
+
+ frac = frac * 10.0 + (*Buffer - '0');
+ prec++;
+ if (*Buffer) Buffer++;
+ }
+
+ dnum = dnum + (frac / xpow10(prec));
+ }
+
+ // Exponent, example 34.00E+20
+ if (*Buffer && toupper(*Buffer) == 'E') {
+
+ int e;
+ int sgn;
+
+ if (*Buffer) Buffer++;
+ sgn = 1;
+
+ if (*Buffer == '-') {
+
+ sgn = -1;
+ if (*Buffer) Buffer++;
+ }
+ else
+ if (*Buffer == '+') {
+
+ sgn = +1;
+ if (*Buffer) Buffer++;
+ }
+
+ e = 0;
+ while (*Buffer && isdigit((int) *Buffer)) {
+
+ if ((cmsFloat64Number) e * 10L < INT_MAX)
+ e = e * 10 + (*Buffer - '0');
+
+ if (*Buffer) Buffer++;
+ }
+
+ e = sgn*e;
+ dnum = dnum * xpow10(e);
+ }
+
+ return sign * dnum;
+}
+
+
+// Reads next symbol
+static
+void InSymbol(cmsIT8* it8)
+{
+ register char *idptr;
+ register int k;
+ SYMBOL key;
+ int sng;
+
+ do {
+
+ while (isseparator(it8->ch))
+ NextCh(it8);
+
+ if (isfirstidchar(it8->ch)) { // Identifier
+
+ k = 0;
+ idptr = it8->id;
+
+ do {
+
+ if (++k < MAXID) *idptr++ = (char) it8->ch;
+
+ NextCh(it8);
+
+ } while (isidchar(it8->ch));
+
+ *idptr = '\0';
+
+
+ key = BinSrchKey(it8->id);
+ if (key == SNONE) it8->sy = SIDENT;
+ else it8->sy = key;
+
+ }
+ else // Is a number?
+ if (isdigit(it8->ch) || it8->ch == '.' || it8->ch == '-' || it8->ch == '+')
+ {
+ int sign = 1;
+
+ if (it8->ch == '-') {
+ sign = -1;
+ NextCh(it8);
+ }
+
+ it8->inum = 0;
+ it8->sy = SINUM;
+
+ if (it8->ch == '0') { // 0xnnnn (Hexa) or 0bnnnn (Binary)
+
+ NextCh(it8);
+ if (toupper(it8->ch) == 'X') {
+
+ int j;
+
+ NextCh(it8);
+ while (isxdigit(it8->ch))
+ {
+ it8->ch = toupper(it8->ch);
+ if (it8->ch >= 'A' && it8->ch <= 'F') j = it8->ch -'A'+10;
+ else j = it8->ch - '0';
+
+ if ((long) it8->inum * 16L > (long) INT_MAX)
+ {
+ SynError(it8, "Invalid hexadecimal number");
+ return;
+ }
+
+ it8->inum = it8->inum * 16 + j;
+ NextCh(it8);
+ }
+ return;
+ }
+
+ if (toupper(it8->ch) == 'B') { // Binary
+
+ int j;
+
+ NextCh(it8);
+ while (it8->ch == '0' || it8->ch == '1')
+ {
+ j = it8->ch - '0';
+
+ if ((long) it8->inum * 2L > (long) INT_MAX)
+ {
+ SynError(it8, "Invalid binary number");
+ return;
+ }
+
+ it8->inum = it8->inum * 2 + j;
+ NextCh(it8);
+ }
+ return;
+ }
+ }
+
+
+ while (isdigit(it8->ch)) {
+
+ if ((long) it8->inum * 10L > (long) INT_MAX) {
+ ReadReal(it8, it8->inum);
+ it8->sy = SDNUM;
+ it8->dnum *= sign;
+ return;
+ }
+
+ it8->inum = it8->inum * 10 + (it8->ch - '0');
+ NextCh(it8);
+ }
+
+ if (it8->ch == '.') {
+
+ ReadReal(it8, it8->inum);
+ it8->sy = SDNUM;
+ it8->dnum *= sign;
+ return;
+ }
+
+ it8 -> inum *= sign;
+
+ // Special case. Numbers followed by letters are taken as identifiers
+
+ if (isidchar(it8 ->ch)) {
+
+ if (it8 ->sy == SINUM) {
+
+ sprintf(it8->id, "%d", it8->inum);
+ }
+ else {
+
+ sprintf(it8->id, it8 ->DoubleFormatter, it8->dnum);
+ }
+
+ k = (int) strlen(it8 ->id);
+ idptr = it8 ->id + k;
+ do {
+
+ if (++k < MAXID) *idptr++ = (char) it8->ch;
+
+ NextCh(it8);
+
+ } while (isidchar(it8->ch));
+
+ *idptr = '\0';
+ it8->sy = SIDENT;
+ }
+ return;
+
+ }
+ else
+ switch ((int) it8->ch) {
+
+ // EOF marker -- ignore it
+ case '\x1a':
+ NextCh(it8);
+ break;
+
+ // Eof stream markers
+ case 0:
+ case -1:
+ it8->sy = SEOF;
+ break;
+
+
+ // Next line
+ case '\r':
+ NextCh(it8);
+ if (it8 ->ch == '\n')
+ NextCh(it8);
+ it8->sy = SEOLN;
+ it8->lineno++;
+ break;
+
+ case '\n':
+ NextCh(it8);
+ it8->sy = SEOLN;
+ it8->lineno++;
+ break;
+
+ // Comment
+ case '#':
+ NextCh(it8);
+ while (it8->ch && it8->ch != '\n' && it8->ch != '\r')
+ NextCh(it8);
+
+ it8->sy = SCOMMENT;
+ break;
+
+ // String.
+ case '\'':
+ case '\"':
+ idptr = it8->str;
+ sng = it8->ch;
+ k = 0;
+ NextCh(it8);
+
+ while (k < MAXSTR && it8->ch != sng) {
+
+ if (it8->ch == '\n'|| it8->ch == '\r') k = MAXSTR+1;
+ else {
+ *idptr++ = (char) it8->ch;
+ NextCh(it8);
+ k++;
+ }
+ }
+
+ it8->sy = SSTRING;
+ *idptr = '\0';
+ NextCh(it8);
+ break;
+
+
+ default:
+ SynError(it8, "Unrecognized character: 0x%x", it8 ->ch);
+ return;
+ }
+
+ } while (it8->sy == SCOMMENT);
+
+ // Handle the include special token
+
+ if (it8 -> sy == SINCLUDE) {
+
+ FILECTX* FileNest;
+
+ if(it8 -> IncludeSP >= (MAXINCLUDE-1)) {
+
+ SynError(it8, "Too many recursion levels");
+ return;
+ }
+
+ InSymbol(it8);
+ if (!Check(it8, SSTRING, "Filename expected")) return;
+
+ FileNest = it8 -> FileStack[it8 -> IncludeSP + 1];
+ if(FileNest == NULL) {
+
+ FileNest = it8 ->FileStack[it8 -> IncludeSP + 1] = (FILECTX*)AllocChunk(it8, sizeof(FILECTX));
+ //if(FileNest == NULL)
+ // TODO: how to manage out-of-memory conditions?
+ }
+
+ if (BuildAbsolutePath(it8->str,
+ it8->FileStack[it8->IncludeSP]->FileName,
+ FileNest->FileName, cmsMAX_PATH-1) == FALSE) {
+ SynError(it8, "File path too long");
+ return;
+ }
+
+ FileNest->Stream = fopen(FileNest->FileName, "rt");
+ if (FileNest->Stream == NULL) {
+
+ SynError(it8, "File %s not found", FileNest->FileName);
+ return;
+ }
+ it8->IncludeSP++;
+
+ it8 ->ch = ' ';
+ InSymbol(it8);
+ }
+
+}
+
+// Checks end of line separator
+static
+cmsBool CheckEOLN(cmsIT8* it8)
+{
+ if (!Check(it8, SEOLN, "Expected separator")) return FALSE;
+ while (it8 -> sy == SEOLN)
+ InSymbol(it8);
+ return TRUE;
+
+}
+
+// Skip a symbol
+
+static
+void Skip(cmsIT8* it8, SYMBOL sy)
+{
+ if (it8->sy == sy && it8->sy != SEOF)
+ InSymbol(it8);
+}
+
+
+// Skip multiple EOLN
+static
+void SkipEOLN(cmsIT8* it8)
+{
+ while (it8->sy == SEOLN) {
+ InSymbol(it8);
+ }
+}
+
+
+// Returns a string holding current value
+static
+cmsBool GetVal(cmsIT8* it8, char* Buffer, cmsUInt32Number max, const char* ErrorTitle)
+{
+ switch (it8->sy) {
+
+ case SEOLN: // Empty value
+ Buffer[0]=0;
+ break;
+ case SIDENT: strncpy(Buffer, it8->id, max);
+ Buffer[max-1]=0;
+ break;
+ case SINUM: snprintf(Buffer, max, "%d", it8 -> inum); break;
+ case SDNUM: snprintf(Buffer, max, it8->DoubleFormatter, it8 -> dnum); break;
+ case SSTRING: strncpy(Buffer, it8->str, max);
+ Buffer[max-1] = 0;
+ break;
+
+
+ default:
+ return SynError(it8, "%s", ErrorTitle);
+ }
+
+ Buffer[max] = 0;
+ return TRUE;
+}
+
+// ---------------------------------------------------------- Table
+
+static
+TABLE* GetTable(cmsIT8* it8)
+{
+ if ((it8 -> nTable >= it8 ->TablesCount)) {
+
+ SynError(it8, "Table %d out of sequence", it8 -> nTable);
+ return it8 -> Tab;
+ }
+
+ return it8 ->Tab + it8 ->nTable;
+}
+
+// ---------------------------------------------------------- Memory management
+
+
+// Frees an allocator and owned memory
+void CMSEXPORT cmsIT8Free(cmsHANDLE hIT8)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ if (it8 == NULL)
+ return;
+
+ if (it8->MemorySink) {
+
+ OWNEDMEM* p;
+ OWNEDMEM* n;
+
+ for (p = it8->MemorySink; p != NULL; p = n) {
+
+ n = p->Next;
+ if (p->Ptr) _cmsFree(it8 ->ContextID, p->Ptr);
+ _cmsFree(it8 ->ContextID, p);
+ }
+ }
+
+ if (it8->MemoryBlock)
+ _cmsFree(it8 ->ContextID, it8->MemoryBlock);
+
+ _cmsFree(it8 ->ContextID, it8);
+}
+
+
+// Allocates a chunk of data, keep linked list
+static
+void* AllocBigBlock(cmsIT8* it8, cmsUInt32Number size)
+{
+ OWNEDMEM* ptr1;
+ void* ptr = _cmsMallocZero(it8->ContextID, size);
+
+ if (ptr != NULL) {
+
+ ptr1 = (OWNEDMEM*) _cmsMallocZero(it8 ->ContextID, sizeof(OWNEDMEM));
+
+ if (ptr1 == NULL) {
+
+ _cmsFree(it8 ->ContextID, ptr);
+ return NULL;
+ }
+
+ ptr1-> Ptr = ptr;
+ ptr1-> Next = it8 -> MemorySink;
+ it8 -> MemorySink = ptr1;
+ }
+
+ return ptr;
+}
+
+
+// Suballocator.
+static
+void* AllocChunk(cmsIT8* it8, cmsUInt32Number size)
+{
+ cmsUInt32Number Free = it8 ->Allocator.BlockSize - it8 ->Allocator.Used;
+ cmsUInt8Number* ptr;
+
+ size = _cmsALIGNMEM(size);
+
+ if (size > Free) {
+
+ if (it8 -> Allocator.BlockSize == 0)
+
+ it8 -> Allocator.BlockSize = 20*1024;
+ else
+ it8 ->Allocator.BlockSize *= 2;
+
+ if (it8 ->Allocator.BlockSize < size)
+ it8 ->Allocator.BlockSize = size;
+
+ it8 ->Allocator.Used = 0;
+ it8 ->Allocator.Block = (cmsUInt8Number*) AllocBigBlock(it8, it8 ->Allocator.BlockSize);
+ }
+
+ ptr = it8 ->Allocator.Block + it8 ->Allocator.Used;
+ it8 ->Allocator.Used += size;
+
+ return (void*) ptr;
+
+}
+
+
+// Allocates a string
+static
+char *AllocString(cmsIT8* it8, const char* str)
+{
+ cmsUInt32Number Size = (cmsUInt32Number) strlen(str)+1;
+ char *ptr;
+
+
+ ptr = (char *) AllocChunk(it8, Size);
+ if (ptr) strncpy (ptr, str, Size-1);
+
+ return ptr;
+}
+
+// Searches through linked list
+
+static
+cmsBool IsAvailableOnList(KEYVALUE* p, const char* Key, const char* Subkey, KEYVALUE** LastPtr)
+{
+ if (LastPtr) *LastPtr = p;
+
+ for (; p != NULL; p = p->Next) {
+
+ if (LastPtr) *LastPtr = p;
+
+ if (*Key != '#') { // Comments are ignored
+
+ if (cmsstrcasecmp(Key, p->Keyword) == 0)
+ break;
+ }
+ }
+
+ if (p == NULL)
+ return FALSE;
+
+ if (Subkey == 0)
+ return TRUE;
+
+ for (; p != NULL; p = p->NextSubkey) {
+
+ if (p ->Subkey == NULL) continue;
+
+ if (LastPtr) *LastPtr = p;
+
+ if (cmsstrcasecmp(Subkey, p->Subkey) == 0)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+
+// Add a property into a linked list
+static
+KEYVALUE* AddToList(cmsIT8* it8, KEYVALUE** Head, const char *Key, const char *Subkey, const char* xValue, WRITEMODE WriteAs)
+{
+ KEYVALUE* p;
+ KEYVALUE* last;
+
+
+ // Check if property is already in list
+
+ if (IsAvailableOnList(*Head, Key, Subkey, &p)) {
+
+ // This may work for editing properties
+
+ // return SynError(it8, "duplicate key <%s>", Key);
+ }
+ else {
+
+ last = p;
+
+ // Allocate the container
+ p = (KEYVALUE*) AllocChunk(it8, sizeof(KEYVALUE));
+ if (p == NULL)
+ {
+ SynError(it8, "AddToList: out of memory");
+ return NULL;
+ }
+
+ // Store name and value
+ p->Keyword = AllocString(it8, Key);
+ p->Subkey = (Subkey == NULL) ? NULL : AllocString(it8, Subkey);
+
+ // Keep the container in our list
+ if (*Head == NULL) {
+ *Head = p;
+ }
+ else
+ {
+ if (Subkey != NULL && last != NULL) {
+
+ last->NextSubkey = p;
+
+ // If Subkey is not null, then last is the last property with the same key,
+ // but not necessarily is the last property in the list, so we need to move
+ // to the actual list end
+ while (last->Next != NULL)
+ last = last->Next;
+ }
+
+ if (last != NULL) last->Next = p;
+ }
+
+ p->Next = NULL;
+ p->NextSubkey = NULL;
+ }
+
+ p->WriteAs = WriteAs;
+
+ if (xValue != NULL) {
+
+ p->Value = AllocString(it8, xValue);
+ }
+ else {
+ p->Value = NULL;
+ }
+
+ return p;
+}
+
+static
+KEYVALUE* AddAvailableProperty(cmsIT8* it8, const char* Key, WRITEMODE as)
+{
+ return AddToList(it8, &it8->ValidKeywords, Key, NULL, NULL, as);
+}
+
+
+static
+KEYVALUE* AddAvailableSampleID(cmsIT8* it8, const char* Key)
+{
+ return AddToList(it8, &it8->ValidSampleID, Key, NULL, NULL, WRITE_UNCOOKED);
+}
+
+
+static
+void AllocTable(cmsIT8* it8)
+{
+ TABLE* t;
+
+ t = it8 ->Tab + it8 ->TablesCount;
+
+ t->HeaderList = NULL;
+ t->DataFormat = NULL;
+ t->Data = NULL;
+
+ it8 ->TablesCount++;
+}
+
+
+cmsInt32Number CMSEXPORT cmsIT8SetTable(cmsHANDLE IT8, cmsUInt32Number nTable)
+{
+ cmsIT8* it8 = (cmsIT8*) IT8;
+
+ if (nTable >= it8 ->TablesCount) {
+
+ if (nTable == it8 ->TablesCount) {
+
+ AllocTable(it8);
+ }
+ else {
+ SynError(it8, "Table %d is out of sequence", nTable);
+ return -1;
+ }
+ }
+
+ it8 ->nTable = nTable;
+
+ return (cmsInt32Number) nTable;
+}
+
+
+
+// Init an empty container
+cmsHANDLE CMSEXPORT cmsIT8Alloc(cmsContext ContextID)
+{
+ cmsIT8* it8;
+ cmsUInt32Number i;
+
+ it8 = (cmsIT8*) _cmsMallocZero(ContextID, sizeof(cmsIT8));
+ if (it8 == NULL) return NULL;
+
+ AllocTable(it8);
+
+ it8->MemoryBlock = NULL;
+ it8->MemorySink = NULL;
+
+ it8 ->nTable = 0;
+
+ it8->ContextID = ContextID;
+ it8->Allocator.Used = 0;
+ it8->Allocator.Block = NULL;
+ it8->Allocator.BlockSize = 0;
+
+ it8->ValidKeywords = NULL;
+ it8->ValidSampleID = NULL;
+
+ it8 -> sy = SNONE;
+ it8 -> ch = ' ';
+ it8 -> Source = NULL;
+ it8 -> inum = 0;
+ it8 -> dnum = 0.0;
+
+ it8->FileStack[0] = (FILECTX*)AllocChunk(it8, sizeof(FILECTX));
+ it8->IncludeSP = 0;
+ it8 -> lineno = 1;
+
+ strcpy(it8->DoubleFormatter, DEFAULT_DBL_FORMAT);
+ cmsIT8SetSheetType((cmsHANDLE) it8, "CGATS.17");
+
+ // Initialize predefined properties & data
+
+ for (i=0; i < NUMPREDEFINEDPROPS; i++)
+ AddAvailableProperty(it8, PredefinedProperties[i].id, PredefinedProperties[i].as);
+
+ for (i=0; i < NUMPREDEFINEDSAMPLEID; i++)
+ AddAvailableSampleID(it8, PredefinedSampleID[i]);
+
+
+ return (cmsHANDLE) it8;
+}
+
+
+const char* CMSEXPORT cmsIT8GetSheetType(cmsHANDLE hIT8)
+{
+ return GetTable((cmsIT8*) hIT8)->SheetType;
+}
+
+cmsBool CMSEXPORT cmsIT8SetSheetType(cmsHANDLE hIT8, const char* Type)
+{
+ TABLE* t = GetTable((cmsIT8*) hIT8);
+
+ strncpy(t ->SheetType, Type, MAXSTR-1);
+ t ->SheetType[MAXSTR-1] = 0;
+ return TRUE;
+}
+
+cmsBool CMSEXPORT cmsIT8SetComment(cmsHANDLE hIT8, const char* Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ if (!Val) return FALSE;
+ if (!*Val) return FALSE;
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, "# ", NULL, Val, WRITE_UNCOOKED) != NULL;
+}
+
+// Sets a property
+cmsBool CMSEXPORT cmsIT8SetPropertyStr(cmsHANDLE hIT8, const char* Key, const char *Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ if (!Val) return FALSE;
+ if (!*Val) return FALSE;
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, Key, NULL, Val, WRITE_STRINGIFY) != NULL;
+}
+
+cmsBool CMSEXPORT cmsIT8SetPropertyDbl(cmsHANDLE hIT8, const char* cProp, cmsFloat64Number Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ char Buffer[1024];
+
+ sprintf(Buffer, it8->DoubleFormatter, Val);
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, cProp, NULL, Buffer, WRITE_UNCOOKED) != NULL;
+}
+
+cmsBool CMSEXPORT cmsIT8SetPropertyHex(cmsHANDLE hIT8, const char* cProp, cmsUInt32Number Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ char Buffer[1024];
+
+ sprintf(Buffer, "%u", Val);
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, cProp, NULL, Buffer, WRITE_HEXADECIMAL) != NULL;
+}
+
+cmsBool CMSEXPORT cmsIT8SetPropertyUncooked(cmsHANDLE hIT8, const char* Key, const char* Buffer)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, Key, NULL, Buffer, WRITE_UNCOOKED) != NULL;
+}
+
+cmsBool CMSEXPORT cmsIT8SetPropertyMulti(cmsHANDLE hIT8, const char* Key, const char* SubKey, const char *Buffer)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ return AddToList(it8, &GetTable(it8)->HeaderList, Key, SubKey, Buffer, WRITE_PAIR) != NULL;
+}
+
+// Gets a property
+const char* CMSEXPORT cmsIT8GetProperty(cmsHANDLE hIT8, const char* Key)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ KEYVALUE* p;
+
+ if (IsAvailableOnList(GetTable(it8) -> HeaderList, Key, NULL, &p))
+ {
+ return p -> Value;
+ }
+ return NULL;
+}
+
+
+cmsFloat64Number CMSEXPORT cmsIT8GetPropertyDbl(cmsHANDLE hIT8, const char* cProp)
+{
+ const char *v = cmsIT8GetProperty(hIT8, cProp);
+
+ if (v == NULL) return 0.0;
+
+ return ParseFloatNumber(v);
+}
+
+const char* CMSEXPORT cmsIT8GetPropertyMulti(cmsHANDLE hIT8, const char* Key, const char *SubKey)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ KEYVALUE* p;
+
+ if (IsAvailableOnList(GetTable(it8) -> HeaderList, Key, SubKey, &p)) {
+ return p -> Value;
+ }
+ return NULL;
+}
+
+// ----------------------------------------------------------------- Datasets
+
+
+static
+void AllocateDataFormat(cmsIT8* it8)
+{
+ TABLE* t = GetTable(it8);
+
+ if (t -> DataFormat) return; // Already allocated
+
+ t -> nSamples = (int) cmsIT8GetPropertyDbl(it8, "NUMBER_OF_FIELDS");
+
+ if (t -> nSamples <= 0) {
+
+ SynError(it8, "AllocateDataFormat: Unknown NUMBER_OF_FIELDS");
+ t -> nSamples = 10;
+ }
+
+ t -> DataFormat = (char**) AllocChunk (it8, ((cmsUInt32Number) t->nSamples + 1) * sizeof(char *));
+ if (t->DataFormat == NULL) {
+
+ SynError(it8, "AllocateDataFormat: Unable to allocate dataFormat array");
+ }
+
+}
+
+static
+const char *GetDataFormat(cmsIT8* it8, int n)
+{
+ TABLE* t = GetTable(it8);
+
+ if (t->DataFormat)
+ return t->DataFormat[n];
+
+ return NULL;
+}
+
+static
+cmsBool SetDataFormat(cmsIT8* it8, int n, const char *label)
+{
+ TABLE* t = GetTable(it8);
+
+ if (!t->DataFormat)
+ AllocateDataFormat(it8);
+
+ if (n > t -> nSamples) {
+ SynError(it8, "More than NUMBER_OF_FIELDS fields.");
+ return FALSE;
+ }
+
+ if (t->DataFormat) {
+ t->DataFormat[n] = AllocString(it8, label);
+ }
+
+ return TRUE;
+}
+
+
+cmsBool CMSEXPORT cmsIT8SetDataFormat(cmsHANDLE h, int n, const char *Sample)
+{
+ cmsIT8* it8 = (cmsIT8*) h;
+ return SetDataFormat(it8, n, Sample);
+}
+
+static
+void AllocateDataSet(cmsIT8* it8)
+{
+ TABLE* t = GetTable(it8);
+
+ if (t -> Data) return; // Already allocated
+
+ t-> nSamples = atoi(cmsIT8GetProperty(it8, "NUMBER_OF_FIELDS"));
+ t-> nPatches = atoi(cmsIT8GetProperty(it8, "NUMBER_OF_SETS"));
+
+ t-> Data = (char**)AllocChunk (it8, ((cmsUInt32Number) t->nSamples + 1) * ((cmsUInt32Number) t->nPatches + 1) *sizeof (char*));
+ if (t->Data == NULL) {
+
+ SynError(it8, "AllocateDataSet: Unable to allocate data array");
+ }
+
+}
+
+static
+char* GetData(cmsIT8* it8, int nSet, int nField)
+{
+ TABLE* t = GetTable(it8);
+ int nSamples = t -> nSamples;
+ int nPatches = t -> nPatches;
+
+ if (nSet >= nPatches || nField >= nSamples)
+ return NULL;
+
+ if (!t->Data) return NULL;
+ return t->Data [nSet * nSamples + nField];
+}
+
+static
+cmsBool SetData(cmsIT8* it8, int nSet, int nField, const char *Val)
+{
+ TABLE* t = GetTable(it8);
+
+ if (!t->Data)
+ AllocateDataSet(it8);
+
+ if (!t->Data) return FALSE;
+
+ if (nSet > t -> nPatches || nSet < 0) {
+
+ return SynError(it8, "Patch %d out of range, there are %d patches", nSet, t -> nPatches);
+ }
+
+ if (nField > t ->nSamples || nField < 0) {
+ return SynError(it8, "Sample %d out of range, there are %d samples", nField, t ->nSamples);
+
+ }
+
+ t->Data [nSet * t -> nSamples + nField] = AllocString(it8, Val);
+ return TRUE;
+}
+
+
+// --------------------------------------------------------------- File I/O
+
+
+// Writes a string to file
+static
+void WriteStr(SAVESTREAM* f, const char *str)
+{
+ cmsUInt32Number len;
+
+ if (str == NULL)
+ str = " ";
+
+ // Length to write
+ len = (cmsUInt32Number) strlen(str);
+ f ->Used += len;
+
+
+ if (f ->stream) { // Should I write it to a file?
+
+ if (fwrite(str, 1, len, f->stream) != len) {
+ cmsSignalError(0, cmsERROR_WRITE, "Write to file error in CGATS parser");
+ return;
+ }
+
+ }
+ else { // Or to a memory block?
+
+ if (f ->Base) { // Am I just counting the bytes?
+
+ if (f ->Used > f ->Max) {
+
+ cmsSignalError(0, cmsERROR_WRITE, "Write to memory overflows in CGATS parser");
+ return;
+ }
+
+ memmove(f ->Ptr, str, len);
+ f->Ptr += len;
+ }
+
+ }
+}
+
+
+// Write formatted
+
+static
+void Writef(SAVESTREAM* f, const char* frm, ...)
+{
+ char Buffer[4096];
+ va_list args;
+
+ va_start(args, frm);
+ vsnprintf(Buffer, 4095, frm, args);
+ Buffer[4095] = 0;
+ WriteStr(f, Buffer);
+ va_end(args);
+
+}
+
+// Writes full header
+static
+void WriteHeader(cmsIT8* it8, SAVESTREAM* fp)
+{
+ KEYVALUE* p;
+ TABLE* t = GetTable(it8);
+
+ // Writes the type
+ WriteStr(fp, t->SheetType);
+ WriteStr(fp, "\n");
+
+ for (p = t->HeaderList; (p != NULL); p = p->Next)
+ {
+ if (*p ->Keyword == '#') {
+
+ char* Pt;
+
+ WriteStr(fp, "#\n# ");
+ for (Pt = p ->Value; *Pt; Pt++) {
+
+
+ Writef(fp, "%c", *Pt);
+
+ if (*Pt == '\n') {
+ WriteStr(fp, "# ");
+ }
+ }
+
+ WriteStr(fp, "\n#\n");
+ continue;
+ }
+
+
+ if (!IsAvailableOnList(it8-> ValidKeywords, p->Keyword, NULL, NULL)) {
+
+#ifdef CMS_STRICT_CGATS
+ WriteStr(fp, "KEYWORD\t\"");
+ WriteStr(fp, p->Keyword);
+ WriteStr(fp, "\"\n");
+#endif
+
+ AddAvailableProperty(it8, p->Keyword, WRITE_UNCOOKED);
+ }
+
+ WriteStr(fp, p->Keyword);
+ if (p->Value) {
+
+ switch (p ->WriteAs) {
+
+ case WRITE_UNCOOKED:
+ Writef(fp, "\t%s", p ->Value);
+ break;
+
+ case WRITE_STRINGIFY:
+ Writef(fp, "\t\"%s\"", p->Value );
+ break;
+
+ case WRITE_HEXADECIMAL:
+ Writef(fp, "\t0x%X", atoi(p ->Value));
+ break;
+
+ case WRITE_BINARY:
+ Writef(fp, "\t0x%B", atoi(p ->Value));
+ break;
+
+ case WRITE_PAIR:
+ Writef(fp, "\t\"%s,%s\"", p->Subkey, p->Value);
+ break;
+
+ default: SynError(it8, "Unknown write mode %d", p ->WriteAs);
+ return;
+ }
+ }
+
+ WriteStr (fp, "\n");
+ }
+
+}
+
+
+// Writes the data format
+static
+void WriteDataFormat(SAVESTREAM* fp, cmsIT8* it8)
+{
+ int i, nSamples;
+ TABLE* t = GetTable(it8);
+
+ if (!t -> DataFormat) return;
+
+ WriteStr(fp, "BEGIN_DATA_FORMAT\n");
+ WriteStr(fp, " ");
+ nSamples = atoi(cmsIT8GetProperty(it8, "NUMBER_OF_FIELDS"));
+
+ for (i = 0; i < nSamples; i++) {
+
+ WriteStr(fp, t->DataFormat[i]);
+ WriteStr(fp, ((i == (nSamples-1)) ? "\n" : "\t"));
+ }
+
+ WriteStr (fp, "END_DATA_FORMAT\n");
+}
+
+
+// Writes data array
+static
+void WriteData(SAVESTREAM* fp, cmsIT8* it8)
+{
+ int i, j;
+ TABLE* t = GetTable(it8);
+
+ if (!t->Data) return;
+
+ WriteStr (fp, "BEGIN_DATA\n");
+
+ t->nPatches = atoi(cmsIT8GetProperty(it8, "NUMBER_OF_SETS"));
+
+ for (i = 0; i < t-> nPatches; i++) {
+
+ WriteStr(fp, " ");
+
+ for (j = 0; j < t->nSamples; j++) {
+
+ char *ptr = t->Data[i*t->nSamples+j];
+
+ if (ptr == NULL) WriteStr(fp, "\"\"");
+ else {
+ // If value contains whitespace, enclose within quote
+
+ if (strchr(ptr, ' ') != NULL) {
+
+ WriteStr(fp, "\"");
+ WriteStr(fp, ptr);
+ WriteStr(fp, "\"");
+ }
+ else
+ WriteStr(fp, ptr);
+ }
+
+ WriteStr(fp, ((j == (t->nSamples-1)) ? "\n" : "\t"));
+ }
+ }
+ WriteStr (fp, "END_DATA\n");
+}
+
+
+
+// Saves whole file
+cmsBool CMSEXPORT cmsIT8SaveToFile(cmsHANDLE hIT8, const char* cFileName)
+{
+ SAVESTREAM sd;
+ cmsUInt32Number i;
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ memset(&sd, 0, sizeof(sd));
+
+ sd.stream = fopen(cFileName, "wt");
+ if (!sd.stream) return FALSE;
+
+ for (i=0; i < it8 ->TablesCount; i++) {
+
+ cmsIT8SetTable(hIT8, i);
+ WriteHeader(it8, &sd);
+ WriteDataFormat(&sd, it8);
+ WriteData(&sd, it8);
+ }
+
+ if (fclose(sd.stream) != 0) return FALSE;
+
+ return TRUE;
+}
+
+
+// Saves to memory
+cmsBool CMSEXPORT cmsIT8SaveToMem(cmsHANDLE hIT8, void *MemPtr, cmsUInt32Number* BytesNeeded)
+{
+ SAVESTREAM sd;
+ cmsUInt32Number i;
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ memset(&sd, 0, sizeof(sd));
+
+ sd.stream = NULL;
+ sd.Base = (cmsUInt8Number*) MemPtr;
+ sd.Ptr = sd.Base;
+
+ sd.Used = 0;
+
+ if (sd.Base)
+ sd.Max = *BytesNeeded; // Write to memory?
+ else
+ sd.Max = 0; // Just counting the needed bytes
+
+ for (i=0; i < it8 ->TablesCount; i++) {
+
+ cmsIT8SetTable(hIT8, i);
+ WriteHeader(it8, &sd);
+ WriteDataFormat(&sd, it8);
+ WriteData(&sd, it8);
+ }
+
+ sd.Used++; // The \0 at the very end
+
+ if (sd.Base)
+ *sd.Ptr = 0;
+
+ *BytesNeeded = sd.Used;
+
+ return TRUE;
+}
+
+
+// -------------------------------------------------------------- Higer level parsing
+
+static
+cmsBool DataFormatSection(cmsIT8* it8)
+{
+ int iField = 0;
+ TABLE* t = GetTable(it8);
+
+ InSymbol(it8); // Eats "BEGIN_DATA_FORMAT"
+ CheckEOLN(it8);
+
+ while (it8->sy != SEND_DATA_FORMAT &&
+ it8->sy != SEOLN &&
+ it8->sy != SEOF &&
+ it8->sy != SSYNERROR) {
+
+ if (it8->sy != SIDENT) {
+
+ return SynError(it8, "Sample type expected");
+ }
+
+ if (!SetDataFormat(it8, iField, it8->id)) return FALSE;
+ iField++;
+
+ InSymbol(it8);
+ SkipEOLN(it8);
+ }
+
+ SkipEOLN(it8);
+ Skip(it8, SEND_DATA_FORMAT);
+ SkipEOLN(it8);
+
+ if (iField != t ->nSamples) {
+ SynError(it8, "Count mismatch. NUMBER_OF_FIELDS was %d, found %d\n", t ->nSamples, iField);
+
+
+ }
+
+ return TRUE;
+}
+
+
+
+static
+cmsBool DataSection (cmsIT8* it8)
+{
+ int iField = 0;
+ int iSet = 0;
+ char Buffer[256];
+ TABLE* t = GetTable(it8);
+
+ InSymbol(it8); // Eats "BEGIN_DATA"
+ CheckEOLN(it8);
+
+ if (!t->Data)
+ AllocateDataSet(it8);
+
+ while (it8->sy != SEND_DATA && it8->sy != SEOF)
+ {
+ if (iField >= t -> nSamples) {
+ iField = 0;
+ iSet++;
+
+ }
+
+ if (it8->sy != SEND_DATA && it8->sy != SEOF) {
+
+ if (!GetVal(it8, Buffer, 255, "Sample data expected"))
+ return FALSE;
+
+ if (!SetData(it8, iSet, iField, Buffer))
+ return FALSE;
+
+ iField++;
+
+ InSymbol(it8);
+ SkipEOLN(it8);
+ }
+ }
+
+ SkipEOLN(it8);
+ Skip(it8, SEND_DATA);
+ SkipEOLN(it8);
+
+ // Check for data completion.
+
+ if ((iSet+1) != t -> nPatches)
+ return SynError(it8, "Count mismatch. NUMBER_OF_SETS was %d, found %d\n", t ->nPatches, iSet+1);
+
+ return TRUE;
+}
+
+
+
+
+static
+cmsBool HeaderSection(cmsIT8* it8)
+{
+ char VarName[MAXID];
+ char Buffer[MAXSTR];
+ KEYVALUE* Key;
+
+ while (it8->sy != SEOF &&
+ it8->sy != SSYNERROR &&
+ it8->sy != SBEGIN_DATA_FORMAT &&
+ it8->sy != SBEGIN_DATA) {
+
+
+ switch (it8 -> sy) {
+
+ case SKEYWORD:
+ InSymbol(it8);
+ if (!GetVal(it8, Buffer, MAXSTR-1, "Keyword expected")) return FALSE;
+ if (!AddAvailableProperty(it8, Buffer, WRITE_UNCOOKED)) return FALSE;
+ InSymbol(it8);
+ break;
+
+
+ case SDATA_FORMAT_ID:
+ InSymbol(it8);
+ if (!GetVal(it8, Buffer, MAXSTR-1, "Keyword expected")) return FALSE;
+ if (!AddAvailableSampleID(it8, Buffer)) return FALSE;
+ InSymbol(it8);
+ break;
+
+
+ case SIDENT:
+ strncpy(VarName, it8->id, MAXID-1);
+ VarName[MAXID-1] = 0;
+
+ if (!IsAvailableOnList(it8-> ValidKeywords, VarName, NULL, &Key)) {
+
+#ifdef CMS_STRICT_CGATS
+ return SynError(it8, "Undefined keyword '%s'", VarName);
+#else
+ Key = AddAvailableProperty(it8, VarName, WRITE_UNCOOKED);
+ if (Key == NULL) return FALSE;
+#endif
+ }
+
+ InSymbol(it8);
+ if (!GetVal(it8, Buffer, MAXSTR-1, "Property data expected")) return FALSE;
+
+ if(Key->WriteAs != WRITE_PAIR) {
+ AddToList(it8, &GetTable(it8)->HeaderList, VarName, NULL, Buffer,
+ (it8->sy == SSTRING) ? WRITE_STRINGIFY : WRITE_UNCOOKED);
+ }
+ else {
+ const char *Subkey;
+ char *Nextkey;
+ if (it8->sy != SSTRING)
+ return SynError(it8, "Invalid value '%s' for property '%s'.", Buffer, VarName);
+
+ // chop the string as a list of "subkey, value" pairs, using ';' as a separator
+ for (Subkey = Buffer; Subkey != NULL; Subkey = Nextkey)
+ {
+ char *Value, *temp;
+
+ // identify token pair boundary
+ Nextkey = (char*) strchr(Subkey, ';');
+ if(Nextkey)
+ *Nextkey++ = '\0';
+
+ // for each pair, split the subkey and the value
+ Value = (char*) strrchr(Subkey, ',');
+ if(Value == NULL)
+ return SynError(it8, "Invalid value for property '%s'.", VarName);
+
+ // gobble the spaces before the coma, and the coma itself
+ temp = Value++;
+ do *temp-- = '\0'; while(temp >= Subkey && *temp == ' ');
+
+ // gobble any space at the right
+ temp = Value + strlen(Value) - 1;
+ while(*temp == ' ') *temp-- = '\0';
+
+ // trim the strings from the left
+ Subkey += strspn(Subkey, " ");
+ Value += strspn(Value, " ");
+
+ if(Subkey[0] == 0 || Value[0] == 0)
+ return SynError(it8, "Invalid value for property '%s'.", VarName);
+ AddToList(it8, &GetTable(it8)->HeaderList, VarName, Subkey, Value, WRITE_PAIR);
+ }
+ }
+
+ InSymbol(it8);
+ break;
+
+
+ case SEOLN: break;
+
+ default:
+ return SynError(it8, "expected keyword or identifier");
+ }
+
+ SkipEOLN(it8);
+ }
+
+ return TRUE;
+
+}
+
+
+static
+void ReadType(cmsIT8* it8, char* SheetTypePtr)
+{
+ // First line is a very special case.
+
+ while (isseparator(it8->ch))
+ NextCh(it8);
+
+ while (it8->ch != '\r' && it8 ->ch != '\n' && it8->ch != '\t' && it8 -> ch != -1) {
+
+ *SheetTypePtr++= (char) it8 ->ch;
+ NextCh(it8);
+ }
+
+ *SheetTypePtr = 0;
+}
+
+
+static
+cmsBool ParseIT8(cmsIT8* it8, cmsBool nosheet)
+{
+ char* SheetTypePtr = it8 ->Tab[0].SheetType;
+
+ if (nosheet == 0) {
+ ReadType(it8, SheetTypePtr);
+ }
+
+ InSymbol(it8);
+
+ SkipEOLN(it8);
+
+ while (it8-> sy != SEOF &&
+ it8-> sy != SSYNERROR) {
+
+ switch (it8 -> sy) {
+
+ case SBEGIN_DATA_FORMAT:
+ if (!DataFormatSection(it8)) return FALSE;
+ break;
+
+ case SBEGIN_DATA:
+
+ if (!DataSection(it8)) return FALSE;
+
+ if (it8 -> sy != SEOF) {
+
+ AllocTable(it8);
+ it8 ->nTable = it8 ->TablesCount - 1;
+
+ // Read sheet type if present. We only support identifier and string.
+ // <ident> <eoln> is a type string
+ // anything else, is not a type string
+ if (nosheet == 0) {
+
+ if (it8 ->sy == SIDENT) {
+
+ // May be a type sheet or may be a prop value statement. We cannot use insymbol in
+ // this special case...
+ while (isseparator(it8->ch))
+ NextCh(it8);
+
+ // If a newline is found, then this is a type string
+ if (it8 ->ch == '\n' || it8->ch == '\r') {
+
+ cmsIT8SetSheetType(it8, it8 ->id);
+ InSymbol(it8);
+ }
+ else
+ {
+ // It is not. Just continue
+ cmsIT8SetSheetType(it8, "");
+ }
+ }
+ else
+ // Validate quoted strings
+ if (it8 ->sy == SSTRING) {
+ cmsIT8SetSheetType(it8, it8 ->str);
+ InSymbol(it8);
+ }
+ }
+
+ }
+ break;
+
+ case SEOLN:
+ SkipEOLN(it8);
+ break;
+
+ default:
+ if (!HeaderSection(it8)) return FALSE;
+ }
+
+ }
+
+ return (it8 -> sy != SSYNERROR);
+}
+
+
+
+// Init usefull pointers
+
+static
+void CookPointers(cmsIT8* it8)
+{
+ int idField, i;
+ char* Fld;
+ cmsUInt32Number j;
+ cmsUInt32Number nOldTable = it8 ->nTable;
+
+ for (j=0; j < it8 ->TablesCount; j++) {
+
+ TABLE* t = it8 ->Tab + j;
+
+ t -> SampleID = 0;
+ it8 ->nTable = j;
+
+ for (idField = 0; idField < t -> nSamples; idField++)
+ {
+ if (t ->DataFormat == NULL){
+ SynError(it8, "Undefined DATA_FORMAT");
+ return;
+ }
+
+ Fld = t->DataFormat[idField];
+ if (!Fld) continue;
+
+
+ if (cmsstrcasecmp(Fld, "SAMPLE_ID") == 0) {
+
+ t -> SampleID = idField;
+
+ for (i=0; i < t -> nPatches; i++) {
+
+ char *Data = GetData(it8, i, idField);
+ if (Data) {
+ char Buffer[256];
+
+ strncpy(Buffer, Data, 255);
+ Buffer[255] = 0;
+
+ if (strlen(Buffer) <= strlen(Data))
+ strcpy(Data, Buffer);
+ else
+ SetData(it8, i, idField, Buffer);
+
+ }
+ }
+
+ }
+
+ // "LABEL" is an extension. It keeps references to forward tables
+
+ if ((cmsstrcasecmp(Fld, "LABEL") == 0) || Fld[0] == '$' ) {
+
+ // Search for table references...
+ for (i=0; i < t -> nPatches; i++) {
+
+ char *Label = GetData(it8, i, idField);
+
+ if (Label) {
+
+ cmsUInt32Number k;
+
+ // This is the label, search for a table containing
+ // this property
+
+ for (k=0; k < it8 ->TablesCount; k++) {
+
+ TABLE* Table = it8 ->Tab + k;
+ KEYVALUE* p;
+
+ if (IsAvailableOnList(Table->HeaderList, Label, NULL, &p)) {
+
+ // Available, keep type and table
+ char Buffer[256];
+
+ char *Type = p ->Value;
+ int nTable = (int) k;
+
+ snprintf(Buffer, 255, "%s %d %s", Label, nTable, Type );
+
+ SetData(it8, i, idField, Buffer);
+ }
+ }
+
+
+ }
+
+ }
+
+
+ }
+
+ }
+ }
+
+ it8 ->nTable = nOldTable;
+}
+
+// Try to infere if the file is a CGATS/IT8 file at all. Read first line
+// that should be something like some printable characters plus a \n
+// returns 0 if this is not like a CGATS, or an integer otherwise. This integer is the number of words in first line?
+static
+int IsMyBlock(cmsUInt8Number* Buffer, int n)
+{
+ int words = 1, space = 0, quot = 0;
+ int i;
+
+ if (n < 10) return 0; // Too small
+
+ if (n > 132)
+ n = 132;
+
+ for (i = 1; i < n; i++) {
+
+ switch(Buffer[i])
+ {
+ case '\n':
+ case '\r':
+ return ((quot == 1) || (words > 2)) ? 0 : words;
+ case '\t':
+ case ' ':
+ if(!quot && !space)
+ space = 1;
+ break;
+ case '\"':
+ quot = !quot;
+ break;
+ default:
+ if (Buffer[i] < 32) return 0;
+ if (Buffer[i] > 127) return 0;
+ words += space;
+ space = 0;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+
+static
+cmsBool IsMyFile(const char* FileName)
+{
+ FILE *fp;
+ cmsUInt32Number Size;
+ cmsUInt8Number Ptr[133];
+
+ fp = fopen(FileName, "rt");
+ if (!fp) {
+ cmsSignalError(0, cmsERROR_FILE, "File '%s' not found", FileName);
+ return FALSE;
+ }
+
+ Size = (cmsUInt32Number) fread(Ptr, 1, 132, fp);
+
+ if (fclose(fp) != 0)
+ return FALSE;
+
+ Ptr[Size] = '\0';
+
+ return IsMyBlock(Ptr, Size);
+}
+
+// ---------------------------------------------------------- Exported routines
+
+
+cmsHANDLE CMSEXPORT cmsIT8LoadFromMem(cmsContext ContextID, void *Ptr, cmsUInt32Number len)
+{
+ cmsHANDLE hIT8;
+ cmsIT8* it8;
+ int type;
+
+ _cmsAssert(Ptr != NULL);
+ _cmsAssert(len != 0);
+
+ type = IsMyBlock((cmsUInt8Number*)Ptr, len);
+ if (type == 0) return NULL;
+
+ hIT8 = cmsIT8Alloc(ContextID);
+ if (!hIT8) return NULL;
+
+ it8 = (cmsIT8*) hIT8;
+ it8 ->MemoryBlock = (char*) _cmsMalloc(ContextID, len + 1);
+
+ strncpy(it8 ->MemoryBlock, (const char*) Ptr, len);
+ it8 ->MemoryBlock[len] = 0;
+
+ strncpy(it8->FileStack[0]->FileName, "", cmsMAX_PATH-1);
+ it8-> Source = it8 -> MemoryBlock;
+
+ if (!ParseIT8(it8, type-1)) {
+
+ cmsIT8Free(hIT8);
+ return FALSE;
+ }
+
+ CookPointers(it8);
+ it8 ->nTable = 0;
+
+ _cmsFree(ContextID, it8->MemoryBlock);
+ it8 -> MemoryBlock = NULL;
+
+ return hIT8;
+
+
+}
+
+
+cmsHANDLE CMSEXPORT cmsIT8LoadFromFile(cmsContext ContextID, const char* cFileName)
+{
+
+ cmsHANDLE hIT8;
+ cmsIT8* it8;
+ int type;
+
+ _cmsAssert(cFileName != NULL);
+
+ type = IsMyFile(cFileName);
+ if (type == 0) return NULL;
+
+ hIT8 = cmsIT8Alloc(ContextID);
+ it8 = (cmsIT8*) hIT8;
+ if (!hIT8) return NULL;
+
+
+ it8 ->FileStack[0]->Stream = fopen(cFileName, "rt");
+
+ if (!it8 ->FileStack[0]->Stream) {
+ cmsIT8Free(hIT8);
+ return NULL;
+ }
+
+
+ strncpy(it8->FileStack[0]->FileName, cFileName, cmsMAX_PATH-1);
+ it8->FileStack[0]->FileName[cmsMAX_PATH-1] = 0;
+
+ if (!ParseIT8(it8, type-1)) {
+
+ fclose(it8 ->FileStack[0]->Stream);
+ cmsIT8Free(hIT8);
+ return NULL;
+ }
+
+ CookPointers(it8);
+ it8 ->nTable = 0;
+
+ if (fclose(it8 ->FileStack[0]->Stream)!= 0) {
+ cmsIT8Free(hIT8);
+ return NULL;
+ }
+
+ return hIT8;
+
+}
+
+int CMSEXPORT cmsIT8EnumDataFormat(cmsHANDLE hIT8, char ***SampleNames)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ TABLE* t;
+
+ _cmsAssert(hIT8 != NULL);
+
+ t = GetTable(it8);
+
+ if (SampleNames)
+ *SampleNames = t -> DataFormat;
+ return t -> nSamples;
+}
+
+
+cmsUInt32Number CMSEXPORT cmsIT8EnumProperties(cmsHANDLE hIT8, char ***PropertyNames)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ KEYVALUE* p;
+ cmsUInt32Number n;
+ char **Props;
+ TABLE* t;
+
+ _cmsAssert(hIT8 != NULL);
+
+ t = GetTable(it8);
+
+ // Pass#1 - count properties
+
+ n = 0;
+ for (p = t -> HeaderList; p != NULL; p = p->Next) {
+ n++;
+ }
+
+
+ Props = (char **) AllocChunk(it8, sizeof(char *) * n);
+
+ // Pass#2 - Fill pointers
+ n = 0;
+ for (p = t -> HeaderList; p != NULL; p = p->Next) {
+ Props[n++] = p -> Keyword;
+ }
+
+ *PropertyNames = Props;
+ return n;
+}
+
+cmsUInt32Number CMSEXPORT cmsIT8EnumPropertyMulti(cmsHANDLE hIT8, const char* cProp, const char ***SubpropertyNames)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ KEYVALUE *p, *tmp;
+ cmsUInt32Number n;
+ const char **Props;
+ TABLE* t;
+
+ _cmsAssert(hIT8 != NULL);
+
+
+ t = GetTable(it8);
+
+ if(!IsAvailableOnList(t->HeaderList, cProp, NULL, &p)) {
+ *SubpropertyNames = 0;
+ return 0;
+ }
+
+ // Pass#1 - count properties
+
+ n = 0;
+ for (tmp = p; tmp != NULL; tmp = tmp->NextSubkey) {
+ if(tmp->Subkey != NULL)
+ n++;
+ }
+
+
+ Props = (const char **) AllocChunk(it8, sizeof(char *) * n);
+
+ // Pass#2 - Fill pointers
+ n = 0;
+ for (tmp = p; tmp != NULL; tmp = tmp->NextSubkey) {
+ if(tmp->Subkey != NULL)
+ Props[n++] = p ->Subkey;
+ }
+
+ *SubpropertyNames = Props;
+ return n;
+}
+
+static
+int LocatePatch(cmsIT8* it8, const char* cPatch)
+{
+ int i;
+ const char *data;
+ TABLE* t = GetTable(it8);
+
+ for (i=0; i < t-> nPatches; i++) {
+
+ data = GetData(it8, i, t->SampleID);
+
+ if (data != NULL) {
+
+ if (cmsstrcasecmp(data, cPatch) == 0)
+ return i;
+ }
+ }
+
+ // SynError(it8, "Couldn't find patch '%s'\n", cPatch);
+ return -1;
+}
+
+
+static
+int LocateEmptyPatch(cmsIT8* it8)
+{
+ int i;
+ const char *data;
+ TABLE* t = GetTable(it8);
+
+ for (i=0; i < t-> nPatches; i++) {
+
+ data = GetData(it8, i, t->SampleID);
+
+ if (data == NULL)
+ return i;
+
+ }
+
+ return -1;
+}
+
+static
+int LocateSample(cmsIT8* it8, const char* cSample)
+{
+ int i;
+ const char *fld;
+ TABLE* t = GetTable(it8);
+
+ for (i=0; i < t->nSamples; i++) {
+
+ fld = GetDataFormat(it8, i);
+ if (cmsstrcasecmp(fld, cSample) == 0)
+ return i;
+ }
+
+ return -1;
+
+}
+
+
+int CMSEXPORT cmsIT8FindDataFormat(cmsHANDLE hIT8, const char* cSample)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ _cmsAssert(hIT8 != NULL);
+
+ return LocateSample(it8, cSample);
+}
+
+
+
+const char* CMSEXPORT cmsIT8GetDataRowCol(cmsHANDLE hIT8, int row, int col)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ _cmsAssert(hIT8 != NULL);
+
+ return GetData(it8, row, col);
+}
+
+
+cmsFloat64Number CMSEXPORT cmsIT8GetDataRowColDbl(cmsHANDLE hIT8, int row, int col)
+{
+ const char* Buffer;
+
+ Buffer = cmsIT8GetDataRowCol(hIT8, row, col);
+
+ if (Buffer == NULL) return 0.0;
+
+ return ParseFloatNumber(Buffer);
+}
+
+
+cmsBool CMSEXPORT cmsIT8SetDataRowCol(cmsHANDLE hIT8, int row, int col, const char* Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ _cmsAssert(hIT8 != NULL);
+
+ return SetData(it8, row, col, Val);
+}
+
+
+cmsBool CMSEXPORT cmsIT8SetDataRowColDbl(cmsHANDLE hIT8, int row, int col, cmsFloat64Number Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ char Buff[256];
+
+ _cmsAssert(hIT8 != NULL);
+
+ sprintf(Buff, it8->DoubleFormatter, Val);
+
+ return SetData(it8, row, col, Buff);
+}
+
+
+
+const char* CMSEXPORT cmsIT8GetData(cmsHANDLE hIT8, const char* cPatch, const char* cSample)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ int iField, iSet;
+
+ _cmsAssert(hIT8 != NULL);
+
+ iField = LocateSample(it8, cSample);
+ if (iField < 0) {
+ return NULL;
+ }
+
+ iSet = LocatePatch(it8, cPatch);
+ if (iSet < 0) {
+ return NULL;
+ }
+
+ return GetData(it8, iSet, iField);
+}
+
+
+cmsFloat64Number CMSEXPORT cmsIT8GetDataDbl(cmsHANDLE it8, const char* cPatch, const char* cSample)
+{
+ const char* Buffer;
+
+ Buffer = cmsIT8GetData(it8, cPatch, cSample);
+
+ return ParseFloatNumber(Buffer);
+}
+
+
+
+cmsBool CMSEXPORT cmsIT8SetData(cmsHANDLE hIT8, const char* cPatch, const char* cSample, const char *Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ int iField, iSet;
+ TABLE* t;
+
+ _cmsAssert(hIT8 != NULL);
+
+ t = GetTable(it8);
+
+ iField = LocateSample(it8, cSample);
+
+ if (iField < 0)
+ return FALSE;
+
+ if (t-> nPatches == 0) {
+
+ AllocateDataFormat(it8);
+ AllocateDataSet(it8);
+ CookPointers(it8);
+ }
+
+ if (cmsstrcasecmp(cSample, "SAMPLE_ID") == 0) {
+
+ iSet = LocateEmptyPatch(it8);
+ if (iSet < 0) {
+ return SynError(it8, "Couldn't add more patches '%s'\n", cPatch);
+ }
+
+ iField = t -> SampleID;
+ }
+ else {
+ iSet = LocatePatch(it8, cPatch);
+ if (iSet < 0) {
+ return FALSE;
+ }
+ }
+
+ return SetData(it8, iSet, iField, Val);
+}
+
+
+cmsBool CMSEXPORT cmsIT8SetDataDbl(cmsHANDLE hIT8, const char* cPatch,
+ const char* cSample,
+ cmsFloat64Number Val)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ char Buff[256];
+
+ _cmsAssert(hIT8 != NULL);
+
+ snprintf(Buff, 255, it8->DoubleFormatter, Val);
+ return cmsIT8SetData(hIT8, cPatch, cSample, Buff);
+}
+
+// Buffer should get MAXSTR at least
+
+const char* CMSEXPORT cmsIT8GetPatchName(cmsHANDLE hIT8, int nPatch, char* buffer)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ TABLE* t;
+ char* Data;
+
+ _cmsAssert(hIT8 != NULL);
+
+ t = GetTable(it8);
+ Data = GetData(it8, nPatch, t->SampleID);
+
+ if (!Data) return NULL;
+ if (!buffer) return Data;
+
+ strncpy(buffer, Data, MAXSTR-1);
+ buffer[MAXSTR-1] = 0;
+ return buffer;
+}
+
+int CMSEXPORT cmsIT8GetPatchByName(cmsHANDLE hIT8, const char *cPatch)
+{
+ _cmsAssert(hIT8 != NULL);
+
+ return LocatePatch((cmsIT8*)hIT8, cPatch);
+}
+
+cmsUInt32Number CMSEXPORT cmsIT8TableCount(cmsHANDLE hIT8)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ _cmsAssert(hIT8 != NULL);
+
+ return it8 ->TablesCount;
+}
+
+// This handles the "LABEL" extension.
+// Label, nTable, Type
+
+int CMSEXPORT cmsIT8SetTableByLabel(cmsHANDLE hIT8, const char* cSet, const char* cField, const char* ExpectedType)
+{
+ const char* cLabelFld;
+ char Type[256], Label[256];
+ int nTable;
+
+ _cmsAssert(hIT8 != NULL);
+
+ if (cField != NULL && *cField == 0)
+ cField = "LABEL";
+
+ if (cField == NULL)
+ cField = "LABEL";
+
+ cLabelFld = cmsIT8GetData(hIT8, cSet, cField);
+ if (!cLabelFld) return -1;
+
+ if (sscanf(cLabelFld, "%255s %d %255s", Label, &nTable, Type) != 3)
+ return -1;
+
+ if (ExpectedType != NULL && *ExpectedType == 0)
+ ExpectedType = NULL;
+
+ if (ExpectedType) {
+
+ if (cmsstrcasecmp(Type, ExpectedType) != 0) return -1;
+ }
+
+ return cmsIT8SetTable(hIT8, nTable);
+}
+
+
+cmsBool CMSEXPORT cmsIT8SetIndexColumn(cmsHANDLE hIT8, const char* cSample)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+ int pos;
+
+ _cmsAssert(hIT8 != NULL);
+
+ pos = LocateSample(it8, cSample);
+ if(pos == -1)
+ return FALSE;
+
+ it8->Tab[it8->nTable].SampleID = pos;
+ return TRUE;
+}
+
+
+void CMSEXPORT cmsIT8DefineDblFormat(cmsHANDLE hIT8, const char* Formatter)
+{
+ cmsIT8* it8 = (cmsIT8*) hIT8;
+
+ _cmsAssert(hIT8 != NULL);
+
+ if (Formatter == NULL)
+ strcpy(it8->DoubleFormatter, DEFAULT_DBL_FORMAT);
+ else
+ strncpy(it8->DoubleFormatter, Formatter, sizeof(it8->DoubleFormatter));
+
+ it8 ->DoubleFormatter[sizeof(it8 ->DoubleFormatter)-1] = 0;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscnvrt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscnvrt.c
new file mode 100644
index 0000000000..1a93e83f90
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmscnvrt.c
@@ -0,0 +1,1142 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+
+// Link several profiles to obtain a single LUT modelling the whole color transform. Intents, Black point
+// compensation and Adaptation parameters may vary across profiles. BPC and Adaptation refers to the PCS
+// after the profile. I.e, BPC[0] refers to connexion between profile(0) and profile(1)
+cmsPipeline* _cmsLinkProfiles(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+//---------------------------------------------------------------------------------
+
+// This is the default routine for ICC-style intents. A user may decide to override it by using a plugin.
+// Supported intents are perceptual, relative colorimetric, saturation and ICC-absolute colorimetric
+static
+cmsPipeline* DefaultICCintents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+//---------------------------------------------------------------------------------
+
+// This is the entry for black-preserving K-only intents, which are non-ICC. Last profile have to be a output profile
+// to do the trick (no devicelinks allowed at that position)
+static
+cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+//---------------------------------------------------------------------------------
+
+// This is the entry for black-plane preserving, which are non-ICC. Again, Last profile have to be a output profile
+// to do the trick (no devicelinks allowed at that position)
+static
+cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number Intents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+//---------------------------------------------------------------------------------
+
+
+// This is a structure holding implementations for all supported intents.
+typedef struct _cms_intents_list {
+
+ cmsUInt32Number Intent;
+ char Description[256];
+ cmsIntentFn Link;
+ struct _cms_intents_list* Next;
+
+} cmsIntentsList;
+
+
+// Built-in intents
+static cmsIntentsList DefaultIntents[] = {
+
+ { INTENT_PERCEPTUAL, "Perceptual", DefaultICCintents, &DefaultIntents[1] },
+ { INTENT_RELATIVE_COLORIMETRIC, "Relative colorimetric", DefaultICCintents, &DefaultIntents[2] },
+ { INTENT_SATURATION, "Saturation", DefaultICCintents, &DefaultIntents[3] },
+ { INTENT_ABSOLUTE_COLORIMETRIC, "Absolute colorimetric", DefaultICCintents, &DefaultIntents[4] },
+ { INTENT_PRESERVE_K_ONLY_PERCEPTUAL, "Perceptual preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[5] },
+ { INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC, "Relative colorimetric preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[6] },
+ { INTENT_PRESERVE_K_ONLY_SATURATION, "Saturation preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[7] },
+ { INTENT_PRESERVE_K_PLANE_PERCEPTUAL, "Perceptual preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[8] },
+ { INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC,"Relative colorimetric preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[9] },
+ { INTENT_PRESERVE_K_PLANE_SATURATION, "Saturation preserving black plane", BlackPreservingKPlaneIntents, NULL }
+};
+
+
+// A pointer to the begining of the list
+_cmsIntentsPluginChunkType _cmsIntentsPluginChunk = { NULL };
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupPluginIntentsList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsIntentsPluginChunkType newHead = { NULL };
+ cmsIntentsList* entry;
+ cmsIntentsList* Anterior = NULL;
+ _cmsIntentsPluginChunkType* head = (_cmsIntentsPluginChunkType*) src->chunks[IntentPlugin];
+
+ // Walk the list copying all nodes
+ for (entry = head->Intents;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ cmsIntentsList *newEntry = ( cmsIntentsList *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(cmsIntentsList));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.Intents == NULL)
+ newHead.Intents = newEntry;
+ }
+
+ ctx ->chunks[IntentPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsIntentsPluginChunkType));
+}
+
+void _cmsAllocIntentsPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ // Copy all linked list
+ DupPluginIntentsList(ctx, src);
+ }
+ else {
+ static _cmsIntentsPluginChunkType IntentsPluginChunkType = { NULL };
+ ctx ->chunks[IntentPlugin] = _cmsSubAllocDup(ctx ->MemPool, &IntentsPluginChunkType, sizeof(_cmsIntentsPluginChunkType));
+ }
+}
+
+
+// Search the list for a suitable intent. Returns NULL if not found
+static
+cmsIntentsList* SearchIntent(cmsContext ContextID, cmsUInt32Number Intent)
+{
+ _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(ContextID, IntentPlugin);
+ cmsIntentsList* pt;
+
+ for (pt = ctx -> Intents; pt != NULL; pt = pt -> Next)
+ if (pt ->Intent == Intent) return pt;
+
+ for (pt = DefaultIntents; pt != NULL; pt = pt -> Next)
+ if (pt ->Intent == Intent) return pt;
+
+ return NULL;
+}
+
+// Black point compensation. Implemented as a linear scaling in XYZ. Black points
+// should come relative to the white point. Fills an matrix/offset element m
+// which is organized as a 4x4 matrix.
+static
+void ComputeBlackPointCompensation(const cmsCIEXYZ* BlackPointIn,
+ const cmsCIEXYZ* BlackPointOut,
+ cmsMAT3* m, cmsVEC3* off)
+{
+ cmsFloat64Number ax, ay, az, bx, by, bz, tx, ty, tz;
+
+ // Now we need to compute a matrix plus an offset m and of such of
+ // [m]*bpin + off = bpout
+ // [m]*D50 + off = D50
+ //
+ // This is a linear scaling in the form ax+b, where
+ // a = (bpout - D50) / (bpin - D50)
+ // b = - D50* (bpout - bpin) / (bpin - D50)
+
+ tx = BlackPointIn->X - cmsD50_XYZ()->X;
+ ty = BlackPointIn->Y - cmsD50_XYZ()->Y;
+ tz = BlackPointIn->Z - cmsD50_XYZ()->Z;
+
+ ax = (BlackPointOut->X - cmsD50_XYZ()->X) / tx;
+ ay = (BlackPointOut->Y - cmsD50_XYZ()->Y) / ty;
+ az = (BlackPointOut->Z - cmsD50_XYZ()->Z) / tz;
+
+ bx = - cmsD50_XYZ()-> X * (BlackPointOut->X - BlackPointIn->X) / tx;
+ by = - cmsD50_XYZ()-> Y * (BlackPointOut->Y - BlackPointIn->Y) / ty;
+ bz = - cmsD50_XYZ()-> Z * (BlackPointOut->Z - BlackPointIn->Z) / tz;
+
+ _cmsVEC3init(&m ->v[0], ax, 0, 0);
+ _cmsVEC3init(&m ->v[1], 0, ay, 0);
+ _cmsVEC3init(&m ->v[2], 0, 0, az);
+ _cmsVEC3init(off, bx, by, bz);
+
+}
+
+
+// Approximate a blackbody illuminant based on CHAD information
+static
+cmsFloat64Number CHAD2Temp(const cmsMAT3* Chad)
+{
+ // Convert D50 across inverse CHAD to get the absolute white point
+ cmsVEC3 d, s;
+ cmsCIEXYZ Dest;
+ cmsCIExyY DestChromaticity;
+ cmsFloat64Number TempK;
+ cmsMAT3 m1, m2;
+
+ m1 = *Chad;
+ if (!_cmsMAT3inverse(&m1, &m2)) return FALSE;
+
+ s.n[VX] = cmsD50_XYZ() -> X;
+ s.n[VY] = cmsD50_XYZ() -> Y;
+ s.n[VZ] = cmsD50_XYZ() -> Z;
+
+ _cmsMAT3eval(&d, &m2, &s);
+
+ Dest.X = d.n[VX];
+ Dest.Y = d.n[VY];
+ Dest.Z = d.n[VZ];
+
+ cmsXYZ2xyY(&DestChromaticity, &Dest);
+
+ if (!cmsTempFromWhitePoint(&TempK, &DestChromaticity))
+ return -1.0;
+
+ return TempK;
+}
+
+// Compute a CHAD based on a given temperature
+static
+ void Temp2CHAD(cmsMAT3* Chad, cmsFloat64Number Temp)
+{
+ cmsCIEXYZ White;
+ cmsCIExyY ChromaticityOfWhite;
+
+ cmsWhitePointFromTemp(&ChromaticityOfWhite, Temp);
+ cmsxyY2XYZ(&White, &ChromaticityOfWhite);
+ _cmsAdaptationMatrix(Chad, NULL, &White, cmsD50_XYZ());
+}
+
+// Join scalings to obtain relative input to absolute and then to relative output.
+// Result is stored in a 3x3 matrix
+static
+cmsBool ComputeAbsoluteIntent(cmsFloat64Number AdaptationState,
+ const cmsCIEXYZ* WhitePointIn,
+ const cmsMAT3* ChromaticAdaptationMatrixIn,
+ const cmsCIEXYZ* WhitePointOut,
+ const cmsMAT3* ChromaticAdaptationMatrixOut,
+ cmsMAT3* m)
+{
+ cmsMAT3 Scale, m1, m2, m3, m4;
+
+ // Adaptation state
+ if (AdaptationState == 1.0) {
+
+ // Observer is fully adapted. Keep chromatic adaptation.
+ // That is the standard V4 behaviour
+ _cmsVEC3init(&m->v[0], WhitePointIn->X / WhitePointOut->X, 0, 0);
+ _cmsVEC3init(&m->v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0);
+ _cmsVEC3init(&m->v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z);
+
+ }
+ else {
+
+ // Incomplete adaptation. This is an advanced feature.
+ _cmsVEC3init(&Scale.v[0], WhitePointIn->X / WhitePointOut->X, 0, 0);
+ _cmsVEC3init(&Scale.v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0);
+ _cmsVEC3init(&Scale.v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z);
+
+
+ if (AdaptationState == 0.0) {
+
+ m1 = *ChromaticAdaptationMatrixOut;
+ _cmsMAT3per(&m2, &m1, &Scale);
+ // m2 holds CHAD from output white to D50 times abs. col. scaling
+
+ // Observer is not adapted, undo the chromatic adaptation
+ _cmsMAT3per(m, &m2, ChromaticAdaptationMatrixOut);
+
+ m3 = *ChromaticAdaptationMatrixIn;
+ if (!_cmsMAT3inverse(&m3, &m4)) return FALSE;
+ _cmsMAT3per(m, &m2, &m4);
+
+ } else {
+
+ cmsMAT3 MixedCHAD;
+ cmsFloat64Number TempSrc, TempDest, Temp;
+
+ m1 = *ChromaticAdaptationMatrixIn;
+ if (!_cmsMAT3inverse(&m1, &m2)) return FALSE;
+ _cmsMAT3per(&m3, &m2, &Scale);
+ // m3 holds CHAD from input white to D50 times abs. col. scaling
+
+ TempSrc = CHAD2Temp(ChromaticAdaptationMatrixIn);
+ TempDest = CHAD2Temp(ChromaticAdaptationMatrixOut);
+
+ if (TempSrc < 0.0 || TempDest < 0.0) return FALSE; // Something went wrong
+
+ if (_cmsMAT3isIdentity(&Scale) && fabs(TempSrc - TempDest) < 0.01) {
+
+ _cmsMAT3identity(m);
+ return TRUE;
+ }
+
+ Temp = (1.0 - AdaptationState) * TempDest + AdaptationState * TempSrc;
+
+ // Get a CHAD from whatever output temperature to D50. This replaces output CHAD
+ Temp2CHAD(&MixedCHAD, Temp);
+
+ _cmsMAT3per(m, &m3, &MixedCHAD);
+ }
+
+ }
+ return TRUE;
+
+}
+
+// Just to see if m matrix should be applied
+static
+cmsBool IsEmptyLayer(cmsMAT3* m, cmsVEC3* off)
+{
+ cmsFloat64Number diff = 0;
+ cmsMAT3 Ident;
+ int i;
+
+ if (m == NULL && off == NULL) return TRUE; // NULL is allowed as an empty layer
+ if (m == NULL && off != NULL) return FALSE; // This is an internal error
+
+ _cmsMAT3identity(&Ident);
+
+ for (i=0; i < 3*3; i++)
+ diff += fabs(((cmsFloat64Number*)m)[i] - ((cmsFloat64Number*)&Ident)[i]);
+
+ for (i=0; i < 3; i++)
+ diff += fabs(((cmsFloat64Number*)off)[i]);
+
+
+ return (diff < 0.002);
+}
+
+
+// Compute the conversion layer
+static
+cmsBool ComputeConversion(int i, cmsHPROFILE hProfiles[],
+ cmsUInt32Number Intent,
+ cmsBool BPC,
+ cmsFloat64Number AdaptationState,
+ cmsMAT3* m, cmsVEC3* off)
+{
+
+ int k;
+
+ // m and off are set to identity and this is detected latter on
+ _cmsMAT3identity(m);
+ _cmsVEC3init(off, 0, 0, 0);
+
+ // If intent is abs. colorimetric,
+ if (Intent == INTENT_ABSOLUTE_COLORIMETRIC) {
+
+ cmsCIEXYZ WhitePointIn, WhitePointOut;
+ cmsMAT3 ChromaticAdaptationMatrixIn, ChromaticAdaptationMatrixOut;
+
+ _cmsReadMediaWhitePoint(&WhitePointIn, hProfiles[i-1]);
+ _cmsReadCHAD(&ChromaticAdaptationMatrixIn, hProfiles[i-1]);
+
+ _cmsReadMediaWhitePoint(&WhitePointOut, hProfiles[i]);
+ _cmsReadCHAD(&ChromaticAdaptationMatrixOut, hProfiles[i]);
+
+ if (!ComputeAbsoluteIntent(AdaptationState,
+ &WhitePointIn, &ChromaticAdaptationMatrixIn,
+ &WhitePointOut, &ChromaticAdaptationMatrixOut, m)) return FALSE;
+
+ }
+ else {
+ // Rest of intents may apply BPC.
+
+ if (BPC) {
+
+ cmsCIEXYZ BlackPointIn, BlackPointOut;
+
+ cmsDetectBlackPoint(&BlackPointIn, hProfiles[i-1], Intent, 0);
+ cmsDetectDestinationBlackPoint(&BlackPointOut, hProfiles[i], Intent, 0);
+
+ // If black points are equal, then do nothing
+ if (BlackPointIn.X != BlackPointOut.X ||
+ BlackPointIn.Y != BlackPointOut.Y ||
+ BlackPointIn.Z != BlackPointOut.Z)
+ ComputeBlackPointCompensation(&BlackPointIn, &BlackPointOut, m, off);
+ }
+ }
+
+ // Offset should be adjusted because the encoding. We encode XYZ normalized to 0..1.0,
+ // to do that, we divide by MAX_ENCODEABLE_XZY. The conversion stage goes XYZ -> XYZ so
+ // we have first to convert from encoded to XYZ and then convert back to encoded.
+ // y = Mx + Off
+ // x = x'c
+ // y = M x'c + Off
+ // y = y'c; y' = y / c
+ // y' = (Mx'c + Off) /c = Mx' + (Off / c)
+
+ for (k=0; k < 3; k++) {
+ off ->n[k] /= MAX_ENCODEABLE_XYZ;
+ }
+
+ return TRUE;
+}
+
+
+// Add a conversion stage if needed. If a matrix/offset m is given, it applies to XYZ space
+static
+cmsBool AddConversion(cmsPipeline* Result, cmsColorSpaceSignature InPCS, cmsColorSpaceSignature OutPCS, cmsMAT3* m, cmsVEC3* off)
+{
+ cmsFloat64Number* m_as_dbl = (cmsFloat64Number*) m;
+ cmsFloat64Number* off_as_dbl = (cmsFloat64Number*) off;
+
+ // Handle PCS mismatches. A specialized stage is added to the LUT in such case
+ switch (InPCS) {
+
+ case cmsSigXYZData: // Input profile operates in XYZ
+
+ switch (OutPCS) {
+
+ case cmsSigXYZData: // XYZ -> XYZ
+ if (!IsEmptyLayer(m, off) &&
+ !cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl)))
+ return FALSE;
+ break;
+
+ case cmsSigLabData: // XYZ -> Lab
+ if (!IsEmptyLayer(m, off) &&
+ !cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl)))
+ return FALSE;
+ if (!cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocXYZ2Lab(Result ->ContextID)))
+ return FALSE;
+ break;
+
+ default:
+ return FALSE; // Colorspace mismatch
+ }
+ break;
+
+ case cmsSigLabData: // Input profile operates in Lab
+
+ switch (OutPCS) {
+
+ case cmsSigXYZData: // Lab -> XYZ
+
+ if (!cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocLab2XYZ(Result ->ContextID)))
+ return FALSE;
+ if (!IsEmptyLayer(m, off) &&
+ !cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl)))
+ return FALSE;
+ break;
+
+ case cmsSigLabData: // Lab -> Lab
+
+ if (!IsEmptyLayer(m, off)) {
+ if (!cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocLab2XYZ(Result ->ContextID)) ||
+ !cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl)) ||
+ !cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocXYZ2Lab(Result ->ContextID)))
+ return FALSE;
+ }
+ break;
+
+ default:
+ return FALSE; // Mismatch
+ }
+ break;
+
+ // On colorspaces other than PCS, check for same space
+ default:
+ if (InPCS != OutPCS) return FALSE;
+ break;
+ }
+
+ return TRUE;
+}
+
+
+// Is a given space compatible with another?
+static
+cmsBool ColorSpaceIsCompatible(cmsColorSpaceSignature a, cmsColorSpaceSignature b)
+{
+ // If they are same, they are compatible.
+ if (a == b) return TRUE;
+
+ // Check for MCH4 substitution of CMYK
+ if ((a == cmsSig4colorData) && (b == cmsSigCmykData)) return TRUE;
+ if ((a == cmsSigCmykData) && (b == cmsSig4colorData)) return TRUE;
+
+ // Check for XYZ/Lab. Those spaces are interchangeable as they can be computed one from other.
+ if ((a == cmsSigXYZData) && (b == cmsSigLabData)) return TRUE;
+ if ((a == cmsSigLabData) && (b == cmsSigXYZData)) return TRUE;
+
+ return FALSE;
+}
+
+
+// Default handler for ICC-style intents
+static
+cmsPipeline* DefaultICCintents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ cmsPipeline* Lut = NULL;
+ cmsPipeline* Result;
+ cmsHPROFILE hProfile;
+ cmsMAT3 m;
+ cmsVEC3 off;
+ cmsColorSpaceSignature ColorSpaceIn, ColorSpaceOut, CurrentColorSpace;
+ cmsProfileClassSignature ClassSig;
+ cmsUInt32Number i, Intent;
+
+ // For safety
+ if (nProfiles == 0) return NULL;
+
+ // Allocate an empty LUT for holding the result. 0 as channel count means 'undefined'
+ Result = cmsPipelineAlloc(ContextID, 0, 0);
+ if (Result == NULL) return NULL;
+
+ CurrentColorSpace = cmsGetColorSpace(hProfiles[0]);
+
+ for (i=0; i < nProfiles; i++) {
+
+ cmsBool lIsDeviceLink, lIsInput;
+
+ hProfile = hProfiles[i];
+ ClassSig = cmsGetDeviceClass(hProfile);
+ lIsDeviceLink = (ClassSig == cmsSigLinkClass || ClassSig == cmsSigAbstractClass );
+
+ // First profile is used as input unless devicelink or abstract
+ if ((i == 0) && !lIsDeviceLink) {
+ lIsInput = TRUE;
+ }
+ else {
+ // Else use profile in the input direction if current space is not PCS
+ lIsInput = (CurrentColorSpace != cmsSigXYZData) &&
+ (CurrentColorSpace != cmsSigLabData);
+ }
+
+ Intent = TheIntents[i];
+
+ if (lIsInput || lIsDeviceLink) {
+
+ ColorSpaceIn = cmsGetColorSpace(hProfile);
+ ColorSpaceOut = cmsGetPCS(hProfile);
+ }
+ else {
+
+ ColorSpaceIn = cmsGetPCS(hProfile);
+ ColorSpaceOut = cmsGetColorSpace(hProfile);
+ }
+
+ if (!ColorSpaceIsCompatible(ColorSpaceIn, CurrentColorSpace)) {
+
+ cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "ColorSpace mismatch");
+ goto Error;
+ }
+
+ // If devicelink is found, then no custom intent is allowed and we can
+ // read the LUT to be applied. Settings don't apply here.
+ if (lIsDeviceLink || ((ClassSig == cmsSigNamedColorClass) && (nProfiles == 1))) {
+
+ // Get the involved LUT from the profile
+ Lut = _cmsReadDevicelinkLUT(hProfile, Intent);
+ if (Lut == NULL) goto Error;
+
+ // What about abstract profiles?
+ if (ClassSig == cmsSigAbstractClass && i > 0) {
+ if (!ComputeConversion(i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error;
+ }
+ else {
+ _cmsMAT3identity(&m);
+ _cmsVEC3init(&off, 0, 0, 0);
+ }
+
+
+ if (!AddConversion(Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error;
+
+ }
+ else {
+
+ if (lIsInput) {
+ // Input direction means non-pcs connection, so proceed like devicelinks
+ Lut = _cmsReadInputLUT(hProfile, Intent);
+ if (Lut == NULL) goto Error;
+ }
+ else {
+
+ // Output direction means PCS connection. Intent may apply here
+ Lut = _cmsReadOutputLUT(hProfile, Intent);
+ if (Lut == NULL) goto Error;
+
+
+ if (!ComputeConversion(i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error;
+ if (!AddConversion(Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error;
+
+ }
+ }
+
+ // Concatenate to the output LUT
+ if (!cmsPipelineCat(Result, Lut))
+ goto Error;
+
+ cmsPipelineFree(Lut);
+ Lut = NULL;
+
+ // Update current space
+ CurrentColorSpace = ColorSpaceOut;
+ }
+
+ return Result;
+
+Error:
+
+ if (Lut != NULL) cmsPipelineFree(Lut);
+ if (Result != NULL) cmsPipelineFree(Result);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(dwFlags);
+}
+
+
+// Wrapper for DLL calling convention
+cmsPipeline* CMSEXPORT _cmsDefaultICCintents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ return DefaultICCintents(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags);
+}
+
+// Black preserving intents ---------------------------------------------------------------------------------------------
+
+// Translate black-preserving intents to ICC ones
+static
+int TranslateNonICCIntents(int Intent)
+{
+ switch (Intent) {
+ case INTENT_PRESERVE_K_ONLY_PERCEPTUAL:
+ case INTENT_PRESERVE_K_PLANE_PERCEPTUAL:
+ return INTENT_PERCEPTUAL;
+
+ case INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC:
+ case INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC:
+ return INTENT_RELATIVE_COLORIMETRIC;
+
+ case INTENT_PRESERVE_K_ONLY_SATURATION:
+ case INTENT_PRESERVE_K_PLANE_SATURATION:
+ return INTENT_SATURATION;
+
+ default: return Intent;
+ }
+}
+
+// Sampler for Black-only preserving CMYK->CMYK transforms
+
+typedef struct {
+ cmsPipeline* cmyk2cmyk; // The original transform
+ cmsToneCurve* KTone; // Black-to-black tone curve
+
+} GrayOnlyParams;
+
+
+// Preserve black only if that is the only ink used
+static
+int BlackPreservingGrayOnlySampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ GrayOnlyParams* bp = (GrayOnlyParams*) Cargo;
+
+ // If going across black only, keep black only
+ if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
+
+ // TAC does not apply because it is black ink!
+ Out[0] = Out[1] = Out[2] = 0;
+ Out[3] = cmsEvalToneCurve16(bp->KTone, In[3]);
+ return TRUE;
+ }
+
+ // Keep normal transform for other colors
+ bp ->cmyk2cmyk ->Eval16Fn(In, Out, bp ->cmyk2cmyk->Data);
+ return TRUE;
+}
+
+// This is the entry for black-preserving K-only intents, which are non-ICC
+static
+cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ GrayOnlyParams bp;
+ cmsPipeline* Result;
+ cmsUInt32Number ICCIntents[256];
+ cmsStage* CLUT;
+ cmsUInt32Number i, nGridPoints;
+
+
+ // Sanity check
+ if (nProfiles < 1 || nProfiles > 255) return NULL;
+
+ // Translate black-preserving intents to ICC ones
+ for (i=0; i < nProfiles; i++)
+ ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]);
+
+ // Check for non-cmyk profiles
+ if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
+ cmsGetColorSpace(hProfiles[nProfiles-1]) != cmsSigCmykData)
+ return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags);
+
+ memset(&bp, 0, sizeof(bp));
+
+ // Allocate an empty LUT for holding the result
+ Result = cmsPipelineAlloc(ContextID, 4, 4);
+ if (Result == NULL) return NULL;
+
+ // Create a LUT holding normal ICC transform
+ bp.cmyk2cmyk = DefaultICCintents(ContextID,
+ nProfiles,
+ ICCIntents,
+ hProfiles,
+ BPC,
+ AdaptationStates,
+ dwFlags);
+
+ if (bp.cmyk2cmyk == NULL) goto Error;
+
+ // Now, compute the tone curve
+ bp.KTone = _cmsBuildKToneCurve(ContextID,
+ 4096,
+ nProfiles,
+ ICCIntents,
+ hProfiles,
+ BPC,
+ AdaptationStates,
+ dwFlags);
+
+ if (bp.KTone == NULL) goto Error;
+
+
+ // How many gridpoints are we going to use?
+ nGridPoints = _cmsReasonableGridpointsByColorspace(cmsSigCmykData, dwFlags);
+
+ // Create the CLUT. 16 bits
+ CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL);
+ if (CLUT == NULL) goto Error;
+
+ // This is the one and only MPE in this LUT
+ if (!cmsPipelineInsertStage(Result, cmsAT_BEGIN, CLUT))
+ goto Error;
+
+ // Sample it. We cannot afford pre/post linearization this time.
+ if (!cmsStageSampleCLut16bit(CLUT, BlackPreservingGrayOnlySampler, (void*) &bp, 0))
+ goto Error;
+
+ // Get rid of xform and tone curve
+ cmsPipelineFree(bp.cmyk2cmyk);
+ cmsFreeToneCurve(bp.KTone);
+
+ return Result;
+
+Error:
+
+ if (bp.cmyk2cmyk != NULL) cmsPipelineFree(bp.cmyk2cmyk);
+ if (bp.KTone != NULL) cmsFreeToneCurve(bp.KTone);
+ if (Result != NULL) cmsPipelineFree(Result);
+ return NULL;
+
+}
+
+// K Plane-preserving CMYK to CMYK ------------------------------------------------------------------------------------
+
+typedef struct {
+
+ cmsPipeline* cmyk2cmyk; // The original transform
+ cmsHTRANSFORM hProofOutput; // Output CMYK to Lab (last profile)
+ cmsHTRANSFORM cmyk2Lab; // The input chain
+ cmsToneCurve* KTone; // Black-to-black tone curve
+ cmsPipeline* LabK2cmyk; // The output profile
+ cmsFloat64Number MaxError;
+
+ cmsHTRANSFORM hRoundTrip;
+ cmsFloat64Number MaxTAC;
+
+
+} PreserveKPlaneParams;
+
+
+// The CLUT will be stored at 16 bits, but calculations are performed at cmsFloat32Number precision
+static
+int BlackPreservingSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ int i;
+ cmsFloat32Number Inf[4], Outf[4];
+ cmsFloat32Number LabK[4];
+ cmsFloat64Number SumCMY, SumCMYK, Error, Ratio;
+ cmsCIELab ColorimetricLab, BlackPreservingLab;
+ PreserveKPlaneParams* bp = (PreserveKPlaneParams*) Cargo;
+
+ // Convert from 16 bits to floating point
+ for (i=0; i < 4; i++)
+ Inf[i] = (cmsFloat32Number) (In[i] / 65535.0);
+
+ // Get the K across Tone curve
+ LabK[3] = cmsEvalToneCurveFloat(bp ->KTone, Inf[3]);
+
+ // If going across black only, keep black only
+ if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
+
+ Out[0] = Out[1] = Out[2] = 0;
+ Out[3] = _cmsQuickSaturateWord(LabK[3] * 65535.0);
+ return TRUE;
+ }
+
+ // Try the original transform,
+ cmsPipelineEvalFloat( Inf, Outf, bp ->cmyk2cmyk);
+
+ // Store a copy of the floating point result into 16-bit
+ for (i=0; i < 4; i++)
+ Out[i] = _cmsQuickSaturateWord(Outf[i] * 65535.0);
+
+ // Maybe K is already ok (mostly on K=0)
+ if ( fabs(Outf[3] - LabK[3]) < (3.0 / 65535.0) ) {
+ return TRUE;
+ }
+
+ // K differ, mesure and keep Lab measurement for further usage
+ // this is done in relative colorimetric intent
+ cmsDoTransform(bp->hProofOutput, Out, &ColorimetricLab, 1);
+
+ // Is not black only and the transform doesn't keep black.
+ // Obtain the Lab of output CMYK. After that we have Lab + K
+ cmsDoTransform(bp ->cmyk2Lab, Outf, LabK, 1);
+
+ // Obtain the corresponding CMY using reverse interpolation
+ // (K is fixed in LabK[3])
+ if (!cmsPipelineEvalReverseFloat(LabK, Outf, Outf, bp ->LabK2cmyk)) {
+
+ // Cannot find a suitable value, so use colorimetric xform
+ // which is already stored in Out[]
+ return TRUE;
+ }
+
+ // Make sure to pass thru K (which now is fixed)
+ Outf[3] = LabK[3];
+
+ // Apply TAC if needed
+ SumCMY = Outf[0] + Outf[1] + Outf[2];
+ SumCMYK = SumCMY + Outf[3];
+
+ if (SumCMYK > bp ->MaxTAC) {
+
+ Ratio = 1 - ((SumCMYK - bp->MaxTAC) / SumCMY);
+ if (Ratio < 0)
+ Ratio = 0;
+ }
+ else
+ Ratio = 1.0;
+
+ Out[0] = _cmsQuickSaturateWord(Outf[0] * Ratio * 65535.0); // C
+ Out[1] = _cmsQuickSaturateWord(Outf[1] * Ratio * 65535.0); // M
+ Out[2] = _cmsQuickSaturateWord(Outf[2] * Ratio * 65535.0); // Y
+ Out[3] = _cmsQuickSaturateWord(Outf[3] * 65535.0);
+
+ // Estimate the error (this goes 16 bits to Lab DBL)
+ cmsDoTransform(bp->hProofOutput, Out, &BlackPreservingLab, 1);
+ Error = cmsDeltaE(&ColorimetricLab, &BlackPreservingLab);
+ if (Error > bp -> MaxError)
+ bp->MaxError = Error;
+
+ return TRUE;
+}
+
+// This is the entry for black-plane preserving, which are non-ICC
+static
+cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ PreserveKPlaneParams bp;
+ cmsPipeline* Result = NULL;
+ cmsUInt32Number ICCIntents[256];
+ cmsStage* CLUT;
+ cmsUInt32Number i, nGridPoints;
+ cmsHPROFILE hLab;
+
+ // Sanity check
+ if (nProfiles < 1 || nProfiles > 255) return NULL;
+
+ // Translate black-preserving intents to ICC ones
+ for (i=0; i < nProfiles; i++)
+ ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]);
+
+ // Check for non-cmyk profiles
+ if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
+ !(cmsGetColorSpace(hProfiles[nProfiles-1]) == cmsSigCmykData ||
+ cmsGetDeviceClass(hProfiles[nProfiles-1]) == cmsSigOutputClass))
+ return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags);
+
+ // Allocate an empty LUT for holding the result
+ Result = cmsPipelineAlloc(ContextID, 4, 4);
+ if (Result == NULL) return NULL;
+
+
+ memset(&bp, 0, sizeof(bp));
+
+ // We need the input LUT of the last profile, assuming this one is responsible of
+ // black generation. This LUT will be seached in inverse order.
+ bp.LabK2cmyk = _cmsReadInputLUT(hProfiles[nProfiles-1], INTENT_RELATIVE_COLORIMETRIC);
+ if (bp.LabK2cmyk == NULL) goto Cleanup;
+
+ // Get total area coverage (in 0..1 domain)
+ bp.MaxTAC = cmsDetectTAC(hProfiles[nProfiles-1]) / 100.0;
+ if (bp.MaxTAC <= 0) goto Cleanup;
+
+
+ // Create a LUT holding normal ICC transform
+ bp.cmyk2cmyk = DefaultICCintents(ContextID,
+ nProfiles,
+ ICCIntents,
+ hProfiles,
+ BPC,
+ AdaptationStates,
+ dwFlags);
+ if (bp.cmyk2cmyk == NULL) goto Cleanup;
+
+ // Now the tone curve
+ bp.KTone = _cmsBuildKToneCurve(ContextID, 4096, nProfiles,
+ ICCIntents,
+ hProfiles,
+ BPC,
+ AdaptationStates,
+ dwFlags);
+ if (bp.KTone == NULL) goto Cleanup;
+
+ // To measure the output, Last profile to Lab
+ hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
+ bp.hProofOutput = cmsCreateTransformTHR(ContextID, hProfiles[nProfiles-1],
+ CHANNELS_SH(4)|BYTES_SH(2), hLab, TYPE_Lab_DBL,
+ INTENT_RELATIVE_COLORIMETRIC,
+ cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
+ if ( bp.hProofOutput == NULL) goto Cleanup;
+
+ // Same as anterior, but lab in the 0..1 range
+ bp.cmyk2Lab = cmsCreateTransformTHR(ContextID, hProfiles[nProfiles-1],
+ FLOAT_SH(1)|CHANNELS_SH(4)|BYTES_SH(4), hLab,
+ FLOAT_SH(1)|CHANNELS_SH(3)|BYTES_SH(4),
+ INTENT_RELATIVE_COLORIMETRIC,
+ cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
+ if (bp.cmyk2Lab == NULL) goto Cleanup;
+ cmsCloseProfile(hLab);
+
+ // Error estimation (for debug only)
+ bp.MaxError = 0;
+
+ // How many gridpoints are we going to use?
+ nGridPoints = _cmsReasonableGridpointsByColorspace(cmsSigCmykData, dwFlags);
+
+
+ CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL);
+ if (CLUT == NULL) goto Cleanup;
+
+ if (!cmsPipelineInsertStage(Result, cmsAT_BEGIN, CLUT))
+ goto Cleanup;
+
+ cmsStageSampleCLut16bit(CLUT, BlackPreservingSampler, (void*) &bp, 0);
+
+Cleanup:
+
+ if (bp.cmyk2cmyk) cmsPipelineFree(bp.cmyk2cmyk);
+ if (bp.cmyk2Lab) cmsDeleteTransform(bp.cmyk2Lab);
+ if (bp.hProofOutput) cmsDeleteTransform(bp.hProofOutput);
+
+ if (bp.KTone) cmsFreeToneCurve(bp.KTone);
+ if (bp.LabK2cmyk) cmsPipelineFree(bp.LabK2cmyk);
+
+ return Result;
+}
+
+// Link routines ------------------------------------------------------------------------------------------------------
+
+// Chain several profiles into a single LUT. It just checks the parameters and then calls the handler
+// for the first intent in chain. The handler may be user-defined. Is up to the handler to deal with the
+// rest of intents in chain. A maximum of 255 profiles at time are supported, which is pretty reasonable.
+cmsPipeline* _cmsLinkProfiles(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ cmsUInt32Number i;
+ cmsIntentsList* Intent;
+
+ // Make sure a reasonable number of profiles is provided
+ if (nProfiles <= 0 || nProfiles > 255) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't link '%d' profiles", nProfiles);
+ return NULL;
+ }
+
+ for (i=0; i < nProfiles; i++) {
+
+ // Check if black point is really needed or allowed. Note that
+ // following Adobe's document:
+ // BPC does not apply to devicelink profiles, nor to abs colorimetric,
+ // and applies always on V4 perceptual and saturation.
+
+ if (TheIntents[i] == INTENT_ABSOLUTE_COLORIMETRIC)
+ BPC[i] = FALSE;
+
+ if (TheIntents[i] == INTENT_PERCEPTUAL || TheIntents[i] == INTENT_SATURATION) {
+
+ // Force BPC for V4 profiles in perceptual and saturation
+ if (cmsGetProfileVersion(hProfiles[i]) >= 4.0)
+ BPC[i] = TRUE;
+ }
+ }
+
+ // Search for a handler. The first intent in the chain defines the handler. That would
+ // prevent using multiple custom intents in a multiintent chain, but the behaviour of
+ // this case would present some issues if the custom intent tries to do things like
+ // preserve primaries. This solution is not perfect, but works well on most cases.
+
+ Intent = SearchIntent(ContextID, TheIntents[0]);
+ if (Intent == NULL) {
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported intent '%d'", TheIntents[0]);
+ return NULL;
+ }
+
+ // Call the handler
+ return Intent ->Link(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags);
+}
+
+// -------------------------------------------------------------------------------------------------
+
+// Get information about available intents. nMax is the maximum space for the supplied "Codes"
+// and "Descriptions" the function returns the total number of intents, which may be greater
+// than nMax, although the matrices are not populated beyond this level.
+cmsUInt32Number CMSEXPORT cmsGetSupportedIntentsTHR(cmsContext ContextID, cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions)
+{
+ _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(ContextID, IntentPlugin);
+ cmsIntentsList* pt;
+ cmsUInt32Number nIntents;
+
+
+ for (nIntents=0, pt = ctx->Intents; pt != NULL; pt = pt -> Next)
+ {
+ if (nIntents < nMax) {
+ if (Codes != NULL)
+ Codes[nIntents] = pt ->Intent;
+
+ if (Descriptions != NULL)
+ Descriptions[nIntents] = pt ->Description;
+ }
+
+ nIntents++;
+ }
+
+ for (nIntents=0, pt = DefaultIntents; pt != NULL; pt = pt -> Next)
+ {
+ if (nIntents < nMax) {
+ if (Codes != NULL)
+ Codes[nIntents] = pt ->Intent;
+
+ if (Descriptions != NULL)
+ Descriptions[nIntents] = pt ->Description;
+ }
+
+ nIntents++;
+ }
+ return nIntents;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetSupportedIntents(cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions)
+{
+ return cmsGetSupportedIntentsTHR(NULL, nMax, Codes, Descriptions);
+}
+
+// The plug-in registration. User can add new intents or override default routines
+cmsBool _cmsRegisterRenderingIntentPlugin(cmsContext id, cmsPluginBase* Data)
+{
+ _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(id, IntentPlugin);
+ cmsPluginRenderingIntent* Plugin = (cmsPluginRenderingIntent*) Data;
+ cmsIntentsList* fl;
+
+ // Do we have to reset the custom intents?
+ if (Data == NULL) {
+
+ ctx->Intents = NULL;
+ return TRUE;
+ }
+
+ fl = (cmsIntentsList*) _cmsPluginMalloc(id, sizeof(cmsIntentsList));
+ if (fl == NULL) return FALSE;
+
+
+ fl ->Intent = Plugin ->Intent;
+ strncpy(fl ->Description, Plugin ->Description, sizeof(fl ->Description)-1);
+ fl ->Description[sizeof(fl ->Description)-1] = 0;
+
+ fl ->Link = Plugin ->Link;
+
+ fl ->Next = ctx ->Intents;
+ ctx ->Intents = fl;
+
+ return TRUE;
+}
+
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c
new file mode 100644
index 0000000000..745238c6e2
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c
@@ -0,0 +1,707 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// I am so tired about incompatibilities on those functions that here are some replacements
+// that hopefully would be fully portable.
+
+// compare two strings ignoring case
+int CMSEXPORT cmsstrcasecmp(const char* s1, const char* s2)
+{
+ register const unsigned char *us1 = (const unsigned char *)s1,
+ *us2 = (const unsigned char *)s2;
+
+ while (toupper(*us1) == toupper(*us2++))
+ if (*us1++ == '\0')
+ return 0;
+
+ return (toupper(*us1) - toupper(*--us2));
+}
+
+// long int because C99 specifies ftell in such way (7.19.9.2)
+long int CMSEXPORT cmsfilelength(FILE* f)
+{
+ long int p , n;
+
+ p = ftell(f); // register current file position
+
+ if (fseek(f, 0, SEEK_END) != 0) {
+ return -1;
+ }
+
+ n = ftell(f);
+ fseek(f, p, SEEK_SET); // file position restored
+
+ return n;
+}
+
+#if 0
+// Memory handling ------------------------------------------------------------------
+//
+// This is the interface to low-level memory management routines. By default a simple
+// wrapping to malloc/free/realloc is provided, although there is a limit on the max
+// amount of memoy that can be reclaimed. This is mostly as a safety feature to prevent
+// bogus or evil code to allocate huge blocks that otherwise lcms would never need.
+
+#define MAX_MEMORY_FOR_ALLOC ((cmsUInt32Number)(1024U*1024U*512U))
+
+// User may override this behaviour by using a memory plug-in, which basically replaces
+// the default memory management functions. In this case, no check is performed and it
+// is up to the plug-in writter to keep in the safe side. There are only three functions
+// required to be implemented: malloc, realloc and free, although the user may want to
+// replace the optional mallocZero, calloc and dup as well.
+
+cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// *********************************************************************************
+
+// This is the default memory allocation function. It does a very coarse
+// check of amout of memory, just to prevent exploits
+static
+void* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
+{
+ if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never allow over maximum
+
+ return (void*) malloc(size);
+
+ cmsUNUSED_PARAMETER(ContextID);
+}
+
+// Generic allocate & zero
+static
+void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
+{
+ void *pt = _cmsMalloc(ContextID, size);
+ if (pt == NULL) return NULL;
+
+ memset(pt, 0, size);
+ return pt;
+}
+
+
+// The default free function. The only check proformed is against NULL pointers
+static
+void _cmsFreeDefaultFn(cmsContext ContextID, void *Ptr)
+{
+ // free(NULL) is defined a no-op by C99, therefore it is safe to
+ // avoid the check, but it is here just in case...
+
+ if (Ptr) free(Ptr);
+
+ cmsUNUSED_PARAMETER(ContextID);
+}
+
+// The default realloc function. Again it checks for exploits. If Ptr is NULL,
+// realloc behaves the same way as malloc and allocates a new block of size bytes.
+static
+void* _cmsReallocDefaultFn(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
+{
+
+ if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never realloc over 512Mb
+
+ return realloc(Ptr, size);
+
+ cmsUNUSED_PARAMETER(ContextID);
+}
+
+
+// The default calloc function. Allocates an array of num elements, each one of size bytes
+// all memory is initialized to zero.
+static
+void* _cmsCallocDefaultFn(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
+{
+ cmsUInt32Number Total = num * size;
+
+ // Preserve calloc behaviour
+ if (Total == 0) return NULL;
+
+ // Safe check for overflow.
+ if (num >= UINT_MAX / size) return NULL;
+
+ // Check for overflow
+ if (Total < num || Total < size) {
+ return NULL;
+ }
+
+ if (Total > MAX_MEMORY_FOR_ALLOC) return NULL; // Never alloc over 512Mb
+
+ return _cmsMallocZero(ContextID, Total);
+}
+
+// Generic block duplication
+static
+void* _cmsDupDefaultFn(cmsContext ContextID, const void* Org, cmsUInt32Number size)
+{
+ void* mem;
+
+ if (size > MAX_MEMORY_FOR_ALLOC) return NULL; // Never dup over 512Mb
+
+ mem = _cmsMalloc(ContextID, size);
+
+ if (mem != NULL && Org != NULL)
+ memmove(mem, Org, size);
+
+ return mem;
+}
+
+
+// Pointers to memory manager functions in Context0
+_cmsMemPluginChunkType _cmsMemPluginChunk = { _cmsMallocDefaultFn, _cmsMallocZeroDefaultFn, _cmsFreeDefaultFn,
+ _cmsReallocDefaultFn, _cmsCallocDefaultFn, _cmsDupDefaultFn
+ };
+
+
+// Reset and duplicate memory manager
+void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
+{
+ _cmsAssert(ctx != NULL);
+
+ if (src != NULL) {
+
+ // Duplicate
+ ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
+ }
+ else {
+
+ // To reset it, we use the default allocators, which cannot be overriden
+ ctx ->chunks[MemPlugin] = &ctx ->DefaultMemoryManager;
+ }
+}
+
+// Auxiliar to fill memory management functions from plugin (or context 0 defaults)
+void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr)
+{
+ if (Plugin == NULL) {
+
+ memcpy(ptr, &_cmsMemPluginChunk, sizeof(_cmsMemPluginChunk));
+ }
+ else {
+
+ ptr ->MallocPtr = Plugin -> MallocPtr;
+ ptr ->FreePtr = Plugin -> FreePtr;
+ ptr ->ReallocPtr = Plugin -> ReallocPtr;
+
+ // Make sure we revert to defaults
+ ptr ->MallocZeroPtr= _cmsMallocZeroDefaultFn;
+ ptr ->CallocPtr = _cmsCallocDefaultFn;
+ ptr ->DupPtr = _cmsDupDefaultFn;
+
+ if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
+ if (Plugin ->CallocPtr != NULL) ptr ->CallocPtr = Plugin -> CallocPtr;
+ if (Plugin ->DupPtr != NULL) ptr ->DupPtr = Plugin -> DupPtr;
+
+ }
+}
+
+
+// Plug-in replacement entry
+cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase *Data)
+{
+ cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
+ _cmsMemPluginChunkType* ptr;
+
+ // NULL forces to reset to defaults. In this special case, the defaults are stored in the context structure.
+ // Remaining plug-ins does NOT have any copy in the context structure, but this is somehow special as the
+ // context internal data should be malloce'd by using those functions.
+ if (Data == NULL) {
+
+ struct _cmsContext_struct* ctx = ( struct _cmsContext_struct*) ContextID;
+
+ // Return to the default allocators
+ if (ContextID != NULL) {
+ ctx->chunks[MemPlugin] = (void*) &ctx->DefaultMemoryManager;
+ }
+ return TRUE;
+ }
+
+ // Check for required callbacks
+ if (Plugin -> MallocPtr == NULL ||
+ Plugin -> FreePtr == NULL ||
+ Plugin -> ReallocPtr == NULL) return FALSE;
+
+ // Set replacement functions
+ ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
+ if (ptr == NULL)
+ return FALSE;
+
+ _cmsInstallAllocFunctions(Plugin, ptr);
+ return TRUE;
+}
+#else
+#include "../../../../../include/fxcrt/fx_system.h"
+#include "../../../../../include/fxcrt/fx_memory.h"
+
+cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin)
+{
+ return TRUE;
+}
+
+// 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,
+ _cmsRealloc, _cmsCalloc, _cmsDupMem
+ };
+
+void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
+{
+ _cmsAssert(ctx != NULL);
+
+ if (src != NULL) {
+
+ // Duplicate
+ ctx ->chunks[MemPlugin] = _cmsSubAllocDup(ctx ->MemPool, src ->chunks[MemPlugin], sizeof(_cmsMemPluginChunkType));
+ }
+ else {
+
+ // To reset it, we use the default allocators, which cannot be overriden
+ ctx ->chunks[MemPlugin] = &ctx ->DefaultMemoryManager;
+ }
+}
+
+void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr)
+{
+ if (Plugin == NULL) {
+
+ memcpy(ptr, &_cmsMemPluginChunk, sizeof(_cmsMemPluginChunk));
+ }
+ else {
+
+ ptr ->MallocPtr = Plugin -> MallocPtr;
+ ptr ->FreePtr = Plugin -> FreePtr;
+ ptr ->ReallocPtr = Plugin -> ReallocPtr;
+
+ // Make sure we revert to defaults
+ ptr ->MallocZeroPtr= _cmsMallocZero;
+ ptr ->CallocPtr = _cmsCalloc;
+ ptr ->DupPtr = _cmsDupMem;
+
+ if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
+ if (Plugin ->CallocPtr != NULL) ptr ->CallocPtr = Plugin -> CallocPtr;
+ if (Plugin ->DupPtr != NULL) ptr ->DupPtr = Plugin -> DupPtr;
+
+ }
+}
+#endif
+
+// ********************************************************************************************
+
+// Sub allocation takes care of many pointers of small size. The memory allocated in
+// this way have be freed at once. Next function allocates a single chunk for linked list
+// I prefer this method over realloc due to the big inpact on xput realloc may have if
+// memory is being swapped to disk. This approach is safer (although that may not be true on all platforms)
+static
+_cmsSubAllocator_chunk* _cmsCreateSubAllocChunk(cmsContext ContextID, cmsUInt32Number Initial)
+{
+ _cmsSubAllocator_chunk* chunk;
+
+ // 20K by default
+ if (Initial == 0)
+ Initial = 20*1024;
+
+ // Create the container
+ chunk = (_cmsSubAllocator_chunk*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator_chunk));
+ if (chunk == NULL) return NULL;
+
+ // Initialize values
+ chunk ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, Initial);
+ if (chunk ->Block == NULL) {
+
+ // Something went wrong
+ _cmsFree(ContextID, chunk);
+ return NULL;
+ }
+
+ chunk ->BlockSize = Initial;
+ chunk ->Used = 0;
+ chunk ->next = NULL;
+
+ return chunk;
+}
+
+// The suballocated is nothing but a pointer to the first element in the list. We also keep
+// the thread ID in this structure.
+_cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial)
+{
+ _cmsSubAllocator* sub;
+
+ // Create the container
+ sub = (_cmsSubAllocator*) _cmsMallocZero(ContextID, sizeof(_cmsSubAllocator));
+ if (sub == NULL) return NULL;
+
+ sub ->ContextID = ContextID;
+
+ sub ->h = _cmsCreateSubAllocChunk(ContextID, Initial);
+ if (sub ->h == NULL) {
+ _cmsFree(ContextID, sub);
+ return NULL;
+ }
+
+ return sub;
+}
+
+
+// Get rid of whole linked list
+void _cmsSubAllocDestroy(_cmsSubAllocator* sub)
+{
+ _cmsSubAllocator_chunk *chunk, *n;
+
+ for (chunk = sub ->h; chunk != NULL; chunk = n) {
+
+ n = chunk->next;
+ if (chunk->Block != NULL) _cmsFree(sub ->ContextID, chunk->Block);
+ _cmsFree(sub ->ContextID, chunk);
+ }
+
+ // Free the header
+ _cmsFree(sub ->ContextID, sub);
+}
+
+
+// Get a pointer to small memory block.
+void* _cmsSubAlloc(_cmsSubAllocator* sub, cmsUInt32Number size)
+{
+ cmsUInt32Number Free = sub -> h ->BlockSize - sub -> h -> Used;
+ cmsUInt8Number* ptr;
+
+ size = _cmsALIGNMEM(size);
+
+ // Check for memory. If there is no room, allocate a new chunk of double memory size.
+ if (size > Free) {
+
+ _cmsSubAllocator_chunk* chunk;
+ cmsUInt32Number newSize;
+
+ newSize = sub -> h ->BlockSize * 2;
+ if (newSize < size) newSize = size;
+
+ chunk = _cmsCreateSubAllocChunk(sub -> ContextID, newSize);
+ if (chunk == NULL) return NULL;
+
+ // Link list
+ chunk ->next = sub ->h;
+ sub ->h = chunk;
+
+ }
+
+ ptr = sub -> h ->Block + sub -> h ->Used;
+ sub -> h -> Used += size;
+
+ return (void*) ptr;
+}
+
+// Duplicate in pool
+void* _cmsSubAllocDup(_cmsSubAllocator* s, const void *ptr, cmsUInt32Number size)
+{
+ void *NewPtr;
+
+ // Dup of null pointer is also NULL
+ if (ptr == NULL)
+ return NULL;
+
+ NewPtr = _cmsSubAlloc(s, size);
+
+ if (ptr != NULL && NewPtr != NULL) {
+ memcpy(NewPtr, ptr, size);
+ }
+
+ return NewPtr;
+}
+
+
+
+// Error logging ******************************************************************
+
+// There is no error handling at all. When a funtion fails, it returns proper value.
+// For example, all create functions does return NULL on failure. Other return FALSE
+// It may be interesting, for the developer, to know why the function is failing.
+// for that reason, lcms2 does offer a logging function. This function does recive
+// a ENGLISH string with some clues on what is going wrong. You can show this
+// info to the end user, or just create some sort of log.
+// The logging function should NOT terminate the program, as this obviously can leave
+// resources. It is the programmer's responsability to check each function return code
+// to make sure it didn't fail.
+
+// Error messages are limited to MAX_ERROR_MESSAGE_LEN
+
+#define MAX_ERROR_MESSAGE_LEN 1024
+
+// ---------------------------------------------------------------------------------------------------------
+
+// This is our default log error
+static void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text);
+
+// Context0 storage, which is global
+_cmsLogErrorChunkType _cmsLogErrorChunk = { DefaultLogErrorHandlerFunction };
+
+// Allocates and inits error logger container for a given context. If src is NULL, only initializes the value
+// to the default. Otherwise, it duplicates the value. The interface is standard across all context clients
+void _cmsAllocLogErrorChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ static _cmsLogErrorChunkType LogErrorChunk = { DefaultLogErrorHandlerFunction };
+ void* from;
+
+ if (src != NULL) {
+ from = src ->chunks[Logger];
+ }
+ else {
+ from = &LogErrorChunk;
+ }
+
+ ctx ->chunks[Logger] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsLogErrorChunkType));
+}
+
+// The default error logger does nothing.
+static
+void DefaultLogErrorHandlerFunction(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *Text)
+{
+ // fprintf(stderr, "[lcms]: %s\n", Text);
+ // fflush(stderr);
+
+ cmsUNUSED_PARAMETER(ContextID);
+ cmsUNUSED_PARAMETER(ErrorCode);
+ cmsUNUSED_PARAMETER(Text);
+}
+
+// Change log error, context based
+void CMSEXPORT cmsSetLogErrorHandlerTHR(cmsContext ContextID, cmsLogErrorHandlerFunction Fn)
+{
+ _cmsLogErrorChunkType* lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
+
+ if (lhg != NULL) {
+
+ if (Fn == NULL)
+ lhg -> LogErrorHandler = DefaultLogErrorHandlerFunction;
+ else
+ lhg -> LogErrorHandler = Fn;
+ }
+}
+
+// Change log error, legacy
+void CMSEXPORT cmsSetLogErrorHandler(cmsLogErrorHandlerFunction Fn)
+{
+ cmsSetLogErrorHandlerTHR(NULL, Fn);
+}
+
+// Log an error
+// ErrorText is a text holding an english description of error.
+void CMSEXPORT cmsSignalError(cmsContext ContextID, cmsUInt32Number ErrorCode, const char *ErrorText, ...)
+{
+ va_list args;
+ char Buffer[MAX_ERROR_MESSAGE_LEN];
+ _cmsLogErrorChunkType* lhg;
+
+
+ va_start(args, ErrorText);
+ vsnprintf(Buffer, MAX_ERROR_MESSAGE_LEN-1, ErrorText, args);
+ va_end(args);
+
+ // Check for the context, if specified go there. If not, go for the global
+ lhg = (_cmsLogErrorChunkType*) _cmsContextGetClientChunk(ContextID, Logger);
+ if (lhg ->LogErrorHandler) {
+ lhg ->LogErrorHandler(ContextID, ErrorCode, Buffer);
+ }
+}
+
+// Utility function to print signatures
+void _cmsTagSignature2String(char String[5], cmsTagSignature sig)
+{
+ cmsUInt32Number be;
+
+ // Convert to big endian
+ be = _cmsAdjustEndianess32((cmsUInt32Number) sig);
+
+ // Move chars
+ memmove(String, &be, 4);
+
+ // Make sure of terminator
+ String[4] = 0;
+}
+
+//--------------------------------------------------------------------------------------------------
+
+
+static
+void* defMtxCreate(cmsContext id)
+{
+ _cmsMutex* ptr_mutex = (_cmsMutex*) _cmsMalloc(id, sizeof(_cmsMutex));
+ _cmsInitMutexPrimitive(ptr_mutex);
+ return (void*) ptr_mutex;
+}
+
+static
+void defMtxDestroy(cmsContext id, void* mtx)
+{
+ _cmsDestroyMutexPrimitive((_cmsMutex *) mtx);
+ _cmsFree(id, mtx);
+}
+
+static
+cmsBool defMtxLock(cmsContext id, void* mtx)
+{
+ cmsUNUSED_PARAMETER(id);
+ return _cmsLockPrimitive((_cmsMutex *) mtx) == 0;
+}
+
+static
+void defMtxUnlock(cmsContext id, void* mtx)
+{
+ cmsUNUSED_PARAMETER(id);
+ _cmsUnlockPrimitive((_cmsMutex *) mtx);
+}
+
+
+
+// Pointers to memory manager functions in Context0
+_cmsMutexPluginChunkType _cmsMutexPluginChunk = { defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
+
+// Allocate and init mutex container.
+void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ static _cmsMutexPluginChunkType MutexChunk = {defMtxCreate, defMtxDestroy, defMtxLock, defMtxUnlock };
+ void* from;
+
+ if (src != NULL) {
+ from = src ->chunks[MutexPlugin];
+ }
+ else {
+ from = &MutexChunk;
+ }
+
+ ctx ->chunks[MutexPlugin] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsMutexPluginChunkType));
+}
+
+// Register new ways to transform
+cmsBool _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ cmsPluginMutex* Plugin = (cmsPluginMutex*) Data;
+ _cmsMutexPluginChunkType* ctx = ( _cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
+
+ if (Data == NULL) {
+
+ // No lock routines
+ ctx->CreateMutexPtr = NULL;
+ ctx->DestroyMutexPtr = NULL;
+ ctx->LockMutexPtr = NULL;
+ ctx ->UnlockMutexPtr = NULL;
+ return TRUE;
+ }
+
+ // Factory callback is required
+ if (Plugin ->CreateMutexPtr == NULL || Plugin ->DestroyMutexPtr == NULL ||
+ Plugin ->LockMutexPtr == NULL || Plugin ->UnlockMutexPtr == NULL) return FALSE;
+
+
+ ctx->CreateMutexPtr = Plugin->CreateMutexPtr;
+ ctx->DestroyMutexPtr = Plugin ->DestroyMutexPtr;
+ ctx ->LockMutexPtr = Plugin ->LockMutexPtr;
+ ctx ->UnlockMutexPtr = Plugin ->UnlockMutexPtr;
+
+ // All is ok
+ return TRUE;
+}
+
+// Generic Mutex fns
+void* CMSEXPORT _cmsCreateMutex(cmsContext ContextID)
+{
+ _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
+
+ if (ptr ->CreateMutexPtr == NULL) return NULL;
+
+ return ptr ->CreateMutexPtr(ContextID);
+}
+
+void CMSEXPORT _cmsDestroyMutex(cmsContext ContextID, void* mtx)
+{
+ _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
+
+ if (ptr ->DestroyMutexPtr != NULL) {
+
+ ptr ->DestroyMutexPtr(ContextID, mtx);
+ }
+}
+
+cmsBool CMSEXPORT _cmsLockMutex(cmsContext ContextID, void* mtx)
+{
+ _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
+
+ if (ptr ->LockMutexPtr == NULL) return TRUE;
+
+ return ptr ->LockMutexPtr(ContextID, mtx);
+}
+
+void CMSEXPORT _cmsUnlockMutex(cmsContext ContextID, void* mtx)
+{
+ _cmsMutexPluginChunkType* ptr = (_cmsMutexPluginChunkType*) _cmsContextGetClientChunk(ContextID, MutexPlugin);
+
+ if (ptr ->UnlockMutexPtr != NULL) {
+
+ ptr ->UnlockMutexPtr(ContextID, mtx);
+ }
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgamma.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgamma.c
new file mode 100644
index 0000000000..d5aa87b105
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgamma.c
@@ -0,0 +1,1298 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2013 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"
+
+// Tone curves are powerful constructs that can contain curves specified in diverse ways.
+// The curve is stored in segments, where each segment can be sampled or specified by parameters.
+// a 16.bit simplification of the *whole* curve is kept for optimization purposes. For float operation,
+// each segment is evaluated separately. Plug-ins may be used to define new parametric schemes,
+// each plug-in may define up to MAX_TYPES_IN_LCMS_PLUGIN functions types. For defining a function,
+// the plug-in should provide the type id, how many parameters each type has, and a pointer to
+// a procedure that evaluates the function. In the case of reverse evaluation, the evaluator will
+// be called with the type id as a negative value, and a sampled version of the reversed curve
+// will be built.
+
+// ----------------------------------------------------------------- Implementation
+// Maxim number of nodes
+#define MAX_NODES_IN_CURVE 4097
+#define MINUS_INF (-1E22F)
+#define PLUS_INF (+1E22F)
+
+// The list of supported parametric curves
+typedef struct _cmsParametricCurvesCollection_st {
+
+ int nFunctions; // Number of supported functions in this chunk
+ int FunctionTypes[MAX_TYPES_IN_LCMS_PLUGIN]; // The identification types
+ int ParameterCount[MAX_TYPES_IN_LCMS_PLUGIN]; // Number of parameters for each function
+ cmsParametricCurveEvaluator Evaluator; // The evaluator
+
+ struct _cmsParametricCurvesCollection_st* Next; // Next in list
+
+} _cmsParametricCurvesCollection;
+
+// This is the default (built-in) evaluator
+static cmsFloat64Number DefaultEvalParametricFn(cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R);
+
+// The built-in list
+static _cmsParametricCurvesCollection DefaultCurves = {
+ 9, // # of curve types
+ { 1, 2, 3, 4, 5, 6, 7, 8, 108 }, // Parametric curve ID
+ { 1, 3, 4, 5, 7, 4, 5, 5, 1 }, // Parameters by type
+ DefaultEvalParametricFn, // Evaluator
+ NULL // Next in chain
+};
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupPluginCurvesList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsCurvesPluginChunkType newHead = { NULL };
+ _cmsParametricCurvesCollection* entry;
+ _cmsParametricCurvesCollection* Anterior = NULL;
+ _cmsCurvesPluginChunkType* head = (_cmsCurvesPluginChunkType*) src->chunks[CurvesPlugin];
+
+ _cmsAssert(head != NULL);
+
+ // Walk the list copying all nodes
+ for (entry = head->ParametricCurves;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ _cmsParametricCurvesCollection *newEntry = ( _cmsParametricCurvesCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsParametricCurvesCollection));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.ParametricCurves == NULL)
+ newHead.ParametricCurves = newEntry;
+ }
+
+ ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsCurvesPluginChunkType));
+}
+
+// The allocator have to follow the chain
+void _cmsAllocCurvesPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsAssert(ctx != NULL);
+
+ if (src != NULL) {
+
+ // Copy all linked list
+ DupPluginCurvesList(ctx, src);
+ }
+ else {
+ static _cmsCurvesPluginChunkType CurvesPluginChunk = { NULL };
+ ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx ->MemPool, &CurvesPluginChunk, sizeof(_cmsCurvesPluginChunkType));
+ }
+}
+
+
+// The linked list head
+_cmsCurvesPluginChunkType _cmsCurvesPluginChunk = { NULL };
+
+// As a way to install new parametric curves
+cmsBool _cmsRegisterParametricCurvesPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin);
+ cmsPluginParametricCurves* Plugin = (cmsPluginParametricCurves*) Data;
+ _cmsParametricCurvesCollection* fl;
+
+ if (Data == NULL) {
+
+ ctx -> ParametricCurves = NULL;
+ return TRUE;
+ }
+
+ fl = (_cmsParametricCurvesCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsParametricCurvesCollection));
+ if (fl == NULL) return FALSE;
+
+ // Copy the parameters
+ fl ->Evaluator = Plugin ->Evaluator;
+ fl ->nFunctions = Plugin ->nFunctions;
+
+ // Make sure no mem overwrites
+ if (fl ->nFunctions > MAX_TYPES_IN_LCMS_PLUGIN)
+ fl ->nFunctions = MAX_TYPES_IN_LCMS_PLUGIN;
+
+ // Copy the data
+ memmove(fl->FunctionTypes, Plugin ->FunctionTypes, fl->nFunctions * sizeof(cmsUInt32Number));
+ memmove(fl->ParameterCount, Plugin ->ParameterCount, fl->nFunctions * sizeof(cmsUInt32Number));
+
+ // Keep linked list
+ fl ->Next = ctx->ParametricCurves;
+ ctx->ParametricCurves = fl;
+
+ // All is ok
+ return TRUE;
+}
+
+
+// Search in type list, return position or -1 if not found
+static
+int IsInSet(int Type, _cmsParametricCurvesCollection* c)
+{
+ int i;
+
+ for (i=0; i < c ->nFunctions; i++)
+ if (abs(Type) == c ->FunctionTypes[i]) return i;
+
+ return -1;
+}
+
+
+// Search for the collection which contains a specific type
+static
+_cmsParametricCurvesCollection *GetParametricCurveByType(cmsContext ContextID, int Type, int* index)
+{
+ _cmsParametricCurvesCollection* c;
+ int Position;
+ _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin);
+
+ for (c = ctx->ParametricCurves; c != NULL; c = c ->Next) {
+
+ Position = IsInSet(Type, c);
+
+ if (Position != -1) {
+ if (index != NULL)
+ *index = Position;
+ return c;
+ }
+ }
+ // If none found, revert for defaults
+ for (c = &DefaultCurves; c != NULL; c = c ->Next) {
+
+ Position = IsInSet(Type, c);
+
+ if (Position != -1) {
+ if (index != NULL)
+ *index = Position;
+ return c;
+ }
+ }
+
+ return NULL;
+}
+
+// Low level allocate, which takes care of memory details. nEntries may be zero, and in this case
+// no optimation curve is computed. nSegments may also be zero in the inverse case, where only the
+// optimization curve is given. Both features simultaneously is an error
+static
+cmsToneCurve* AllocateToneCurveStruct(cmsContext ContextID, cmsInt32Number nEntries,
+ cmsInt32Number nSegments, const cmsCurveSegment* Segments,
+ const cmsUInt16Number* Values)
+{
+ cmsToneCurve* p;
+ int i;
+
+ // We allow huge tables, which are then restricted for smoothing operations
+ if (nEntries > 65530 || nEntries < 0) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't create tone curve of more than 65530 entries");
+ return NULL;
+ }
+
+ if (nEntries <= 0 && nSegments <= 0) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't create tone curve with zero segments and no table");
+ return NULL;
+ }
+
+ // Allocate all required pointers, etc.
+ p = (cmsToneCurve*) _cmsMallocZero(ContextID, sizeof(cmsToneCurve));
+ if (!p) return NULL;
+
+ // In this case, there are no segments
+ if (nSegments <= 0) {
+ p ->Segments = NULL;
+ p ->Evals = NULL;
+ }
+ else {
+ p ->Segments = (cmsCurveSegment*) _cmsCalloc(ContextID, nSegments, sizeof(cmsCurveSegment));
+ if (p ->Segments == NULL) goto Error;
+
+ p ->Evals = (cmsParametricCurveEvaluator*) _cmsCalloc(ContextID, nSegments, sizeof(cmsParametricCurveEvaluator));
+ if (p ->Evals == NULL) goto Error;
+ }
+
+ p -> nSegments = nSegments;
+
+ // This 16-bit table contains a limited precision representation of the whole curve and is kept for
+ // increasing xput on certain operations.
+ if (nEntries <= 0) {
+ p ->Table16 = NULL;
+ }
+ else {
+ p ->Table16 = (cmsUInt16Number*) _cmsCalloc(ContextID, nEntries, sizeof(cmsUInt16Number));
+ if (p ->Table16 == NULL) goto Error;
+ }
+
+ p -> nEntries = nEntries;
+
+ // Initialize members if requested
+ if (Values != NULL && (nEntries > 0)) {
+
+ for (i=0; i < nEntries; i++)
+ p ->Table16[i] = Values[i];
+ }
+
+ // Initialize the segments stuff. The evaluator for each segment is located and a pointer to it
+ // is placed in advance to maximize performance.
+ if (Segments != NULL && (nSegments > 0)) {
+
+ _cmsParametricCurvesCollection *c;
+
+ p ->SegInterp = (cmsInterpParams**) _cmsCalloc(ContextID, nSegments, sizeof(cmsInterpParams*));
+ if (p ->SegInterp == NULL) goto Error;
+
+ for (i=0; i< nSegments; i++) {
+
+ // Type 0 is a special marker for table-based curves
+ if (Segments[i].Type == 0)
+ p ->SegInterp[i] = _cmsComputeInterpParams(ContextID, Segments[i].nGridPoints, 1, 1, NULL, CMS_LERP_FLAGS_FLOAT);
+
+ memmove(&p ->Segments[i], &Segments[i], sizeof(cmsCurveSegment));
+
+ if (Segments[i].Type == 0 && Segments[i].SampledPoints != NULL)
+ p ->Segments[i].SampledPoints = (cmsFloat32Number*) _cmsDupMem(ContextID, Segments[i].SampledPoints, sizeof(cmsFloat32Number) * Segments[i].nGridPoints);
+ else
+ p ->Segments[i].SampledPoints = NULL;
+
+
+ c = GetParametricCurveByType(ContextID, Segments[i].Type, NULL);
+ if (c != NULL)
+ p ->Evals[i] = c ->Evaluator;
+ }
+ }
+
+ p ->InterpParams = _cmsComputeInterpParams(ContextID, p ->nEntries, 1, 1, p->Table16, CMS_LERP_FLAGS_16BITS);
+ if (p->InterpParams != NULL)
+ return p;
+
+Error:
+ if (p -> Segments) _cmsFree(ContextID, p ->Segments);
+ if (p -> Evals) _cmsFree(ContextID, p -> Evals);
+ if (p ->Table16) _cmsFree(ContextID, p ->Table16);
+ _cmsFree(ContextID, p);
+ return NULL;
+}
+
+
+// Parametric Fn using floating point
+static
+cmsFloat64Number DefaultEvalParametricFn(cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R)
+{
+ cmsFloat64Number e, Val, disc;
+
+ switch (Type) {
+
+ // X = Y ^ Gamma
+ case 1:
+ if (R < 0) {
+
+ if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE)
+ Val = R;
+ else
+ Val = 0;
+ }
+ else
+ Val = pow(R, Params[0]);
+ break;
+
+ // Type 1 Reversed: X = Y ^1/gamma
+ case -1:
+ if (R < 0) {
+
+ if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE)
+ Val = R;
+ else
+ Val = 0;
+ }
+ else
+ Val = pow(R, 1/Params[0]);
+ break;
+
+ // CIE 122-1966
+ // Y = (aX + b)^Gamma | X >= -b/a
+ // Y = 0 | else
+ case 2:
+ disc = -Params[2] / Params[1];
+
+ if (R >= disc ) {
+
+ e = Params[1]*R + Params[2];
+
+ if (e > 0)
+ Val = pow(e, Params[0]);
+ else
+ Val = 0;
+ }
+ else
+ Val = 0;
+ break;
+
+ // Type 2 Reversed
+ // X = (Y ^1/g - b) / a
+ case -2:
+ if (R < 0)
+ Val = 0;
+ else
+ Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
+
+ if (Val < 0)
+ Val = 0;
+ break;
+
+
+ // IEC 61966-3
+ // Y = (aX + b)^Gamma | X <= -b/a
+ // Y = c | else
+ case 3:
+ disc = -Params[2] / Params[1];
+ if (disc < 0)
+ disc = 0;
+
+ if (R >= disc) {
+
+ e = Params[1]*R + Params[2];
+
+ if (e > 0)
+ Val = pow(e, Params[0]) + Params[3];
+ else
+ Val = 0;
+ }
+ else
+ Val = Params[3];
+ break;
+
+
+ // Type 3 reversed
+ // X=((Y-c)^1/g - b)/a | (Y>=c)
+ // X=-b/a | (Y<c)
+ case -3:
+ if (R >= Params[3]) {
+
+ e = R - Params[3];
+
+ if (e > 0)
+ Val = (pow(e, 1/Params[0]) - Params[2]) / Params[1];
+ else
+ Val = 0;
+ }
+ else {
+ Val = -Params[2] / Params[1];
+ }
+ break;
+
+
+ // IEC 61966-2.1 (sRGB)
+ // Y = (aX + b)^Gamma | X >= d
+ // Y = cX | X < d
+ case 4:
+ if (R >= Params[4]) {
+
+ e = Params[1]*R + Params[2];
+
+ if (e > 0)
+ Val = pow(e, Params[0]);
+ else
+ Val = 0;
+ }
+ else
+ Val = R * Params[3];
+ break;
+
+ // Type 4 reversed
+ // X=((Y^1/g-b)/a) | Y >= (ad+b)^g
+ // X=Y/c | Y< (ad+b)^g
+ case -4:
+ e = Params[1] * Params[4] + Params[2];
+ if (e < 0)
+ disc = 0;
+ else
+ disc = pow(e, Params[0]);
+
+ if (R >= disc) {
+
+ Val = (pow(R, 1.0/Params[0]) - Params[2]) / Params[1];
+ }
+ else {
+ Val = R / Params[3];
+ }
+ break;
+
+
+ // Y = (aX + b)^Gamma + e | X >= d
+ // Y = cX + f | X < d
+ case 5:
+ if (R >= Params[4]) {
+
+ e = Params[1]*R + Params[2];
+
+ if (e > 0)
+ Val = pow(e, Params[0]) + Params[5];
+ else
+ Val = Params[5];
+ }
+ else
+ Val = R*Params[3] + Params[6];
+ break;
+
+
+ // Reversed type 5
+ // X=((Y-e)1/g-b)/a | Y >=(ad+b)^g+e), cd+f
+ // X=(Y-f)/c | else
+ case -5:
+
+ disc = Params[3] * Params[4] + Params[6];
+ if (R >= disc) {
+
+ e = R - Params[5];
+ if (e < 0)
+ Val = 0;
+ else
+ Val = (pow(e, 1.0/Params[0]) - Params[2]) / Params[1];
+ }
+ else {
+ Val = (R - Params[6]) / Params[3];
+ }
+ break;
+
+
+ // Types 6,7,8 comes from segmented curves as described in ICCSpecRevision_02_11_06_Float.pdf
+ // Type 6 is basically identical to type 5 without d
+
+ // Y = (a * X + b) ^ Gamma + c
+ case 6:
+ e = Params[1]*R + Params[2];
+
+ if (e < 0)
+ Val = Params[3];
+ else
+ Val = pow(e, Params[0]) + Params[3];
+ break;
+
+ // ((Y - c) ^1/Gamma - b) / a
+ case -6:
+ e = R - Params[3];
+ if (e < 0)
+ Val = 0;
+ else
+ Val = (pow(e, 1.0/Params[0]) - Params[2]) / Params[1];
+ break;
+
+
+ // Y = a * log (b * X^Gamma + c) + d
+ case 7:
+
+ e = Params[2] * pow(R, Params[0]) + Params[3];
+ if (e <= 0)
+ Val = Params[4];
+ else
+ Val = Params[1]*log10(e) + Params[4];
+ break;
+
+ // (Y - d) / a = log(b * X ^Gamma + c)
+ // pow(10, (Y-d) / a) = b * X ^Gamma + c
+ // pow((pow(10, (Y-d) / a) - c) / b, 1/g) = X
+ case -7:
+ Val = pow((pow(10.0, (R-Params[4]) / Params[1]) - Params[3]) / Params[2], 1.0 / Params[0]);
+ break;
+
+
+ //Y = a * b^(c*X+d) + e
+ case 8:
+ Val = (Params[0] * pow(Params[1], Params[2] * R + Params[3]) + Params[4]);
+ break;
+
+
+ // Y = (log((y-e) / a) / log(b) - d ) / c
+ // a=0, b=1, c=2, d=3, e=4,
+ case -8:
+
+ disc = R - Params[4];
+ if (disc < 0) Val = 0;
+ else
+ Val = (log(disc / Params[0]) / log(Params[1]) - Params[3]) / Params[2];
+ break;
+
+ // S-Shaped: (1 - (1-x)^1/g)^1/g
+ case 108:
+ Val = pow(1.0 - pow(1 - R, 1/Params[0]), 1/Params[0]);
+ break;
+
+ // y = (1 - (1-x)^1/g)^1/g
+ // y^g = (1 - (1-x)^1/g)
+ // 1 - y^g = (1-x)^1/g
+ // (1 - y^g)^g = 1 - x
+ // 1 - (1 - y^g)^g
+ case -108:
+ Val = 1 - pow(1 - pow(R, Params[0]), Params[0]);
+ break;
+
+ default:
+ // Unsupported parametric curve. Should never reach here
+ return 0;
+ }
+
+ return Val;
+}
+
+// Evaluate a segmented funtion for a single value. Return -1 if no valid segment found .
+// If fn type is 0, perform an interpolation on the table
+static
+cmsFloat64Number EvalSegmentedFn(const cmsToneCurve *g, cmsFloat64Number R)
+{
+ int i;
+
+ for (i = g ->nSegments-1; i >= 0 ; --i) {
+
+ // Check for domain
+ if ((R > g ->Segments[i].x0) && (R <= g ->Segments[i].x1)) {
+
+ // Type == 0 means segment is sampled
+ if (g ->Segments[i].Type == 0) {
+
+ cmsFloat32Number R1 = (cmsFloat32Number) (R - g ->Segments[i].x0) / (g ->Segments[i].x1 - g ->Segments[i].x0);
+ cmsFloat32Number Out;
+
+ // Setup the table (TODO: clean that)
+ g ->SegInterp[i]-> Table = g ->Segments[i].SampledPoints;
+
+ g ->SegInterp[i] -> Interpolation.LerpFloat(&R1, &Out, g ->SegInterp[i]);
+
+ return Out;
+ }
+ else
+ return g ->Evals[i](g->Segments[i].Type, g ->Segments[i].Params, R);
+ }
+ }
+
+ return MINUS_INF;
+}
+
+// Access to estimated low-res table
+cmsUInt32Number CMSEXPORT cmsGetToneCurveEstimatedTableEntries(const cmsToneCurve* t)
+{
+ _cmsAssert(t != NULL);
+ return t ->nEntries;
+}
+
+const cmsUInt16Number* CMSEXPORT cmsGetToneCurveEstimatedTable(const cmsToneCurve* t)
+{
+ _cmsAssert(t != NULL);
+ return t ->Table16;
+}
+
+
+// Create an empty gamma curve, by using tables. This specifies only the limited-precision part, and leaves the
+// floating point description empty.
+cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurve16(cmsContext ContextID, cmsInt32Number nEntries, const cmsUInt16Number Values[])
+{
+ return AllocateToneCurveStruct(ContextID, nEntries, 0, NULL, Values);
+}
+
+static
+int EntriesByGamma(cmsFloat64Number Gamma)
+{
+ if (fabs(Gamma - 1.0) < 0.001) return 2;
+ return 4096;
+}
+
+
+// Create a segmented gamma, fill the table
+cmsToneCurve* CMSEXPORT cmsBuildSegmentedToneCurve(cmsContext ContextID,
+ cmsInt32Number nSegments, const cmsCurveSegment Segments[])
+{
+ int i;
+ cmsFloat64Number R, Val;
+ cmsToneCurve* g;
+ int nGridPoints = 4096;
+
+ _cmsAssert(Segments != NULL);
+
+ // Optimizatin for identity curves.
+ if (nSegments == 1 && Segments[0].Type == 1) {
+
+ nGridPoints = EntriesByGamma(Segments[0].Params[0]);
+ }
+
+ g = AllocateToneCurveStruct(ContextID, nGridPoints, nSegments, Segments, NULL);
+ if (g == NULL) return NULL;
+
+ // Once we have the floating point version, we can approximate a 16 bit table of 4096 entries
+ // for performance reasons. This table would normally not be used except on 8/16 bits transforms.
+ for (i=0; i < nGridPoints; i++) {
+
+ R = (cmsFloat64Number) i / (nGridPoints-1);
+
+ Val = EvalSegmentedFn(g, R);
+
+ // Round and saturate
+ g ->Table16[i] = _cmsQuickSaturateWord(Val * 65535.0);
+ }
+
+ return g;
+}
+
+// Use a segmented curve to store the floating point table
+cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurveFloat(cmsContext ContextID, cmsUInt32Number nEntries, const cmsFloat32Number values[])
+{
+ cmsCurveSegment Seg[3];
+
+ // A segmented tone curve should have function segments in the first and last positions
+ // Initialize segmented curve part up to 0 to constant value = samples[0]
+ Seg[0].x0 = MINUS_INF;
+ Seg[0].x1 = 0;
+ Seg[0].Type = 6;
+
+ Seg[0].Params[0] = 1;
+ Seg[0].Params[1] = 0;
+ Seg[0].Params[2] = 0;
+ Seg[0].Params[3] = values[0];
+ Seg[0].Params[4] = 0;
+
+ // From zero to 1
+ Seg[1].x0 = 0;
+ Seg[1].x1 = 1.0;
+ Seg[1].Type = 0;
+
+ Seg[1].nGridPoints = nEntries;
+ Seg[1].SampledPoints = (cmsFloat32Number*) values;
+
+ // Final segment is constant = lastsample
+ Seg[2].x0 = 1.0;
+ Seg[2].x1 = PLUS_INF;
+ Seg[2].Type = 6;
+
+ Seg[2].Params[0] = 1;
+ Seg[2].Params[1] = 0;
+ Seg[2].Params[2] = 0;
+ Seg[2].Params[3] = values[nEntries-1];
+ Seg[2].Params[4] = 0;
+
+
+ return cmsBuildSegmentedToneCurve(ContextID, 3, Seg);
+}
+
+// Parametric curves
+//
+// Parameters goes as: Curve, a, b, c, d, e, f
+// Type is the ICC type +1
+// if type is negative, then the curve is analyticaly inverted
+cmsToneCurve* CMSEXPORT cmsBuildParametricToneCurve(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[])
+{
+ cmsCurveSegment Seg0;
+ int Pos = 0;
+ cmsUInt32Number size;
+ _cmsParametricCurvesCollection* c = GetParametricCurveByType(ContextID, Type, &Pos);
+
+ _cmsAssert(Params != NULL);
+
+ if (c == NULL) {
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Invalid parametric curve type %d", Type);
+ return NULL;
+ }
+
+ memset(&Seg0, 0, sizeof(Seg0));
+
+ Seg0.x0 = MINUS_INF;
+ Seg0.x1 = PLUS_INF;
+ Seg0.Type = Type;
+
+ size = c->ParameterCount[Pos] * sizeof(cmsFloat64Number);
+ memmove(Seg0.Params, Params, size);
+
+ return cmsBuildSegmentedToneCurve(ContextID, 1, &Seg0);
+}
+
+
+
+// Build a gamma table based on gamma constant
+cmsToneCurve* CMSEXPORT cmsBuildGamma(cmsContext ContextID, cmsFloat64Number Gamma)
+{
+ return cmsBuildParametricToneCurve(ContextID, 1, &Gamma);
+}
+
+
+// Free all memory taken by the gamma curve
+void CMSEXPORT cmsFreeToneCurve(cmsToneCurve* Curve)
+{
+ cmsContext ContextID;
+
+ // added by Xiaochuan Liu
+ // Curve->InterpParams may be null
+ if (Curve == NULL || Curve->InterpParams == NULL) return;
+
+ ContextID = Curve ->InterpParams->ContextID;
+
+ _cmsFreeInterpParams(Curve ->InterpParams);
+ Curve ->InterpParams = NULL;
+
+ if (Curve -> Table16)
+ {
+ _cmsFree(ContextID, Curve ->Table16);
+ Curve ->Table16 = NULL;
+ }
+
+ if (Curve ->Segments) {
+
+ cmsUInt32Number i;
+
+ for (i=0; i < Curve ->nSegments; i++) {
+
+ if (Curve ->Segments[i].SampledPoints) {
+ _cmsFree(ContextID, Curve ->Segments[i].SampledPoints);
+ Curve ->Segments[i].SampledPoints = NULL;
+ }
+
+ if (Curve ->SegInterp[i] != 0)
+ {
+ _cmsFreeInterpParams(Curve->SegInterp[i]);
+ Curve->SegInterp[i] = NULL;
+ }
+ }
+
+ _cmsFree(ContextID, Curve ->Segments);
+ Curve ->Segments = NULL;
+ _cmsFree(ContextID, Curve ->SegInterp);
+ Curve ->SegInterp = NULL;
+ }
+
+ if (Curve -> Evals)
+ {
+ _cmsFree(ContextID, Curve -> Evals);
+ Curve -> Evals = NULL;
+ }
+
+ if (Curve)
+ {
+ _cmsFree(ContextID, Curve);
+ Curve = NULL;
+ }
+}
+
+// Utility function, free 3 gamma tables
+void CMSEXPORT cmsFreeToneCurveTriple(cmsToneCurve* Curve[3])
+{
+
+ _cmsAssert(Curve != NULL);
+
+ if (Curve[0] != NULL) cmsFreeToneCurve(Curve[0]);
+ if (Curve[1] != NULL) cmsFreeToneCurve(Curve[1]);
+ if (Curve[2] != NULL) cmsFreeToneCurve(Curve[2]);
+
+ Curve[0] = Curve[1] = Curve[2] = NULL;
+}
+
+
+// Duplicate a gamma table
+cmsToneCurve* CMSEXPORT cmsDupToneCurve(const cmsToneCurve* In)
+{
+ // Xiaochuan Liu
+ // fix openpdf bug(mantis id:0055683, google id:360198)
+ // the function CurveSetElemTypeFree in cmslut.c also needs to check pointer
+ if (In == NULL || In ->InterpParams == NULL) return NULL;
+
+ return AllocateToneCurveStruct(In ->InterpParams ->ContextID, In ->nEntries, In ->nSegments, In ->Segments, In ->Table16);
+}
+
+// Joins two curves for X and Y. Curves should be monotonic.
+// We want to get
+//
+// y = Y^-1(X(t))
+//
+cmsToneCurve* CMSEXPORT cmsJoinToneCurve(cmsContext ContextID,
+ const cmsToneCurve* X,
+ const cmsToneCurve* Y, cmsUInt32Number nResultingPoints)
+{
+ cmsToneCurve* out = NULL;
+ cmsToneCurve* Yreversed = NULL;
+ cmsFloat32Number t, x;
+ cmsFloat32Number* Res = NULL;
+ cmsUInt32Number i;
+
+
+ _cmsAssert(X != NULL);
+ _cmsAssert(Y != NULL);
+
+ Yreversed = cmsReverseToneCurveEx(nResultingPoints, Y);
+ if (Yreversed == NULL) goto Error;
+
+ Res = (cmsFloat32Number*) _cmsCalloc(ContextID, nResultingPoints, sizeof(cmsFloat32Number));
+ if (Res == NULL) goto Error;
+
+ //Iterate
+ for (i=0; i < nResultingPoints; i++) {
+
+ t = (cmsFloat32Number) i / (nResultingPoints-1);
+ x = cmsEvalToneCurveFloat(X, t);
+ Res[i] = cmsEvalToneCurveFloat(Yreversed, x);
+ }
+
+ // Allocate space for output
+ out = cmsBuildTabulatedToneCurveFloat(ContextID, nResultingPoints, Res);
+
+Error:
+
+ if (Res != NULL) _cmsFree(ContextID, Res);
+ if (Yreversed != NULL) cmsFreeToneCurve(Yreversed);
+
+ return out;
+}
+
+
+
+// Get the surrounding nodes. This is tricky on non-monotonic tables
+static
+int GetInterval(cmsFloat64Number In, const cmsUInt16Number LutTable[], const struct _cms_interp_struc* p)
+{
+ int i;
+ int y0, y1;
+
+ // A 1 point table is not allowed
+ if (p -> Domain[0] < 1) return -1;
+
+ // Let's see if ascending or descending.
+ if (LutTable[0] < LutTable[p ->Domain[0]]) {
+
+ // Table is overall ascending
+ for (i=p->Domain[0]-1; i >=0; --i) {
+
+ y0 = LutTable[i];
+ y1 = LutTable[i+1];
+
+ if (y0 <= y1) { // Increasing
+ if (In >= y0 && In <= y1) return i;
+ }
+ else
+ if (y1 < y0) { // Decreasing
+ if (In >= y1 && In <= y0) return i;
+ }
+ }
+ }
+ else {
+ // Table is overall descending
+ for (i=0; i < (int) p -> Domain[0]; i++) {
+
+ y0 = LutTable[i];
+ y1 = LutTable[i+1];
+
+ if (y0 <= y1) { // Increasing
+ if (In >= y0 && In <= y1) return i;
+ }
+ else
+ if (y1 < y0) { // Decreasing
+ if (In >= y1 && In <= y0) return i;
+ }
+ }
+ }
+
+ return -1;
+}
+
+// Reverse a gamma table
+cmsToneCurve* CMSEXPORT cmsReverseToneCurveEx(cmsInt32Number nResultSamples, const cmsToneCurve* InCurve)
+{
+ cmsToneCurve *out;
+ cmsFloat64Number a = 0, b = 0, y, x1, y1, x2, y2;
+ int i, j;
+ int Ascending;
+
+ _cmsAssert(InCurve != NULL);
+
+ // Try to reverse it analytically whatever possible
+
+ if (InCurve ->nSegments == 1 && InCurve ->Segments[0].Type > 0 &&
+ /* InCurve -> Segments[0].Type <= 5 */
+ GetParametricCurveByType(InCurve ->InterpParams->ContextID, InCurve ->Segments[0].Type, NULL) != NULL) {
+
+ return cmsBuildParametricToneCurve(InCurve ->InterpParams->ContextID,
+ -(InCurve -> Segments[0].Type),
+ InCurve -> Segments[0].Params);
+ }
+
+ // Nope, reverse the table.
+ out = cmsBuildTabulatedToneCurve16(InCurve ->InterpParams->ContextID, nResultSamples, NULL);
+ if (out == NULL)
+ return NULL;
+
+ // We want to know if this is an ascending or descending table
+ Ascending = !cmsIsToneCurveDescending(InCurve);
+
+ // Iterate across Y axis
+ for (i=0; i < nResultSamples; i++) {
+
+ y = (cmsFloat64Number) i * 65535.0 / (nResultSamples - 1);
+
+ // Find interval in which y is within.
+ j = GetInterval(y, InCurve->Table16, InCurve->InterpParams);
+ if (j >= 0) {
+
+
+ // Get limits of interval
+ x1 = InCurve ->Table16[j];
+ x2 = InCurve ->Table16[j+1];
+
+ y1 = (cmsFloat64Number) (j * 65535.0) / (InCurve ->nEntries - 1);
+ y2 = (cmsFloat64Number) ((j+1) * 65535.0 ) / (InCurve ->nEntries - 1);
+
+ // If collapsed, then use any
+ if (x1 == x2) {
+
+ out ->Table16[i] = _cmsQuickSaturateWord(Ascending ? y2 : y1);
+ continue;
+
+ } else {
+
+ // Interpolate
+ a = (y2 - y1) / (x2 - x1);
+ b = y2 - a * x2;
+ }
+ }
+
+ out ->Table16[i] = _cmsQuickSaturateWord(a* y + b);
+ }
+
+
+ return out;
+}
+
+// Reverse a gamma table
+cmsToneCurve* CMSEXPORT cmsReverseToneCurve(const cmsToneCurve* InGamma)
+{
+ _cmsAssert(InGamma != NULL);
+
+ return cmsReverseToneCurveEx(4096, InGamma);
+}
+
+// From: Eilers, P.H.C. (1994) Smoothing and interpolation with finite
+// differences. in: Graphic Gems IV, Heckbert, P.S. (ed.), Academic press.
+//
+// Smoothing and interpolation with second differences.
+//
+// Input: weights (w), data (y): vector from 1 to m.
+// Input: smoothing parameter (lambda), length (m).
+// Output: smoothed vector (z): vector from 1 to m.
+
+static
+cmsBool smooth2(cmsContext ContextID, cmsFloat32Number w[], cmsFloat32Number y[], cmsFloat32Number z[], cmsFloat32Number lambda, int m)
+{
+ int i, i1, i2;
+ cmsFloat32Number *c, *d, *e;
+ cmsBool st;
+
+
+ c = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
+ d = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
+ e = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
+
+ if (c != NULL && d != NULL && e != NULL) {
+
+
+ d[1] = w[1] + lambda;
+ c[1] = -2 * lambda / d[1];
+ e[1] = lambda /d[1];
+ z[1] = w[1] * y[1];
+ d[2] = w[2] + 5 * lambda - d[1] * c[1] * c[1];
+ c[2] = (-4 * lambda - d[1] * c[1] * e[1]) / d[2];
+ e[2] = lambda / d[2];
+ z[2] = w[2] * y[2] - c[1] * z[1];
+
+ for (i = 3; i < m - 1; i++) {
+ i1 = i - 1; i2 = i - 2;
+ d[i]= w[i] + 6 * lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
+ c[i] = (-4 * lambda -d[i1] * c[i1] * e[i1])/ d[i];
+ e[i] = lambda / d[i];
+ z[i] = w[i] * y[i] - c[i1] * z[i1] - e[i2] * z[i2];
+ }
+
+ i1 = m - 2; i2 = m - 3;
+
+ d[m - 1] = w[m - 1] + 5 * lambda -c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
+ c[m - 1] = (-2 * lambda - d[i1] * c[i1] * e[i1]) / d[m - 1];
+ z[m - 1] = w[m - 1] * y[m - 1] - c[i1] * z[i1] - e[i2] * z[i2];
+ i1 = m - 1; i2 = m - 2;
+
+ d[m] = w[m] + lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
+ z[m] = (w[m] * y[m] - c[i1] * z[i1] - e[i2] * z[i2]) / d[m];
+ z[m - 1] = z[m - 1] / d[m - 1] - c[m - 1] * z[m];
+
+ for (i = m - 2; 1<= i; i--)
+ z[i] = z[i] / d[i] - c[i] * z[i + 1] - e[i] * z[i + 2];
+
+ st = TRUE;
+ }
+ else st = FALSE;
+
+ if (c != NULL) _cmsFree(ContextID, c);
+ if (d != NULL) _cmsFree(ContextID, d);
+ if (e != NULL) _cmsFree(ContextID, e);
+
+ return st;
+}
+
+// Smooths a curve sampled at regular intervals.
+cmsBool CMSEXPORT cmsSmoothToneCurve(cmsToneCurve* Tab, cmsFloat64Number lambda)
+{
+ cmsFloat32Number w[MAX_NODES_IN_CURVE], y[MAX_NODES_IN_CURVE], z[MAX_NODES_IN_CURVE];
+ int i, nItems, Zeros, Poles;
+
+ if (Tab == NULL) return FALSE;
+
+ if (cmsIsToneCurveLinear(Tab)) return TRUE; // Nothing to do
+
+ nItems = Tab -> nEntries;
+
+ if (nItems >= MAX_NODES_IN_CURVE) {
+ cmsSignalError(Tab ->InterpParams->ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: too many points.");
+ return FALSE;
+ }
+
+ memset(w, 0, nItems * sizeof(cmsFloat32Number));
+ memset(y, 0, nItems * sizeof(cmsFloat32Number));
+ memset(z, 0, nItems * sizeof(cmsFloat32Number));
+
+ for (i=0; i < nItems; i++)
+ {
+ y[i+1] = (cmsFloat32Number) Tab -> Table16[i];
+ w[i+1] = 1.0;
+ }
+
+ if (!smooth2(Tab ->InterpParams->ContextID, w, y, z, (cmsFloat32Number) lambda, nItems)) return FALSE;
+
+ // Do some reality - checking...
+ Zeros = Poles = 0;
+ for (i=nItems; i > 1; --i) {
+
+ if (z[i] == 0.) Zeros++;
+ if (z[i] >= 65535.) Poles++;
+ if (z[i] < z[i-1]) {
+ cmsSignalError(Tab ->InterpParams->ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Non-Monotonic.");
+ return FALSE;
+ }
+ }
+
+ if (Zeros > (nItems / 3)) {
+ cmsSignalError(Tab ->InterpParams->ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly zeros.");
+ return FALSE;
+ }
+ if (Poles > (nItems / 3)) {
+ cmsSignalError(Tab ->InterpParams->ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly poles.");
+ return FALSE;
+ }
+
+ // Seems ok
+ for (i=0; i < nItems; i++) {
+
+ // Clamp to cmsUInt16Number
+ Tab -> Table16[i] = _cmsQuickSaturateWord(z[i+1]);
+ }
+
+ return TRUE;
+}
+
+// Is a table linear? Do not use parametric since we cannot guarantee some weird parameters resulting
+// in a linear table. This way assures it is linear in 12 bits, which should be enought in most cases.
+cmsBool CMSEXPORT cmsIsToneCurveLinear(const cmsToneCurve* Curve)
+{
+ cmsUInt32Number i;
+ int diff;
+
+ _cmsAssert(Curve != NULL);
+
+ for (i=0; i < Curve ->nEntries; i++) {
+
+ diff = abs((int) Curve->Table16[i] - (int) _cmsQuantizeVal(i, Curve ->nEntries));
+ if (diff > 0x0f)
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+// Same, but for monotonicity
+cmsBool CMSEXPORT cmsIsToneCurveMonotonic(const cmsToneCurve* t)
+{
+ int n;
+ int i, last;
+ cmsBool lDescending;
+
+ _cmsAssert(t != NULL);
+
+ // Degenerated curves are monotonic? Ok, let's pass them
+ n = t ->nEntries;
+ if (n < 2) return TRUE;
+
+ // Curve direction
+ lDescending = cmsIsToneCurveDescending(t);
+
+ if (lDescending) {
+
+ last = t ->Table16[0];
+
+ for (i = 1; i < n; i++) {
+
+ if (t ->Table16[i] - last > 2) // We allow some ripple
+ return FALSE;
+ else
+ last = t ->Table16[i];
+
+ }
+ }
+ else {
+
+ last = t ->Table16[n-1];
+
+ for (i = n-2; i >= 0; --i) {
+
+ if (t ->Table16[i] - last > 2)
+ return FALSE;
+ else
+ last = t ->Table16[i];
+
+ }
+ }
+
+ return TRUE;
+}
+
+// Same, but for descending tables
+cmsBool CMSEXPORT cmsIsToneCurveDescending(const cmsToneCurve* t)
+{
+ _cmsAssert(t != NULL);
+
+ return t ->Table16[0] > t ->Table16[t ->nEntries-1];
+}
+
+
+// Another info fn: is out gamma table multisegment?
+cmsBool CMSEXPORT cmsIsToneCurveMultisegment(const cmsToneCurve* t)
+{
+ _cmsAssert(t != NULL);
+
+ return t -> nSegments > 1;
+}
+
+cmsInt32Number CMSEXPORT cmsGetToneCurveParametricType(const cmsToneCurve* t)
+{
+ _cmsAssert(t != NULL);
+
+ if (t -> nSegments != 1) return 0;
+ return t ->Segments[0].Type;
+}
+
+// We need accuracy this time
+cmsFloat32Number CMSEXPORT cmsEvalToneCurveFloat(const cmsToneCurve* Curve, cmsFloat32Number v)
+{
+ _cmsAssert(Curve != NULL);
+
+ // Check for 16 bits table. If so, this is a limited-precision tone curve
+ if (Curve ->nSegments == 0) {
+
+ cmsUInt16Number In, Out;
+
+ In = (cmsUInt16Number) _cmsQuickSaturateWord(v * 65535.0);
+ Out = cmsEvalToneCurve16(Curve, In);
+
+ return (cmsFloat32Number) (Out / 65535.0);
+ }
+
+ return (cmsFloat32Number) EvalSegmentedFn(Curve, v);
+}
+
+// We need xput over here
+cmsUInt16Number CMSEXPORT cmsEvalToneCurve16(const cmsToneCurve* Curve, cmsUInt16Number v)
+{
+ cmsUInt16Number out;
+
+ _cmsAssert(Curve != NULL);
+
+ Curve ->InterpParams ->Interpolation.Lerp16(&v, &out, Curve ->InterpParams);
+ return out;
+}
+
+
+// Least squares fitting.
+// A mathematical procedure for finding the best-fitting curve to a given set of points by
+// minimizing the sum of the squares of the offsets ("the residuals") of the points from the curve.
+// The sum of the squares of the offsets is used instead of the offset absolute values because
+// this allows the residuals to be treated as a continuous differentiable quantity.
+//
+// y = f(x) = x ^ g
+//
+// R = (yi - (xi^g))
+// R2 = (yi - (xi^g))2
+// SUM R2 = SUM (yi - (xi^g))2
+//
+// dR2/dg = -2 SUM x^g log(x)(y - x^g)
+// solving for dR2/dg = 0
+//
+// g = 1/n * SUM(log(y) / log(x))
+
+cmsFloat64Number CMSEXPORT cmsEstimateGamma(const cmsToneCurve* t, cmsFloat64Number Precision)
+{
+ cmsFloat64Number gamma, sum, sum2;
+ cmsFloat64Number n, x, y, Std;
+ cmsUInt32Number i;
+
+ _cmsAssert(t != NULL);
+
+ sum = sum2 = n = 0;
+
+ // Excluding endpoints
+ for (i=1; i < (MAX_NODES_IN_CURVE-1); i++) {
+
+ x = (cmsFloat64Number) i / (MAX_NODES_IN_CURVE-1);
+ y = (cmsFloat64Number) cmsEvalToneCurveFloat(t, (cmsFloat32Number) x);
+
+ // Avoid 7% on lower part to prevent
+ // artifacts due to linear ramps
+
+ if (y > 0. && y < 1. && x > 0.07) {
+
+ gamma = log(y) / log(x);
+ sum += gamma;
+ sum2 += gamma * gamma;
+ n++;
+ }
+ }
+
+ // Take a look on SD to see if gamma isn't exponential at all
+ Std = sqrt((n * sum2 - sum * sum) / (n*(n-1)));
+
+ if (Std > Precision)
+ return -1.0;
+
+ return (sum / n); // The mean
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c
new file mode 100644
index 0000000000..09427650c9
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c
@@ -0,0 +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;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmshalf.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmshalf.c
new file mode 100644
index 0000000000..f038b57b4c
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmshalf.c
@@ -0,0 +1,534 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+#ifndef CMS_NO_HALF_SUPPORT
+
+// This code is inspired in the paper "Fast Half Float Conversions"
+// by Jeroen van der Zijp
+
+static cmsUInt32Number Mantissa[2048] = {
+
+0x00000000, 0x33800000, 0x34000000, 0x34400000, 0x34800000, 0x34a00000,
+0x34c00000, 0x34e00000, 0x35000000, 0x35100000, 0x35200000, 0x35300000,
+0x35400000, 0x35500000, 0x35600000, 0x35700000, 0x35800000, 0x35880000,
+0x35900000, 0x35980000, 0x35a00000, 0x35a80000, 0x35b00000, 0x35b80000,
+0x35c00000, 0x35c80000, 0x35d00000, 0x35d80000, 0x35e00000, 0x35e80000,
+0x35f00000, 0x35f80000, 0x36000000, 0x36040000, 0x36080000, 0x360c0000,
+0x36100000, 0x36140000, 0x36180000, 0x361c0000, 0x36200000, 0x36240000,
+0x36280000, 0x362c0000, 0x36300000, 0x36340000, 0x36380000, 0x363c0000,
+0x36400000, 0x36440000, 0x36480000, 0x364c0000, 0x36500000, 0x36540000,
+0x36580000, 0x365c0000, 0x36600000, 0x36640000, 0x36680000, 0x366c0000,
+0x36700000, 0x36740000, 0x36780000, 0x367c0000, 0x36800000, 0x36820000,
+0x36840000, 0x36860000, 0x36880000, 0x368a0000, 0x368c0000, 0x368e0000,
+0x36900000, 0x36920000, 0x36940000, 0x36960000, 0x36980000, 0x369a0000,
+0x369c0000, 0x369e0000, 0x36a00000, 0x36a20000, 0x36a40000, 0x36a60000,
+0x36a80000, 0x36aa0000, 0x36ac0000, 0x36ae0000, 0x36b00000, 0x36b20000,
+0x36b40000, 0x36b60000, 0x36b80000, 0x36ba0000, 0x36bc0000, 0x36be0000,
+0x36c00000, 0x36c20000, 0x36c40000, 0x36c60000, 0x36c80000, 0x36ca0000,
+0x36cc0000, 0x36ce0000, 0x36d00000, 0x36d20000, 0x36d40000, 0x36d60000,
+0x36d80000, 0x36da0000, 0x36dc0000, 0x36de0000, 0x36e00000, 0x36e20000,
+0x36e40000, 0x36e60000, 0x36e80000, 0x36ea0000, 0x36ec0000, 0x36ee0000,
+0x36f00000, 0x36f20000, 0x36f40000, 0x36f60000, 0x36f80000, 0x36fa0000,
+0x36fc0000, 0x36fe0000, 0x37000000, 0x37010000, 0x37020000, 0x37030000,
+0x37040000, 0x37050000, 0x37060000, 0x37070000, 0x37080000, 0x37090000,
+0x370a0000, 0x370b0000, 0x370c0000, 0x370d0000, 0x370e0000, 0x370f0000,
+0x37100000, 0x37110000, 0x37120000, 0x37130000, 0x37140000, 0x37150000,
+0x37160000, 0x37170000, 0x37180000, 0x37190000, 0x371a0000, 0x371b0000,
+0x371c0000, 0x371d0000, 0x371e0000, 0x371f0000, 0x37200000, 0x37210000,
+0x37220000, 0x37230000, 0x37240000, 0x37250000, 0x37260000, 0x37270000,
+0x37280000, 0x37290000, 0x372a0000, 0x372b0000, 0x372c0000, 0x372d0000,
+0x372e0000, 0x372f0000, 0x37300000, 0x37310000, 0x37320000, 0x37330000,
+0x37340000, 0x37350000, 0x37360000, 0x37370000, 0x37380000, 0x37390000,
+0x373a0000, 0x373b0000, 0x373c0000, 0x373d0000, 0x373e0000, 0x373f0000,
+0x37400000, 0x37410000, 0x37420000, 0x37430000, 0x37440000, 0x37450000,
+0x37460000, 0x37470000, 0x37480000, 0x37490000, 0x374a0000, 0x374b0000,
+0x374c0000, 0x374d0000, 0x374e0000, 0x374f0000, 0x37500000, 0x37510000,
+0x37520000, 0x37530000, 0x37540000, 0x37550000, 0x37560000, 0x37570000,
+0x37580000, 0x37590000, 0x375a0000, 0x375b0000, 0x375c0000, 0x375d0000,
+0x375e0000, 0x375f0000, 0x37600000, 0x37610000, 0x37620000, 0x37630000,
+0x37640000, 0x37650000, 0x37660000, 0x37670000, 0x37680000, 0x37690000,
+0x376a0000, 0x376b0000, 0x376c0000, 0x376d0000, 0x376e0000, 0x376f0000,
+0x37700000, 0x37710000, 0x37720000, 0x37730000, 0x37740000, 0x37750000,
+0x37760000, 0x37770000, 0x37780000, 0x37790000, 0x377a0000, 0x377b0000,
+0x377c0000, 0x377d0000, 0x377e0000, 0x377f0000, 0x37800000, 0x37808000,
+0x37810000, 0x37818000, 0x37820000, 0x37828000, 0x37830000, 0x37838000,
+0x37840000, 0x37848000, 0x37850000, 0x37858000, 0x37860000, 0x37868000,
+0x37870000, 0x37878000, 0x37880000, 0x37888000, 0x37890000, 0x37898000,
+0x378a0000, 0x378a8000, 0x378b0000, 0x378b8000, 0x378c0000, 0x378c8000,
+0x378d0000, 0x378d8000, 0x378e0000, 0x378e8000, 0x378f0000, 0x378f8000,
+0x37900000, 0x37908000, 0x37910000, 0x37918000, 0x37920000, 0x37928000,
+0x37930000, 0x37938000, 0x37940000, 0x37948000, 0x37950000, 0x37958000,
+0x37960000, 0x37968000, 0x37970000, 0x37978000, 0x37980000, 0x37988000,
+0x37990000, 0x37998000, 0x379a0000, 0x379a8000, 0x379b0000, 0x379b8000,
+0x379c0000, 0x379c8000, 0x379d0000, 0x379d8000, 0x379e0000, 0x379e8000,
+0x379f0000, 0x379f8000, 0x37a00000, 0x37a08000, 0x37a10000, 0x37a18000,
+0x37a20000, 0x37a28000, 0x37a30000, 0x37a38000, 0x37a40000, 0x37a48000,
+0x37a50000, 0x37a58000, 0x37a60000, 0x37a68000, 0x37a70000, 0x37a78000,
+0x37a80000, 0x37a88000, 0x37a90000, 0x37a98000, 0x37aa0000, 0x37aa8000,
+0x37ab0000, 0x37ab8000, 0x37ac0000, 0x37ac8000, 0x37ad0000, 0x37ad8000,
+0x37ae0000, 0x37ae8000, 0x37af0000, 0x37af8000, 0x37b00000, 0x37b08000,
+0x37b10000, 0x37b18000, 0x37b20000, 0x37b28000, 0x37b30000, 0x37b38000,
+0x37b40000, 0x37b48000, 0x37b50000, 0x37b58000, 0x37b60000, 0x37b68000,
+0x37b70000, 0x37b78000, 0x37b80000, 0x37b88000, 0x37b90000, 0x37b98000,
+0x37ba0000, 0x37ba8000, 0x37bb0000, 0x37bb8000, 0x37bc0000, 0x37bc8000,
+0x37bd0000, 0x37bd8000, 0x37be0000, 0x37be8000, 0x37bf0000, 0x37bf8000,
+0x37c00000, 0x37c08000, 0x37c10000, 0x37c18000, 0x37c20000, 0x37c28000,
+0x37c30000, 0x37c38000, 0x37c40000, 0x37c48000, 0x37c50000, 0x37c58000,
+0x37c60000, 0x37c68000, 0x37c70000, 0x37c78000, 0x37c80000, 0x37c88000,
+0x37c90000, 0x37c98000, 0x37ca0000, 0x37ca8000, 0x37cb0000, 0x37cb8000,
+0x37cc0000, 0x37cc8000, 0x37cd0000, 0x37cd8000, 0x37ce0000, 0x37ce8000,
+0x37cf0000, 0x37cf8000, 0x37d00000, 0x37d08000, 0x37d10000, 0x37d18000,
+0x37d20000, 0x37d28000, 0x37d30000, 0x37d38000, 0x37d40000, 0x37d48000,
+0x37d50000, 0x37d58000, 0x37d60000, 0x37d68000, 0x37d70000, 0x37d78000,
+0x37d80000, 0x37d88000, 0x37d90000, 0x37d98000, 0x37da0000, 0x37da8000,
+0x37db0000, 0x37db8000, 0x37dc0000, 0x37dc8000, 0x37dd0000, 0x37dd8000,
+0x37de0000, 0x37de8000, 0x37df0000, 0x37df8000, 0x37e00000, 0x37e08000,
+0x37e10000, 0x37e18000, 0x37e20000, 0x37e28000, 0x37e30000, 0x37e38000,
+0x37e40000, 0x37e48000, 0x37e50000, 0x37e58000, 0x37e60000, 0x37e68000,
+0x37e70000, 0x37e78000, 0x37e80000, 0x37e88000, 0x37e90000, 0x37e98000,
+0x37ea0000, 0x37ea8000, 0x37eb0000, 0x37eb8000, 0x37ec0000, 0x37ec8000,
+0x37ed0000, 0x37ed8000, 0x37ee0000, 0x37ee8000, 0x37ef0000, 0x37ef8000,
+0x37f00000, 0x37f08000, 0x37f10000, 0x37f18000, 0x37f20000, 0x37f28000,
+0x37f30000, 0x37f38000, 0x37f40000, 0x37f48000, 0x37f50000, 0x37f58000,
+0x37f60000, 0x37f68000, 0x37f70000, 0x37f78000, 0x37f80000, 0x37f88000,
+0x37f90000, 0x37f98000, 0x37fa0000, 0x37fa8000, 0x37fb0000, 0x37fb8000,
+0x37fc0000, 0x37fc8000, 0x37fd0000, 0x37fd8000, 0x37fe0000, 0x37fe8000,
+0x37ff0000, 0x37ff8000, 0x38000000, 0x38004000, 0x38008000, 0x3800c000,
+0x38010000, 0x38014000, 0x38018000, 0x3801c000, 0x38020000, 0x38024000,
+0x38028000, 0x3802c000, 0x38030000, 0x38034000, 0x38038000, 0x3803c000,
+0x38040000, 0x38044000, 0x38048000, 0x3804c000, 0x38050000, 0x38054000,
+0x38058000, 0x3805c000, 0x38060000, 0x38064000, 0x38068000, 0x3806c000,
+0x38070000, 0x38074000, 0x38078000, 0x3807c000, 0x38080000, 0x38084000,
+0x38088000, 0x3808c000, 0x38090000, 0x38094000, 0x38098000, 0x3809c000,
+0x380a0000, 0x380a4000, 0x380a8000, 0x380ac000, 0x380b0000, 0x380b4000,
+0x380b8000, 0x380bc000, 0x380c0000, 0x380c4000, 0x380c8000, 0x380cc000,
+0x380d0000, 0x380d4000, 0x380d8000, 0x380dc000, 0x380e0000, 0x380e4000,
+0x380e8000, 0x380ec000, 0x380f0000, 0x380f4000, 0x380f8000, 0x380fc000,
+0x38100000, 0x38104000, 0x38108000, 0x3810c000, 0x38110000, 0x38114000,
+0x38118000, 0x3811c000, 0x38120000, 0x38124000, 0x38128000, 0x3812c000,
+0x38130000, 0x38134000, 0x38138000, 0x3813c000, 0x38140000, 0x38144000,
+0x38148000, 0x3814c000, 0x38150000, 0x38154000, 0x38158000, 0x3815c000,
+0x38160000, 0x38164000, 0x38168000, 0x3816c000, 0x38170000, 0x38174000,
+0x38178000, 0x3817c000, 0x38180000, 0x38184000, 0x38188000, 0x3818c000,
+0x38190000, 0x38194000, 0x38198000, 0x3819c000, 0x381a0000, 0x381a4000,
+0x381a8000, 0x381ac000, 0x381b0000, 0x381b4000, 0x381b8000, 0x381bc000,
+0x381c0000, 0x381c4000, 0x381c8000, 0x381cc000, 0x381d0000, 0x381d4000,
+0x381d8000, 0x381dc000, 0x381e0000, 0x381e4000, 0x381e8000, 0x381ec000,
+0x381f0000, 0x381f4000, 0x381f8000, 0x381fc000, 0x38200000, 0x38204000,
+0x38208000, 0x3820c000, 0x38210000, 0x38214000, 0x38218000, 0x3821c000,
+0x38220000, 0x38224000, 0x38228000, 0x3822c000, 0x38230000, 0x38234000,
+0x38238000, 0x3823c000, 0x38240000, 0x38244000, 0x38248000, 0x3824c000,
+0x38250000, 0x38254000, 0x38258000, 0x3825c000, 0x38260000, 0x38264000,
+0x38268000, 0x3826c000, 0x38270000, 0x38274000, 0x38278000, 0x3827c000,
+0x38280000, 0x38284000, 0x38288000, 0x3828c000, 0x38290000, 0x38294000,
+0x38298000, 0x3829c000, 0x382a0000, 0x382a4000, 0x382a8000, 0x382ac000,
+0x382b0000, 0x382b4000, 0x382b8000, 0x382bc000, 0x382c0000, 0x382c4000,
+0x382c8000, 0x382cc000, 0x382d0000, 0x382d4000, 0x382d8000, 0x382dc000,
+0x382e0000, 0x382e4000, 0x382e8000, 0x382ec000, 0x382f0000, 0x382f4000,
+0x382f8000, 0x382fc000, 0x38300000, 0x38304000, 0x38308000, 0x3830c000,
+0x38310000, 0x38314000, 0x38318000, 0x3831c000, 0x38320000, 0x38324000,
+0x38328000, 0x3832c000, 0x38330000, 0x38334000, 0x38338000, 0x3833c000,
+0x38340000, 0x38344000, 0x38348000, 0x3834c000, 0x38350000, 0x38354000,
+0x38358000, 0x3835c000, 0x38360000, 0x38364000, 0x38368000, 0x3836c000,
+0x38370000, 0x38374000, 0x38378000, 0x3837c000, 0x38380000, 0x38384000,
+0x38388000, 0x3838c000, 0x38390000, 0x38394000, 0x38398000, 0x3839c000,
+0x383a0000, 0x383a4000, 0x383a8000, 0x383ac000, 0x383b0000, 0x383b4000,
+0x383b8000, 0x383bc000, 0x383c0000, 0x383c4000, 0x383c8000, 0x383cc000,
+0x383d0000, 0x383d4000, 0x383d8000, 0x383dc000, 0x383e0000, 0x383e4000,
+0x383e8000, 0x383ec000, 0x383f0000, 0x383f4000, 0x383f8000, 0x383fc000,
+0x38400000, 0x38404000, 0x38408000, 0x3840c000, 0x38410000, 0x38414000,
+0x38418000, 0x3841c000, 0x38420000, 0x38424000, 0x38428000, 0x3842c000,
+0x38430000, 0x38434000, 0x38438000, 0x3843c000, 0x38440000, 0x38444000,
+0x38448000, 0x3844c000, 0x38450000, 0x38454000, 0x38458000, 0x3845c000,
+0x38460000, 0x38464000, 0x38468000, 0x3846c000, 0x38470000, 0x38474000,
+0x38478000, 0x3847c000, 0x38480000, 0x38484000, 0x38488000, 0x3848c000,
+0x38490000, 0x38494000, 0x38498000, 0x3849c000, 0x384a0000, 0x384a4000,
+0x384a8000, 0x384ac000, 0x384b0000, 0x384b4000, 0x384b8000, 0x384bc000,
+0x384c0000, 0x384c4000, 0x384c8000, 0x384cc000, 0x384d0000, 0x384d4000,
+0x384d8000, 0x384dc000, 0x384e0000, 0x384e4000, 0x384e8000, 0x384ec000,
+0x384f0000, 0x384f4000, 0x384f8000, 0x384fc000, 0x38500000, 0x38504000,
+0x38508000, 0x3850c000, 0x38510000, 0x38514000, 0x38518000, 0x3851c000,
+0x38520000, 0x38524000, 0x38528000, 0x3852c000, 0x38530000, 0x38534000,
+0x38538000, 0x3853c000, 0x38540000, 0x38544000, 0x38548000, 0x3854c000,
+0x38550000, 0x38554000, 0x38558000, 0x3855c000, 0x38560000, 0x38564000,
+0x38568000, 0x3856c000, 0x38570000, 0x38574000, 0x38578000, 0x3857c000,
+0x38580000, 0x38584000, 0x38588000, 0x3858c000, 0x38590000, 0x38594000,
+0x38598000, 0x3859c000, 0x385a0000, 0x385a4000, 0x385a8000, 0x385ac000,
+0x385b0000, 0x385b4000, 0x385b8000, 0x385bc000, 0x385c0000, 0x385c4000,
+0x385c8000, 0x385cc000, 0x385d0000, 0x385d4000, 0x385d8000, 0x385dc000,
+0x385e0000, 0x385e4000, 0x385e8000, 0x385ec000, 0x385f0000, 0x385f4000,
+0x385f8000, 0x385fc000, 0x38600000, 0x38604000, 0x38608000, 0x3860c000,
+0x38610000, 0x38614000, 0x38618000, 0x3861c000, 0x38620000, 0x38624000,
+0x38628000, 0x3862c000, 0x38630000, 0x38634000, 0x38638000, 0x3863c000,
+0x38640000, 0x38644000, 0x38648000, 0x3864c000, 0x38650000, 0x38654000,
+0x38658000, 0x3865c000, 0x38660000, 0x38664000, 0x38668000, 0x3866c000,
+0x38670000, 0x38674000, 0x38678000, 0x3867c000, 0x38680000, 0x38684000,
+0x38688000, 0x3868c000, 0x38690000, 0x38694000, 0x38698000, 0x3869c000,
+0x386a0000, 0x386a4000, 0x386a8000, 0x386ac000, 0x386b0000, 0x386b4000,
+0x386b8000, 0x386bc000, 0x386c0000, 0x386c4000, 0x386c8000, 0x386cc000,
+0x386d0000, 0x386d4000, 0x386d8000, 0x386dc000, 0x386e0000, 0x386e4000,
+0x386e8000, 0x386ec000, 0x386f0000, 0x386f4000, 0x386f8000, 0x386fc000,
+0x38700000, 0x38704000, 0x38708000, 0x3870c000, 0x38710000, 0x38714000,
+0x38718000, 0x3871c000, 0x38720000, 0x38724000, 0x38728000, 0x3872c000,
+0x38730000, 0x38734000, 0x38738000, 0x3873c000, 0x38740000, 0x38744000,
+0x38748000, 0x3874c000, 0x38750000, 0x38754000, 0x38758000, 0x3875c000,
+0x38760000, 0x38764000, 0x38768000, 0x3876c000, 0x38770000, 0x38774000,
+0x38778000, 0x3877c000, 0x38780000, 0x38784000, 0x38788000, 0x3878c000,
+0x38790000, 0x38794000, 0x38798000, 0x3879c000, 0x387a0000, 0x387a4000,
+0x387a8000, 0x387ac000, 0x387b0000, 0x387b4000, 0x387b8000, 0x387bc000,
+0x387c0000, 0x387c4000, 0x387c8000, 0x387cc000, 0x387d0000, 0x387d4000,
+0x387d8000, 0x387dc000, 0x387e0000, 0x387e4000, 0x387e8000, 0x387ec000,
+0x387f0000, 0x387f4000, 0x387f8000, 0x387fc000, 0x38000000, 0x38002000,
+0x38004000, 0x38006000, 0x38008000, 0x3800a000, 0x3800c000, 0x3800e000,
+0x38010000, 0x38012000, 0x38014000, 0x38016000, 0x38018000, 0x3801a000,
+0x3801c000, 0x3801e000, 0x38020000, 0x38022000, 0x38024000, 0x38026000,
+0x38028000, 0x3802a000, 0x3802c000, 0x3802e000, 0x38030000, 0x38032000,
+0x38034000, 0x38036000, 0x38038000, 0x3803a000, 0x3803c000, 0x3803e000,
+0x38040000, 0x38042000, 0x38044000, 0x38046000, 0x38048000, 0x3804a000,
+0x3804c000, 0x3804e000, 0x38050000, 0x38052000, 0x38054000, 0x38056000,
+0x38058000, 0x3805a000, 0x3805c000, 0x3805e000, 0x38060000, 0x38062000,
+0x38064000, 0x38066000, 0x38068000, 0x3806a000, 0x3806c000, 0x3806e000,
+0x38070000, 0x38072000, 0x38074000, 0x38076000, 0x38078000, 0x3807a000,
+0x3807c000, 0x3807e000, 0x38080000, 0x38082000, 0x38084000, 0x38086000,
+0x38088000, 0x3808a000, 0x3808c000, 0x3808e000, 0x38090000, 0x38092000,
+0x38094000, 0x38096000, 0x38098000, 0x3809a000, 0x3809c000, 0x3809e000,
+0x380a0000, 0x380a2000, 0x380a4000, 0x380a6000, 0x380a8000, 0x380aa000,
+0x380ac000, 0x380ae000, 0x380b0000, 0x380b2000, 0x380b4000, 0x380b6000,
+0x380b8000, 0x380ba000, 0x380bc000, 0x380be000, 0x380c0000, 0x380c2000,
+0x380c4000, 0x380c6000, 0x380c8000, 0x380ca000, 0x380cc000, 0x380ce000,
+0x380d0000, 0x380d2000, 0x380d4000, 0x380d6000, 0x380d8000, 0x380da000,
+0x380dc000, 0x380de000, 0x380e0000, 0x380e2000, 0x380e4000, 0x380e6000,
+0x380e8000, 0x380ea000, 0x380ec000, 0x380ee000, 0x380f0000, 0x380f2000,
+0x380f4000, 0x380f6000, 0x380f8000, 0x380fa000, 0x380fc000, 0x380fe000,
+0x38100000, 0x38102000, 0x38104000, 0x38106000, 0x38108000, 0x3810a000,
+0x3810c000, 0x3810e000, 0x38110000, 0x38112000, 0x38114000, 0x38116000,
+0x38118000, 0x3811a000, 0x3811c000, 0x3811e000, 0x38120000, 0x38122000,
+0x38124000, 0x38126000, 0x38128000, 0x3812a000, 0x3812c000, 0x3812e000,
+0x38130000, 0x38132000, 0x38134000, 0x38136000, 0x38138000, 0x3813a000,
+0x3813c000, 0x3813e000, 0x38140000, 0x38142000, 0x38144000, 0x38146000,
+0x38148000, 0x3814a000, 0x3814c000, 0x3814e000, 0x38150000, 0x38152000,
+0x38154000, 0x38156000, 0x38158000, 0x3815a000, 0x3815c000, 0x3815e000,
+0x38160000, 0x38162000, 0x38164000, 0x38166000, 0x38168000, 0x3816a000,
+0x3816c000, 0x3816e000, 0x38170000, 0x38172000, 0x38174000, 0x38176000,
+0x38178000, 0x3817a000, 0x3817c000, 0x3817e000, 0x38180000, 0x38182000,
+0x38184000, 0x38186000, 0x38188000, 0x3818a000, 0x3818c000, 0x3818e000,
+0x38190000, 0x38192000, 0x38194000, 0x38196000, 0x38198000, 0x3819a000,
+0x3819c000, 0x3819e000, 0x381a0000, 0x381a2000, 0x381a4000, 0x381a6000,
+0x381a8000, 0x381aa000, 0x381ac000, 0x381ae000, 0x381b0000, 0x381b2000,
+0x381b4000, 0x381b6000, 0x381b8000, 0x381ba000, 0x381bc000, 0x381be000,
+0x381c0000, 0x381c2000, 0x381c4000, 0x381c6000, 0x381c8000, 0x381ca000,
+0x381cc000, 0x381ce000, 0x381d0000, 0x381d2000, 0x381d4000, 0x381d6000,
+0x381d8000, 0x381da000, 0x381dc000, 0x381de000, 0x381e0000, 0x381e2000,
+0x381e4000, 0x381e6000, 0x381e8000, 0x381ea000, 0x381ec000, 0x381ee000,
+0x381f0000, 0x381f2000, 0x381f4000, 0x381f6000, 0x381f8000, 0x381fa000,
+0x381fc000, 0x381fe000, 0x38200000, 0x38202000, 0x38204000, 0x38206000,
+0x38208000, 0x3820a000, 0x3820c000, 0x3820e000, 0x38210000, 0x38212000,
+0x38214000, 0x38216000, 0x38218000, 0x3821a000, 0x3821c000, 0x3821e000,
+0x38220000, 0x38222000, 0x38224000, 0x38226000, 0x38228000, 0x3822a000,
+0x3822c000, 0x3822e000, 0x38230000, 0x38232000, 0x38234000, 0x38236000,
+0x38238000, 0x3823a000, 0x3823c000, 0x3823e000, 0x38240000, 0x38242000,
+0x38244000, 0x38246000, 0x38248000, 0x3824a000, 0x3824c000, 0x3824e000,
+0x38250000, 0x38252000, 0x38254000, 0x38256000, 0x38258000, 0x3825a000,
+0x3825c000, 0x3825e000, 0x38260000, 0x38262000, 0x38264000, 0x38266000,
+0x38268000, 0x3826a000, 0x3826c000, 0x3826e000, 0x38270000, 0x38272000,
+0x38274000, 0x38276000, 0x38278000, 0x3827a000, 0x3827c000, 0x3827e000,
+0x38280000, 0x38282000, 0x38284000, 0x38286000, 0x38288000, 0x3828a000,
+0x3828c000, 0x3828e000, 0x38290000, 0x38292000, 0x38294000, 0x38296000,
+0x38298000, 0x3829a000, 0x3829c000, 0x3829e000, 0x382a0000, 0x382a2000,
+0x382a4000, 0x382a6000, 0x382a8000, 0x382aa000, 0x382ac000, 0x382ae000,
+0x382b0000, 0x382b2000, 0x382b4000, 0x382b6000, 0x382b8000, 0x382ba000,
+0x382bc000, 0x382be000, 0x382c0000, 0x382c2000, 0x382c4000, 0x382c6000,
+0x382c8000, 0x382ca000, 0x382cc000, 0x382ce000, 0x382d0000, 0x382d2000,
+0x382d4000, 0x382d6000, 0x382d8000, 0x382da000, 0x382dc000, 0x382de000,
+0x382e0000, 0x382e2000, 0x382e4000, 0x382e6000, 0x382e8000, 0x382ea000,
+0x382ec000, 0x382ee000, 0x382f0000, 0x382f2000, 0x382f4000, 0x382f6000,
+0x382f8000, 0x382fa000, 0x382fc000, 0x382fe000, 0x38300000, 0x38302000,
+0x38304000, 0x38306000, 0x38308000, 0x3830a000, 0x3830c000, 0x3830e000,
+0x38310000, 0x38312000, 0x38314000, 0x38316000, 0x38318000, 0x3831a000,
+0x3831c000, 0x3831e000, 0x38320000, 0x38322000, 0x38324000, 0x38326000,
+0x38328000, 0x3832a000, 0x3832c000, 0x3832e000, 0x38330000, 0x38332000,
+0x38334000, 0x38336000, 0x38338000, 0x3833a000, 0x3833c000, 0x3833e000,
+0x38340000, 0x38342000, 0x38344000, 0x38346000, 0x38348000, 0x3834a000,
+0x3834c000, 0x3834e000, 0x38350000, 0x38352000, 0x38354000, 0x38356000,
+0x38358000, 0x3835a000, 0x3835c000, 0x3835e000, 0x38360000, 0x38362000,
+0x38364000, 0x38366000, 0x38368000, 0x3836a000, 0x3836c000, 0x3836e000,
+0x38370000, 0x38372000, 0x38374000, 0x38376000, 0x38378000, 0x3837a000,
+0x3837c000, 0x3837e000, 0x38380000, 0x38382000, 0x38384000, 0x38386000,
+0x38388000, 0x3838a000, 0x3838c000, 0x3838e000, 0x38390000, 0x38392000,
+0x38394000, 0x38396000, 0x38398000, 0x3839a000, 0x3839c000, 0x3839e000,
+0x383a0000, 0x383a2000, 0x383a4000, 0x383a6000, 0x383a8000, 0x383aa000,
+0x383ac000, 0x383ae000, 0x383b0000, 0x383b2000, 0x383b4000, 0x383b6000,
+0x383b8000, 0x383ba000, 0x383bc000, 0x383be000, 0x383c0000, 0x383c2000,
+0x383c4000, 0x383c6000, 0x383c8000, 0x383ca000, 0x383cc000, 0x383ce000,
+0x383d0000, 0x383d2000, 0x383d4000, 0x383d6000, 0x383d8000, 0x383da000,
+0x383dc000, 0x383de000, 0x383e0000, 0x383e2000, 0x383e4000, 0x383e6000,
+0x383e8000, 0x383ea000, 0x383ec000, 0x383ee000, 0x383f0000, 0x383f2000,
+0x383f4000, 0x383f6000, 0x383f8000, 0x383fa000, 0x383fc000, 0x383fe000,
+0x38400000, 0x38402000, 0x38404000, 0x38406000, 0x38408000, 0x3840a000,
+0x3840c000, 0x3840e000, 0x38410000, 0x38412000, 0x38414000, 0x38416000,
+0x38418000, 0x3841a000, 0x3841c000, 0x3841e000, 0x38420000, 0x38422000,
+0x38424000, 0x38426000, 0x38428000, 0x3842a000, 0x3842c000, 0x3842e000,
+0x38430000, 0x38432000, 0x38434000, 0x38436000, 0x38438000, 0x3843a000,
+0x3843c000, 0x3843e000, 0x38440000, 0x38442000, 0x38444000, 0x38446000,
+0x38448000, 0x3844a000, 0x3844c000, 0x3844e000, 0x38450000, 0x38452000,
+0x38454000, 0x38456000, 0x38458000, 0x3845a000, 0x3845c000, 0x3845e000,
+0x38460000, 0x38462000, 0x38464000, 0x38466000, 0x38468000, 0x3846a000,
+0x3846c000, 0x3846e000, 0x38470000, 0x38472000, 0x38474000, 0x38476000,
+0x38478000, 0x3847a000, 0x3847c000, 0x3847e000, 0x38480000, 0x38482000,
+0x38484000, 0x38486000, 0x38488000, 0x3848a000, 0x3848c000, 0x3848e000,
+0x38490000, 0x38492000, 0x38494000, 0x38496000, 0x38498000, 0x3849a000,
+0x3849c000, 0x3849e000, 0x384a0000, 0x384a2000, 0x384a4000, 0x384a6000,
+0x384a8000, 0x384aa000, 0x384ac000, 0x384ae000, 0x384b0000, 0x384b2000,
+0x384b4000, 0x384b6000, 0x384b8000, 0x384ba000, 0x384bc000, 0x384be000,
+0x384c0000, 0x384c2000, 0x384c4000, 0x384c6000, 0x384c8000, 0x384ca000,
+0x384cc000, 0x384ce000, 0x384d0000, 0x384d2000, 0x384d4000, 0x384d6000,
+0x384d8000, 0x384da000, 0x384dc000, 0x384de000, 0x384e0000, 0x384e2000,
+0x384e4000, 0x384e6000, 0x384e8000, 0x384ea000, 0x384ec000, 0x384ee000,
+0x384f0000, 0x384f2000, 0x384f4000, 0x384f6000, 0x384f8000, 0x384fa000,
+0x384fc000, 0x384fe000, 0x38500000, 0x38502000, 0x38504000, 0x38506000,
+0x38508000, 0x3850a000, 0x3850c000, 0x3850e000, 0x38510000, 0x38512000,
+0x38514000, 0x38516000, 0x38518000, 0x3851a000, 0x3851c000, 0x3851e000,
+0x38520000, 0x38522000, 0x38524000, 0x38526000, 0x38528000, 0x3852a000,
+0x3852c000, 0x3852e000, 0x38530000, 0x38532000, 0x38534000, 0x38536000,
+0x38538000, 0x3853a000, 0x3853c000, 0x3853e000, 0x38540000, 0x38542000,
+0x38544000, 0x38546000, 0x38548000, 0x3854a000, 0x3854c000, 0x3854e000,
+0x38550000, 0x38552000, 0x38554000, 0x38556000, 0x38558000, 0x3855a000,
+0x3855c000, 0x3855e000, 0x38560000, 0x38562000, 0x38564000, 0x38566000,
+0x38568000, 0x3856a000, 0x3856c000, 0x3856e000, 0x38570000, 0x38572000,
+0x38574000, 0x38576000, 0x38578000, 0x3857a000, 0x3857c000, 0x3857e000,
+0x38580000, 0x38582000, 0x38584000, 0x38586000, 0x38588000, 0x3858a000,
+0x3858c000, 0x3858e000, 0x38590000, 0x38592000, 0x38594000, 0x38596000,
+0x38598000, 0x3859a000, 0x3859c000, 0x3859e000, 0x385a0000, 0x385a2000,
+0x385a4000, 0x385a6000, 0x385a8000, 0x385aa000, 0x385ac000, 0x385ae000,
+0x385b0000, 0x385b2000, 0x385b4000, 0x385b6000, 0x385b8000, 0x385ba000,
+0x385bc000, 0x385be000, 0x385c0000, 0x385c2000, 0x385c4000, 0x385c6000,
+0x385c8000, 0x385ca000, 0x385cc000, 0x385ce000, 0x385d0000, 0x385d2000,
+0x385d4000, 0x385d6000, 0x385d8000, 0x385da000, 0x385dc000, 0x385de000,
+0x385e0000, 0x385e2000, 0x385e4000, 0x385e6000, 0x385e8000, 0x385ea000,
+0x385ec000, 0x385ee000, 0x385f0000, 0x385f2000, 0x385f4000, 0x385f6000,
+0x385f8000, 0x385fa000, 0x385fc000, 0x385fe000, 0x38600000, 0x38602000,
+0x38604000, 0x38606000, 0x38608000, 0x3860a000, 0x3860c000, 0x3860e000,
+0x38610000, 0x38612000, 0x38614000, 0x38616000, 0x38618000, 0x3861a000,
+0x3861c000, 0x3861e000, 0x38620000, 0x38622000, 0x38624000, 0x38626000,
+0x38628000, 0x3862a000, 0x3862c000, 0x3862e000, 0x38630000, 0x38632000,
+0x38634000, 0x38636000, 0x38638000, 0x3863a000, 0x3863c000, 0x3863e000,
+0x38640000, 0x38642000, 0x38644000, 0x38646000, 0x38648000, 0x3864a000,
+0x3864c000, 0x3864e000, 0x38650000, 0x38652000, 0x38654000, 0x38656000,
+0x38658000, 0x3865a000, 0x3865c000, 0x3865e000, 0x38660000, 0x38662000,
+0x38664000, 0x38666000, 0x38668000, 0x3866a000, 0x3866c000, 0x3866e000,
+0x38670000, 0x38672000, 0x38674000, 0x38676000, 0x38678000, 0x3867a000,
+0x3867c000, 0x3867e000, 0x38680000, 0x38682000, 0x38684000, 0x38686000,
+0x38688000, 0x3868a000, 0x3868c000, 0x3868e000, 0x38690000, 0x38692000,
+0x38694000, 0x38696000, 0x38698000, 0x3869a000, 0x3869c000, 0x3869e000,
+0x386a0000, 0x386a2000, 0x386a4000, 0x386a6000, 0x386a8000, 0x386aa000,
+0x386ac000, 0x386ae000, 0x386b0000, 0x386b2000, 0x386b4000, 0x386b6000,
+0x386b8000, 0x386ba000, 0x386bc000, 0x386be000, 0x386c0000, 0x386c2000,
+0x386c4000, 0x386c6000, 0x386c8000, 0x386ca000, 0x386cc000, 0x386ce000,
+0x386d0000, 0x386d2000, 0x386d4000, 0x386d6000, 0x386d8000, 0x386da000,
+0x386dc000, 0x386de000, 0x386e0000, 0x386e2000, 0x386e4000, 0x386e6000,
+0x386e8000, 0x386ea000, 0x386ec000, 0x386ee000, 0x386f0000, 0x386f2000,
+0x386f4000, 0x386f6000, 0x386f8000, 0x386fa000, 0x386fc000, 0x386fe000,
+0x38700000, 0x38702000, 0x38704000, 0x38706000, 0x38708000, 0x3870a000,
+0x3870c000, 0x3870e000, 0x38710000, 0x38712000, 0x38714000, 0x38716000,
+0x38718000, 0x3871a000, 0x3871c000, 0x3871e000, 0x38720000, 0x38722000,
+0x38724000, 0x38726000, 0x38728000, 0x3872a000, 0x3872c000, 0x3872e000,
+0x38730000, 0x38732000, 0x38734000, 0x38736000, 0x38738000, 0x3873a000,
+0x3873c000, 0x3873e000, 0x38740000, 0x38742000, 0x38744000, 0x38746000,
+0x38748000, 0x3874a000, 0x3874c000, 0x3874e000, 0x38750000, 0x38752000,
+0x38754000, 0x38756000, 0x38758000, 0x3875a000, 0x3875c000, 0x3875e000,
+0x38760000, 0x38762000, 0x38764000, 0x38766000, 0x38768000, 0x3876a000,
+0x3876c000, 0x3876e000, 0x38770000, 0x38772000, 0x38774000, 0x38776000,
+0x38778000, 0x3877a000, 0x3877c000, 0x3877e000, 0x38780000, 0x38782000,
+0x38784000, 0x38786000, 0x38788000, 0x3878a000, 0x3878c000, 0x3878e000,
+0x38790000, 0x38792000, 0x38794000, 0x38796000, 0x38798000, 0x3879a000,
+0x3879c000, 0x3879e000, 0x387a0000, 0x387a2000, 0x387a4000, 0x387a6000,
+0x387a8000, 0x387aa000, 0x387ac000, 0x387ae000, 0x387b0000, 0x387b2000,
+0x387b4000, 0x387b6000, 0x387b8000, 0x387ba000, 0x387bc000, 0x387be000,
+0x387c0000, 0x387c2000, 0x387c4000, 0x387c6000, 0x387c8000, 0x387ca000,
+0x387cc000, 0x387ce000, 0x387d0000, 0x387d2000, 0x387d4000, 0x387d6000,
+0x387d8000, 0x387da000, 0x387dc000, 0x387de000, 0x387e0000, 0x387e2000,
+0x387e4000, 0x387e6000, 0x387e8000, 0x387ea000, 0x387ec000, 0x387ee000,
+0x387f0000, 0x387f2000, 0x387f4000, 0x387f6000, 0x387f8000, 0x387fa000,
+0x387fc000, 0x387fe000
+};
+
+static cmsUInt16Number Offset[64] = {
+0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0000, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
+0x0400, 0x0400, 0x0400, 0x0400
+};
+
+static cmsUInt32Number Exponent[64] = {
+0x00000000, 0x00800000, 0x01000000, 0x01800000, 0x02000000, 0x02800000,
+0x03000000, 0x03800000, 0x04000000, 0x04800000, 0x05000000, 0x05800000,
+0x06000000, 0x06800000, 0x07000000, 0x07800000, 0x08000000, 0x08800000,
+0x09000000, 0x09800000, 0x0a000000, 0x0a800000, 0x0b000000, 0x0b800000,
+0x0c000000, 0x0c800000, 0x0d000000, 0x0d800000, 0x0e000000, 0x0e800000,
+0x0f000000, 0x47800000, 0x80000000, 0x80800000, 0x81000000, 0x81800000,
+0x82000000, 0x82800000, 0x83000000, 0x83800000, 0x84000000, 0x84800000,
+0x85000000, 0x85800000, 0x86000000, 0x86800000, 0x87000000, 0x87800000,
+0x88000000, 0x88800000, 0x89000000, 0x89800000, 0x8a000000, 0x8a800000,
+0x8b000000, 0x8b800000, 0x8c000000, 0x8c800000, 0x8d000000, 0x8d800000,
+0x8e000000, 0x8e800000, 0x8f000000, 0xc7800000
+};
+
+static cmsUInt16Number Base[512] = {
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040,
+0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x0c00, 0x1000, 0x1400, 0x1800, 0x1c00,
+0x2000, 0x2400, 0x2800, 0x2c00, 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x4400,
+0x4800, 0x4c00, 0x5000, 0x5400, 0x5800, 0x5c00, 0x6000, 0x6400, 0x6800, 0x6c00,
+0x7000, 0x7400, 0x7800, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
+0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8001,
+0x8002, 0x8004, 0x8008, 0x8010, 0x8020, 0x8040, 0x8080, 0x8100, 0x8200, 0x8400,
+0x8800, 0x8c00, 0x9000, 0x9400, 0x9800, 0x9c00, 0xa000, 0xa400, 0xa800, 0xac00,
+0xb000, 0xb400, 0xb800, 0xbc00, 0xc000, 0xc400, 0xc800, 0xcc00, 0xd000, 0xd400,
+0xd800, 0xdc00, 0xe000, 0xe400, 0xe800, 0xec00, 0xf000, 0xf400, 0xf800, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
+0xfc00, 0xfc00
+};
+
+static cmsUInt8Number Shift[512] = {
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x17,
+0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d,
+0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
+0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0d, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13,
+0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
+0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
+0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+0x18, 0x18, 0x18, 0x18, 0x0d
+};
+
+cmsFloat32Number _cmsHalf2Float(cmsUInt16Number h)
+{
+ union {
+ cmsFloat32Number flt;
+ cmsUInt32Number num;
+ } out;
+
+ int n = h >> 10;
+
+ out.num = Mantissa[ (h & 0x3ff) + Offset[ n ] ] + Exponent[ n ];
+ return out.flt;
+}
+
+cmsUInt16Number _cmsFloat2Half(cmsFloat32Number flt)
+{
+ union {
+ cmsFloat32Number flt;
+ cmsUInt32Number num;
+ } in;
+
+ cmsUInt32Number n, j;
+
+ in.flt = flt;
+ n = in.num;
+ j = (n >> 23) & 0x1ff;
+
+ return (cmsUInt16Number) ((cmsUInt32Number) Base[ j ] + (( n & 0x007fffff) >> Shift[ j ]));
+}
+
+#endif
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsintrp.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsintrp.c
new file mode 100644
index 0000000000..5d5f35d3fc
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsintrp.c
@@ -0,0 +1,1506 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// This module incorporates several interpolation routines, for 1 to 8 channels on input and
+// up to 65535 channels on output. The user may change those by using the interpolation plug-in
+
+// Interpolation routines by default
+static cmsInterpFunction DefaultInterpolatorsFactory(cmsUInt32Number nInputChannels, cmsUInt32Number nOutputChannels, cmsUInt32Number dwFlags);
+
+// This is the default factory
+_cmsInterpPluginChunkType _cmsInterpPluginChunk = { NULL };
+
+// The interpolation plug-in memory chunk allocator/dup
+void _cmsAllocInterpPluginChunk(struct _cmsContext_struct* ctx, const struct _cmsContext_struct* src)
+{
+ void* from;
+
+ _cmsAssert(ctx != NULL);
+
+ if (src != NULL) {
+ from = src ->chunks[InterpPlugin];
+ }
+ else {
+ static _cmsInterpPluginChunkType InterpPluginChunk = { NULL };
+
+ from = &InterpPluginChunk;
+ }
+
+ _cmsAssert(from != NULL);
+ ctx ->chunks[InterpPlugin] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsInterpPluginChunkType));
+}
+
+
+// Main plug-in entry
+cmsBool _cmsRegisterInterpPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ cmsPluginInterpolation* Plugin = (cmsPluginInterpolation*) Data;
+ _cmsInterpPluginChunkType* ptr = (_cmsInterpPluginChunkType*) _cmsContextGetClientChunk(ContextID, InterpPlugin);
+
+ if (Data == NULL) {
+
+ ptr ->Interpolators = NULL;
+ return TRUE;
+ }
+
+ // Set replacement functions
+ ptr ->Interpolators = Plugin ->InterpolatorsFactory;
+ return TRUE;
+}
+
+
+// Set the interpolation method
+cmsBool _cmsSetInterpolationRoutine(cmsContext ContextID, cmsInterpParams* p)
+{
+ _cmsInterpPluginChunkType* ptr = (_cmsInterpPluginChunkType*) _cmsContextGetClientChunk(ContextID, InterpPlugin);
+
+ p ->Interpolation.Lerp16 = NULL;
+
+ // Invoke factory, possibly in the Plug-in
+ if (ptr ->Interpolators != NULL)
+ p ->Interpolation = ptr->Interpolators(p -> nInputs, p ->nOutputs, p ->dwFlags);
+
+ // If unsupported by the plug-in, go for the LittleCMS default.
+ // If happens only if an extern plug-in is being used
+ if (p ->Interpolation.Lerp16 == NULL)
+ p ->Interpolation = DefaultInterpolatorsFactory(p ->nInputs, p ->nOutputs, p ->dwFlags);
+
+ // Check for valid interpolator (we just check one member of the union)
+ if (p ->Interpolation.Lerp16 == NULL) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+// This function precalculates as many parameters as possible to speed up the interpolation.
+cmsInterpParams* _cmsComputeInterpParamsEx(cmsContext ContextID,
+ const cmsUInt32Number nSamples[],
+ int InputChan, int OutputChan,
+ const void *Table,
+ cmsUInt32Number dwFlags)
+{
+ cmsInterpParams* p;
+ int i;
+
+ // Check for maximum inputs
+ if (InputChan > MAX_INPUT_DIMENSIONS) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", InputChan, MAX_INPUT_DIMENSIONS);
+ return NULL;
+ }
+
+ // Creates an empty object
+ p = (cmsInterpParams*) _cmsMallocZero(ContextID, sizeof(cmsInterpParams));
+ if (p == NULL) return NULL;
+
+ // Keep original parameters
+ p -> dwFlags = dwFlags;
+ p -> nInputs = InputChan;
+ p -> nOutputs = OutputChan;
+ p ->Table = Table;
+ p ->ContextID = ContextID;
+
+ // Fill samples per input direction and domain (which is number of nodes minus one)
+ for (i=0; i < InputChan; i++) {
+
+ p -> nSamples[i] = nSamples[i];
+ p -> Domain[i] = nSamples[i] - 1;
+ }
+
+ // Compute factors to apply to each component to index the grid array
+ p -> opta[0] = p -> nOutputs;
+ for (i=1; i < InputChan; i++)
+ p ->opta[i] = p ->opta[i-1] * nSamples[InputChan-i];
+
+
+ if (!_cmsSetInterpolationRoutine(ContextID, p)) {
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported interpolation (%d->%d channels)", InputChan, OutputChan);
+ _cmsFree(ContextID, p);
+ return NULL;
+ }
+
+ // All seems ok
+ return p;
+}
+
+
+// This one is a wrapper on the anterior, but assuming all directions have same number of nodes
+cmsInterpParams* _cmsComputeInterpParams(cmsContext ContextID, int nSamples, int InputChan, int OutputChan, const void* Table, cmsUInt32Number dwFlags)
+{
+ int i;
+ cmsUInt32Number Samples[MAX_INPUT_DIMENSIONS];
+
+ // Fill the auxiliar array
+ for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
+ Samples[i] = nSamples;
+
+ // Call the extended function
+ return _cmsComputeInterpParamsEx(ContextID, Samples, InputChan, OutputChan, Table, dwFlags);
+}
+
+
+// Free all associated memory
+void _cmsFreeInterpParams(cmsInterpParams* p)
+{
+ if (p != NULL) _cmsFree(p ->ContextID, p);
+}
+
+
+// Inline fixed point interpolation
+cmsINLINE cmsUInt16Number LinearInterp(cmsS15Fixed16Number a, cmsS15Fixed16Number l, cmsS15Fixed16Number h)
+{
+ cmsUInt32Number dif = (cmsUInt32Number) (h - l) * a + 0x8000;
+ dif = (dif >> 16) + l;
+ return (cmsUInt16Number) (dif);
+}
+
+
+// Linear interpolation (Fixed-point optimized)
+static
+void LinLerp1D(register const cmsUInt16Number Value[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p)
+{
+ cmsUInt16Number y1, y0;
+ int cell0, rest;
+ int val3;
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p ->Table;
+
+ // if last value...
+ if (Value[0] == 0xffff) {
+
+ Output[0] = LutTable[p -> Domain[0]];
+ return;
+ }
+
+ val3 = p -> Domain[0] * Value[0];
+ val3 = _cmsToFixedDomain(val3); // To fixed 15.16
+
+ cell0 = FIXED_TO_INT(val3); // Cell is 16 MSB bits
+ rest = FIXED_REST_TO_INT(val3); // Rest is 16 LSB bits
+
+ y0 = LutTable[cell0];
+ y1 = LutTable[cell0+1];
+
+
+ Output[0] = LinearInterp(rest, y0, y1);
+}
+
+// To prevent out of bounds indexing
+cmsINLINE cmsFloat32Number fclamp(cmsFloat32Number v)
+{
+ return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v);
+}
+
+// Floating-point version of 1D interpolation
+static
+void LinLerp1Dfloat(const cmsFloat32Number Value[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ cmsFloat32Number y1, y0;
+ cmsFloat32Number val2, rest;
+ int cell0, cell1;
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p ->Table;
+
+ val2 = fclamp(Value[0]);
+
+ // if last value...
+ if (val2 == 1.0) {
+ Output[0] = LutTable[p -> Domain[0]];
+ return;
+ }
+
+ val2 *= p -> Domain[0];
+
+ cell0 = (int) floor(val2);
+ cell1 = (int) ceil(val2);
+
+ // Rest is 16 LSB bits
+ rest = val2 - cell0;
+
+ y0 = LutTable[cell0] ;
+ y1 = LutTable[cell1] ;
+
+ Output[0] = y0 + (y1 - y0) * rest;
+}
+
+
+
+// Eval gray LUT having only one input channel
+static
+void Eval1Input(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p16)
+{
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, k1, rk, K0, K1;
+ int v;
+ cmsUInt32Number OutChan;
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p16 -> Table;
+
+ v = Input[0] * p16 -> Domain[0];
+ fk = _cmsToFixedDomain(v);
+
+ k0 = FIXED_TO_INT(fk);
+ rk = (cmsUInt16Number) FIXED_REST_TO_INT(fk);
+
+ k1 = k0 + (Input[0] != 0xFFFFU ? 1 : 0);
+
+ K0 = p16 -> opta[0] * k0;
+ K1 = p16 -> opta[0] * k1;
+
+ for (OutChan=0; OutChan < p16->nOutputs; OutChan++) {
+
+ Output[OutChan] = LinearInterp(rk, LutTable[K0+OutChan], LutTable[K1+OutChan]);
+ }
+}
+
+
+
+// Eval gray LUT having only one input channel
+static
+void Eval1InputFloat(const cmsFloat32Number Value[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ cmsFloat32Number y1, y0;
+ cmsFloat32Number val2, rest;
+ int cell0, cell1;
+ cmsUInt32Number OutChan;
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p ->Table;
+
+ val2 = fclamp(Value[0]);
+
+ // if last value...
+ if (val2 == 1.0) {
+ Output[0] = LutTable[p -> Domain[0]];
+ return;
+ }
+
+ val2 *= p -> Domain[0];
+
+ cell0 = (int) floor(val2);
+ cell1 = (int) ceil(val2);
+
+ // Rest is 16 LSB bits
+ rest = val2 - cell0;
+
+ cell0 *= p -> opta[0];
+ cell1 *= p -> opta[0];
+
+ for (OutChan=0; OutChan < p->nOutputs; OutChan++) {
+
+ y0 = LutTable[cell0 + OutChan] ;
+ y1 = LutTable[cell1 + OutChan] ;
+
+ Output[OutChan] = y0 + (y1 - y0) * rest;
+ }
+}
+
+// Bilinear interpolation (16 bits) - cmsFloat32Number version
+static
+void BilinearInterpFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+
+{
+# define LERP(a,l,h) (cmsFloat32Number) ((l)+(((h)-(l))*(a)))
+# define DENS(i,j) (LutTable[(i)+(j)+OutChan])
+
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p ->Table;
+ cmsFloat32Number px, py;
+ int x0, y0,
+ X0, Y0, X1, Y1;
+ int TotalOut, OutChan;
+ cmsFloat32Number fx, fy,
+ d00, d01, d10, d11,
+ dx0, dx1,
+ dxy;
+
+ TotalOut = p -> nOutputs;
+ px = fclamp(Input[0]) * p->Domain[0];
+ py = fclamp(Input[1]) * p->Domain[1];
+
+ x0 = (int) _cmsQuickFloor(px); fx = px - (cmsFloat32Number) x0;
+ y0 = (int) _cmsQuickFloor(py); fy = py - (cmsFloat32Number) y0;
+
+ X0 = p -> opta[1] * x0;
+ X1 = X0 + (Input[0] >= 1.0 ? 0 : p->opta[1]);
+
+ Y0 = p -> opta[0] * y0;
+ Y1 = Y0 + (Input[1] >= 1.0 ? 0 : p->opta[0]);
+
+ for (OutChan = 0; OutChan < TotalOut; OutChan++) {
+
+ d00 = DENS(X0, Y0);
+ d01 = DENS(X0, Y1);
+ d10 = DENS(X1, Y0);
+ d11 = DENS(X1, Y1);
+
+ dx0 = LERP(fx, d00, d10);
+ dx1 = LERP(fx, d01, d11);
+
+ dxy = LERP(fy, dx0, dx1);
+
+ Output[OutChan] = dxy;
+ }
+
+
+# undef LERP
+# undef DENS
+}
+
+// Bilinear interpolation (16 bits) - optimized version
+static
+void BilinearInterp16(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p)
+
+{
+#define DENS(i,j) (LutTable[(i)+(j)+OutChan])
+#define LERP(a,l,h) (cmsUInt16Number) (l + ROUND_FIXED_TO_INT(((h-l)*a)))
+
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p ->Table;
+ int OutChan, TotalOut;
+ cmsS15Fixed16Number fx, fy;
+ register int rx, ry;
+ int x0, y0;
+ register int X0, X1, Y0, Y1;
+ int d00, d01, d10, d11,
+ dx0, dx1,
+ dxy;
+
+ TotalOut = p -> nOutputs;
+
+ fx = _cmsToFixedDomain((int) Input[0] * p -> Domain[0]);
+ x0 = FIXED_TO_INT(fx);
+ rx = FIXED_REST_TO_INT(fx); // Rest in 0..1.0 domain
+
+
+ fy = _cmsToFixedDomain((int) Input[1] * p -> Domain[1]);
+ y0 = FIXED_TO_INT(fy);
+ ry = FIXED_REST_TO_INT(fy);
+
+
+ X0 = p -> opta[1] * x0;
+ X1 = X0 + (Input[0] == 0xFFFFU ? 0 : p->opta[1]);
+
+ Y0 = p -> opta[0] * y0;
+ Y1 = Y0 + (Input[1] == 0xFFFFU ? 0 : p->opta[0]);
+
+ for (OutChan = 0; OutChan < TotalOut; OutChan++) {
+
+ d00 = DENS(X0, Y0);
+ d01 = DENS(X0, Y1);
+ d10 = DENS(X1, Y0);
+ d11 = DENS(X1, Y1);
+
+ dx0 = LERP(rx, d00, d10);
+ dx1 = LERP(rx, d01, d11);
+
+ dxy = LERP(ry, dx0, dx1);
+
+ Output[OutChan] = (cmsUInt16Number) dxy;
+ }
+
+
+# undef LERP
+# undef DENS
+}
+
+
+// Trilinear interpolation (16 bits) - cmsFloat32Number version
+static
+void TrilinearInterpFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+
+{
+# define LERP(a,l,h) (cmsFloat32Number) ((l)+(((h)-(l))*(a)))
+# define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
+
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p ->Table;
+ cmsFloat32Number px, py, pz;
+ int x0, y0, z0,
+ X0, Y0, Z0, X1, Y1, Z1;
+ int TotalOut, OutChan;
+ cmsFloat32Number fx, fy, fz,
+ d000, d001, d010, d011,
+ d100, d101, d110, d111,
+ dx00, dx01, dx10, dx11,
+ dxy0, dxy1, dxyz;
+
+ TotalOut = p -> nOutputs;
+
+ // We need some clipping here
+ px = fclamp(Input[0]) * p->Domain[0];
+ py = fclamp(Input[1]) * p->Domain[1];
+ pz = fclamp(Input[2]) * p->Domain[2];
+
+ x0 = (int) _cmsQuickFloor(px); fx = px - (cmsFloat32Number) x0;
+ y0 = (int) _cmsQuickFloor(py); fy = py - (cmsFloat32Number) y0;
+ z0 = (int) _cmsQuickFloor(pz); fz = pz - (cmsFloat32Number) z0;
+
+ X0 = p -> opta[2] * x0;
+ X1 = X0 + (Input[0] >= 1.0 ? 0 : p->opta[2]);
+
+ Y0 = p -> opta[1] * y0;
+ Y1 = Y0 + (Input[1] >= 1.0 ? 0 : p->opta[1]);
+
+ Z0 = p -> opta[0] * z0;
+ Z1 = Z0 + (Input[2] >= 1.0 ? 0 : p->opta[0]);
+
+ for (OutChan = 0; OutChan < TotalOut; OutChan++) {
+
+ d000 = DENS(X0, Y0, Z0);
+ d001 = DENS(X0, Y0, Z1);
+ d010 = DENS(X0, Y1, Z0);
+ d011 = DENS(X0, Y1, Z1);
+
+ d100 = DENS(X1, Y0, Z0);
+ d101 = DENS(X1, Y0, Z1);
+ d110 = DENS(X1, Y1, Z0);
+ d111 = DENS(X1, Y1, Z1);
+
+
+ dx00 = LERP(fx, d000, d100);
+ dx01 = LERP(fx, d001, d101);
+ dx10 = LERP(fx, d010, d110);
+ dx11 = LERP(fx, d011, d111);
+
+ dxy0 = LERP(fy, dx00, dx10);
+ dxy1 = LERP(fy, dx01, dx11);
+
+ dxyz = LERP(fz, dxy0, dxy1);
+
+ Output[OutChan] = dxyz;
+ }
+
+
+# undef LERP
+# undef DENS
+}
+
+// Trilinear interpolation (16 bits) - optimized version
+static
+void TrilinearInterp16(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p)
+
+{
+#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
+#define LERP(a,l,h) (cmsUInt16Number) (l + ROUND_FIXED_TO_INT(((h-l)*a)))
+
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p ->Table;
+ int OutChan, TotalOut;
+ cmsS15Fixed16Number fx, fy, fz;
+ register int rx, ry, rz;
+ int x0, y0, z0;
+ register int X0, X1, Y0, Y1, Z0, Z1;
+ int d000, d001, d010, d011,
+ d100, d101, d110, d111,
+ dx00, dx01, dx10, dx11,
+ dxy0, dxy1, dxyz;
+
+ TotalOut = p -> nOutputs;
+
+ fx = _cmsToFixedDomain((int) Input[0] * p -> Domain[0]);
+ x0 = FIXED_TO_INT(fx);
+ rx = FIXED_REST_TO_INT(fx); // Rest in 0..1.0 domain
+
+
+ fy = _cmsToFixedDomain((int) Input[1] * p -> Domain[1]);
+ y0 = FIXED_TO_INT(fy);
+ ry = FIXED_REST_TO_INT(fy);
+
+ fz = _cmsToFixedDomain((int) Input[2] * p -> Domain[2]);
+ z0 = FIXED_TO_INT(fz);
+ rz = FIXED_REST_TO_INT(fz);
+
+
+ X0 = p -> opta[2] * x0;
+ X1 = X0 + (Input[0] == 0xFFFFU ? 0 : p->opta[2]);
+
+ Y0 = p -> opta[1] * y0;
+ Y1 = Y0 + (Input[1] == 0xFFFFU ? 0 : p->opta[1]);
+
+ Z0 = p -> opta[0] * z0;
+ Z1 = Z0 + (Input[2] == 0xFFFFU ? 0 : p->opta[0]);
+
+ for (OutChan = 0; OutChan < TotalOut; OutChan++) {
+
+ d000 = DENS(X0, Y0, Z0);
+ d001 = DENS(X0, Y0, Z1);
+ d010 = DENS(X0, Y1, Z0);
+ d011 = DENS(X0, Y1, Z1);
+
+ d100 = DENS(X1, Y0, Z0);
+ d101 = DENS(X1, Y0, Z1);
+ d110 = DENS(X1, Y1, Z0);
+ d111 = DENS(X1, Y1, Z1);
+
+
+ dx00 = LERP(rx, d000, d100);
+ dx01 = LERP(rx, d001, d101);
+ dx10 = LERP(rx, d010, d110);
+ dx11 = LERP(rx, d011, d111);
+
+ dxy0 = LERP(ry, dx00, dx10);
+ dxy1 = LERP(ry, dx01, dx11);
+
+ dxyz = LERP(rz, dxy0, dxy1);
+
+ Output[OutChan] = (cmsUInt16Number) dxyz;
+ }
+
+
+# undef LERP
+# undef DENS
+}
+
+
+// Tetrahedral interpolation, using Sakamoto algorithm.
+#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
+static
+void TetrahedralInterpFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number px, py, pz;
+ int x0, y0, z0,
+ X0, Y0, Z0, X1, Y1, Z1;
+ cmsFloat32Number rx, ry, rz;
+ cmsFloat32Number c0, c1=0, c2=0, c3=0;
+ int OutChan, TotalOut;
+
+ TotalOut = p -> nOutputs;
+
+ // We need some clipping here
+ px = fclamp(Input[0]) * p->Domain[0];
+ py = fclamp(Input[1]) * p->Domain[1];
+ pz = fclamp(Input[2]) * p->Domain[2];
+
+ x0 = (int) _cmsQuickFloor(px); rx = (px - (cmsFloat32Number) x0);
+ y0 = (int) _cmsQuickFloor(py); ry = (py - (cmsFloat32Number) y0);
+ z0 = (int) _cmsQuickFloor(pz); rz = (pz - (cmsFloat32Number) z0);
+
+
+ X0 = p -> opta[2] * x0;
+ X1 = X0 + (Input[0] >= 1.0 ? 0 : p->opta[2]);
+
+ Y0 = p -> opta[1] * y0;
+ Y1 = Y0 + (Input[1] >= 1.0 ? 0 : p->opta[1]);
+
+ Z0 = p -> opta[0] * z0;
+ Z1 = Z0 + (Input[2] >= 1.0 ? 0 : p->opta[0]);
+
+ for (OutChan=0; OutChan < TotalOut; OutChan++) {
+
+ // These are the 6 Tetrahedral
+
+ c0 = DENS(X0, Y0, Z0);
+
+ if (rx >= ry && ry >= rz) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (rx >= rz && rz >= ry) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
+
+ }
+ else
+ if (rz >= rx && rx >= ry) {
+
+ c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else
+ if (ry >= rx && rx >= rz) {
+
+ c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (ry >= rz && rz >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
+
+ }
+ else
+ if (rz >= ry && ry >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else {
+ c1 = c2 = c3 = 0;
+ }
+
+ Output[OutChan] = c0 + c1 * rx + c2 * ry + c3 * rz;
+ }
+
+}
+
+#undef DENS
+
+
+
+
+static
+void TetrahedralInterp16(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p)
+{
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p -> Table;
+ cmsS15Fixed16Number fx, fy, fz;
+ cmsS15Fixed16Number rx, ry, rz;
+ int x0, y0, z0;
+ cmsS15Fixed16Number c0, c1, c2, c3, Rest;
+ cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
+ cmsUInt32Number TotalOut = p -> nOutputs;
+
+ fx = _cmsToFixedDomain((int) Input[0] * p -> Domain[0]);
+ fy = _cmsToFixedDomain((int) Input[1] * p -> Domain[1]);
+ fz = _cmsToFixedDomain((int) Input[2] * p -> Domain[2]);
+
+ x0 = FIXED_TO_INT(fx);
+ y0 = FIXED_TO_INT(fy);
+ z0 = FIXED_TO_INT(fz);
+
+ rx = FIXED_REST_TO_INT(fx);
+ ry = FIXED_REST_TO_INT(fy);
+ rz = FIXED_REST_TO_INT(fz);
+
+ X0 = p -> opta[2] * x0;
+ X1 = (Input[0] == 0xFFFFU ? 0 : p->opta[2]);
+
+ Y0 = p -> opta[1] * y0;
+ Y1 = (Input[1] == 0xFFFFU ? 0 : p->opta[1]);
+
+ Z0 = p -> opta[0] * z0;
+ Z1 = (Input[2] == 0xFFFFU ? 0 : p->opta[0]);
+
+ LutTable = &LutTable[X0+Y0+Z0];
+
+ // Output should be computed as x = ROUND_FIXED_TO_INT(_cmsToFixedDomain(Rest))
+ // which expands as: x = (Rest + ((Rest+0x7fff)/0xFFFF) + 0x8000)>>16
+ // This can be replaced by: t = Rest+0x8001, x = (t + (t>>16))>>16
+ // at the cost of being off by one at 7fff and 17ffe.
+
+ if (rx >= ry) {
+ if (ry >= rz) {
+ Y1 += X1;
+ Z1 += Y1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c3 -= c2;
+ c2 -= c1;
+ c1 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ } else if (rz >= rx) {
+ X1 += Z1;
+ Y1 += X1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c2 -= c1;
+ c1 -= c3;
+ c3 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ } else {
+ Z1 += X1;
+ Y1 += Z1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c2 -= c3;
+ c3 -= c1;
+ c1 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ }
+ } else {
+ if (rx >= rz) {
+ X1 += Y1;
+ Z1 += X1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c3 -= c1;
+ c1 -= c2;
+ c2 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ } else if (ry >= rz) {
+ Z1 += Y1;
+ X1 += Z1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c1 -= c3;
+ c3 -= c2;
+ c2 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ } else {
+ Y1 += Z1;
+ X1 += Y1;
+ for (; TotalOut; TotalOut--) {
+ c1 = LutTable[X1];
+ c2 = LutTable[Y1];
+ c3 = LutTable[Z1];
+ c0 = *LutTable++;
+ c1 -= c2;
+ c2 -= c3;
+ c3 -= c0;
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ *Output++ = (cmsUInt16Number) c0 + ((Rest + (Rest>>16))>>16);
+ }
+ }
+ }
+}
+
+
+#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
+static
+void Eval4Inputs(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p16)
+{
+ const cmsUInt16Number* LutTable;
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, rk;
+ int K0, K1;
+ cmsS15Fixed16Number fx, fy, fz;
+ cmsS15Fixed16Number rx, ry, rz;
+ int x0, y0, z0;
+ cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
+ cmsUInt32Number i;
+ cmsS15Fixed16Number c0, c1, c2, c3, Rest;
+ cmsUInt32Number OutChan;
+ cmsUInt16Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+
+
+ fk = _cmsToFixedDomain((int) Input[0] * p16 -> Domain[0]);
+ fx = _cmsToFixedDomain((int) Input[1] * p16 -> Domain[1]);
+ fy = _cmsToFixedDomain((int) Input[2] * p16 -> Domain[2]);
+ fz = _cmsToFixedDomain((int) Input[3] * p16 -> Domain[3]);
+
+ k0 = FIXED_TO_INT(fk);
+ x0 = FIXED_TO_INT(fx);
+ y0 = FIXED_TO_INT(fy);
+ z0 = FIXED_TO_INT(fz);
+
+ rk = FIXED_REST_TO_INT(fk);
+ rx = FIXED_REST_TO_INT(fx);
+ ry = FIXED_REST_TO_INT(fy);
+ rz = FIXED_REST_TO_INT(fz);
+
+ K0 = p16 -> opta[3] * k0;
+ K1 = K0 + (Input[0] == 0xFFFFU ? 0 : p16->opta[3]);
+
+ X0 = p16 -> opta[2] * x0;
+ X1 = X0 + (Input[1] == 0xFFFFU ? 0 : p16->opta[2]);
+
+ Y0 = p16 -> opta[1] * y0;
+ Y1 = Y0 + (Input[2] == 0xFFFFU ? 0 : p16->opta[1]);
+
+ Z0 = p16 -> opta[0] * z0;
+ Z1 = Z0 + (Input[3] == 0xFFFFU ? 0 : p16->opta[0]);
+
+ LutTable = (cmsUInt16Number*) p16 -> Table;
+ LutTable += K0;
+
+ for (OutChan=0; OutChan < p16 -> nOutputs; OutChan++) {
+
+ c0 = DENS(X0, Y0, Z0);
+
+ if (rx >= ry && ry >= rz) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (rx >= rz && rz >= ry) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
+
+ }
+ else
+ if (rz >= rx && rx >= ry) {
+
+ c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else
+ if (ry >= rx && rx >= rz) {
+
+ c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (ry >= rz && rz >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
+
+ }
+ else
+ if (rz >= ry && ry >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else {
+ c1 = c2 = c3 = 0;
+ }
+
+ Rest = c1 * rx + c2 * ry + c3 * rz;
+
+ Tmp1[OutChan] = (cmsUInt16Number) c0 + ROUND_FIXED_TO_INT(_cmsToFixedDomain(Rest));
+ }
+
+
+ LutTable = (cmsUInt16Number*) p16 -> Table;
+ LutTable += K1;
+
+ for (OutChan=0; OutChan < p16 -> nOutputs; OutChan++) {
+
+ c0 = DENS(X0, Y0, Z0);
+
+ if (rx >= ry && ry >= rz) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (rx >= rz && rz >= ry) {
+
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
+
+ }
+ else
+ if (rz >= rx && rx >= ry) {
+
+ c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else
+ if (ry >= rx && rx >= rz) {
+
+ c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+
+ }
+ else
+ if (ry >= rz && rz >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
+
+ }
+ else
+ if (rz >= ry && ry >= rx) {
+
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+
+ }
+ else {
+ c1 = c2 = c3 = 0;
+ }
+
+ Rest = c1 * rx + c2 * ry + c3 * rz;
+
+ Tmp2[OutChan] = (cmsUInt16Number) c0 + ROUND_FIXED_TO_INT(_cmsToFixedDomain(Rest));
+ }
+
+
+
+ for (i=0; i < p16 -> nOutputs; i++) {
+ Output[i] = LinearInterp(rk, Tmp1[i], Tmp2[i]);
+ }
+}
+#undef DENS
+
+
+// For more that 3 inputs (i.e., CMYK)
+// evaluate two 3-dimensional interpolations and then linearly interpolate between them.
+
+
+static
+void Eval4InputsFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number rest;
+ cmsFloat32Number pk;
+ int k0, K0, K1;
+ const cmsFloat32Number* T;
+ cmsUInt32Number i;
+ cmsFloat32Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ pk = fclamp(Input[0]) * p->Domain[0];
+ k0 = _cmsQuickFloor(pk);
+ rest = pk - (cmsFloat32Number) k0;
+
+ K0 = p -> opta[3] * k0;
+ K1 = K0 + (Input[0] >= 1.0 ? 0 : p->opta[3]);
+
+ p1 = *p;
+ memmove(&p1.Domain[0], &p ->Domain[1], 3*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ TetrahedralInterpFloat(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+ TetrahedralInterpFloat(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p -> nOutputs; i++)
+ {
+ cmsFloat32Number y0 = Tmp1[i];
+ cmsFloat32Number y1 = Tmp2[i];
+
+ Output[i] = y0 + (y1 - y0) * rest;
+ }
+}
+
+
+static
+void Eval5Inputs(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+
+ register const cmsInterpParams* p16)
+{
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p16 -> Table;
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, rk;
+ int K0, K1;
+ const cmsUInt16Number* T;
+ cmsUInt32Number i;
+ cmsUInt16Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+
+ fk = _cmsToFixedDomain((cmsS15Fixed16Number) Input[0] * p16 -> Domain[0]);
+ k0 = FIXED_TO_INT(fk);
+ rk = FIXED_REST_TO_INT(fk);
+
+ K0 = p16 -> opta[4] * k0;
+ K1 = p16 -> opta[4] * (k0 + (Input[0] != 0xFFFFU ? 1 : 0));
+
+ p1 = *p16;
+ memmove(&p1.Domain[0], &p16 ->Domain[1], 4*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval4Inputs(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval4Inputs(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p16 -> nOutputs; i++) {
+
+ Output[i] = LinearInterp(rk, Tmp1[i], Tmp2[i]);
+ }
+
+}
+
+
+static
+void Eval5InputsFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number rest;
+ cmsFloat32Number pk;
+ int k0, K0, K1;
+ const cmsFloat32Number* T;
+ cmsUInt32Number i;
+ cmsFloat32Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ pk = fclamp(Input[0]) * p->Domain[0];
+ k0 = _cmsQuickFloor(pk);
+ rest = pk - (cmsFloat32Number) k0;
+
+ K0 = p -> opta[4] * k0;
+ K1 = K0 + (Input[0] >= 1.0 ? 0 : p->opta[4]);
+
+ p1 = *p;
+ memmove(&p1.Domain[0], &p ->Domain[1], 4*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval4InputsFloat(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval4InputsFloat(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p -> nOutputs; i++) {
+
+ cmsFloat32Number y0 = Tmp1[i];
+ cmsFloat32Number y1 = Tmp2[i];
+
+ Output[i] = y0 + (y1 - y0) * rest;
+ }
+}
+
+
+
+static
+void Eval6Inputs(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p16)
+{
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p16 -> Table;
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, rk;
+ int K0, K1;
+ const cmsUInt16Number* T;
+ cmsUInt32Number i;
+ cmsUInt16Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ fk = _cmsToFixedDomain((cmsS15Fixed16Number) Input[0] * p16 -> Domain[0]);
+ k0 = FIXED_TO_INT(fk);
+ rk = FIXED_REST_TO_INT(fk);
+
+ K0 = p16 -> opta[5] * k0;
+ K1 = p16 -> opta[5] * (k0 + (Input[0] != 0xFFFFU ? 1 : 0));
+
+ p1 = *p16;
+ memmove(&p1.Domain[0], &p16 ->Domain[1], 5*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval5Inputs(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval5Inputs(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p16 -> nOutputs; i++) {
+
+ Output[i] = LinearInterp(rk, Tmp1[i], Tmp2[i]);
+ }
+
+}
+
+
+static
+void Eval6InputsFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number rest;
+ cmsFloat32Number pk;
+ int k0, K0, K1;
+ const cmsFloat32Number* T;
+ cmsUInt32Number i;
+ cmsFloat32Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ pk = fclamp(Input[0]) * p->Domain[0];
+ k0 = _cmsQuickFloor(pk);
+ rest = pk - (cmsFloat32Number) k0;
+
+ K0 = p -> opta[5] * k0;
+ K1 = K0 + (Input[0] >= 1.0 ? 0 : p->opta[5]);
+
+ p1 = *p;
+ memmove(&p1.Domain[0], &p ->Domain[1], 5*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval5InputsFloat(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval5InputsFloat(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p -> nOutputs; i++) {
+
+ cmsFloat32Number y0 = Tmp1[i];
+ cmsFloat32Number y1 = Tmp2[i];
+
+ Output[i] = y0 + (y1 - y0) * rest;
+ }
+}
+
+
+static
+void Eval7Inputs(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p16)
+{
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p16 -> Table;
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, rk;
+ int K0, K1;
+ const cmsUInt16Number* T;
+ cmsUInt32Number i;
+ cmsUInt16Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+
+ fk = _cmsToFixedDomain((cmsS15Fixed16Number) Input[0] * p16 -> Domain[0]);
+ k0 = FIXED_TO_INT(fk);
+ rk = FIXED_REST_TO_INT(fk);
+
+ K0 = p16 -> opta[6] * k0;
+ K1 = p16 -> opta[6] * (k0 + (Input[0] != 0xFFFFU ? 1 : 0));
+
+ p1 = *p16;
+ memmove(&p1.Domain[0], &p16 ->Domain[1], 6*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval6Inputs(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval6Inputs(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p16 -> nOutputs; i++) {
+ Output[i] = LinearInterp(rk, Tmp1[i], Tmp2[i]);
+ }
+}
+
+
+static
+void Eval7InputsFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number rest;
+ cmsFloat32Number pk;
+ int k0, K0, K1;
+ const cmsFloat32Number* T;
+ cmsUInt32Number i;
+ cmsFloat32Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ pk = fclamp(Input[0]) * p->Domain[0];
+ k0 = _cmsQuickFloor(pk);
+ rest = pk - (cmsFloat32Number) k0;
+
+ K0 = p -> opta[6] * k0;
+ K1 = K0 + (Input[0] >= 1.0 ? 0 : p->opta[6]);
+
+ p1 = *p;
+ memmove(&p1.Domain[0], &p ->Domain[1], 6*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval6InputsFloat(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval6InputsFloat(Input + 1, Tmp2, &p1);
+
+
+ for (i=0; i < p -> nOutputs; i++) {
+
+ cmsFloat32Number y0 = Tmp1[i];
+ cmsFloat32Number y1 = Tmp2[i];
+
+ Output[i] = y0 + (y1 - y0) * rest;
+
+ }
+}
+
+static
+void Eval8Inputs(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const cmsInterpParams* p16)
+{
+ const cmsUInt16Number* LutTable = (cmsUInt16Number*) p16 -> Table;
+ cmsS15Fixed16Number fk;
+ cmsS15Fixed16Number k0, rk;
+ int K0, K1;
+ const cmsUInt16Number* T;
+ cmsUInt32Number i;
+ cmsUInt16Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ fk = _cmsToFixedDomain((cmsS15Fixed16Number) Input[0] * p16 -> Domain[0]);
+ k0 = FIXED_TO_INT(fk);
+ rk = FIXED_REST_TO_INT(fk);
+
+ K0 = p16 -> opta[7] * k0;
+ K1 = p16 -> opta[7] * (k0 + (Input[0] != 0xFFFFU ? 1 : 0));
+
+ p1 = *p16;
+ memmove(&p1.Domain[0], &p16 ->Domain[1], 7*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval7Inputs(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+ Eval7Inputs(Input + 1, Tmp2, &p1);
+
+ for (i=0; i < p16 -> nOutputs; i++) {
+ Output[i] = LinearInterp(rk, Tmp1[i], Tmp2[i]);
+ }
+}
+
+
+
+static
+void Eval8InputsFloat(const cmsFloat32Number Input[],
+ cmsFloat32Number Output[],
+ const cmsInterpParams* p)
+{
+ const cmsFloat32Number* LutTable = (cmsFloat32Number*) p -> Table;
+ cmsFloat32Number rest;
+ cmsFloat32Number pk;
+ int k0, K0, K1;
+ const cmsFloat32Number* T;
+ cmsUInt32Number i;
+ cmsFloat32Number Tmp1[MAX_STAGE_CHANNELS], Tmp2[MAX_STAGE_CHANNELS];
+ cmsInterpParams p1;
+
+ pk = fclamp(Input[0]) * p->Domain[0];
+ k0 = _cmsQuickFloor(pk);
+ rest = pk - (cmsFloat32Number) k0;
+
+ K0 = p -> opta[7] * k0;
+ K1 = K0 + (Input[0] >= 1.0 ? 0 : p->opta[7]);
+
+ p1 = *p;
+ memmove(&p1.Domain[0], &p ->Domain[1], 7*sizeof(cmsUInt32Number));
+
+ T = LutTable + K0;
+ p1.Table = T;
+
+ Eval7InputsFloat(Input + 1, Tmp1, &p1);
+
+ T = LutTable + K1;
+ p1.Table = T;
+
+ Eval7InputsFloat(Input + 1, Tmp2, &p1);
+
+
+ for (i=0; i < p -> nOutputs; i++) {
+
+ cmsFloat32Number y0 = Tmp1[i];
+ cmsFloat32Number y1 = Tmp2[i];
+
+ Output[i] = y0 + (y1 - y0) * rest;
+ }
+}
+
+// The default factory
+static
+cmsInterpFunction DefaultInterpolatorsFactory(cmsUInt32Number nInputChannels, cmsUInt32Number nOutputChannels, cmsUInt32Number dwFlags)
+{
+
+ cmsInterpFunction Interpolation;
+ cmsBool IsFloat = (dwFlags & CMS_LERP_FLAGS_FLOAT);
+ cmsBool IsTrilinear = (dwFlags & CMS_LERP_FLAGS_TRILINEAR);
+
+ memset(&Interpolation, 0, sizeof(Interpolation));
+
+ // Safety check
+ if (nInputChannels >= 4 && nOutputChannels >= MAX_STAGE_CHANNELS)
+ return Interpolation;
+
+ switch (nInputChannels) {
+
+ case 1: // Gray LUT / linear
+
+ if (nOutputChannels == 1) {
+
+ if (IsFloat)
+ Interpolation.LerpFloat = LinLerp1Dfloat;
+ else
+ Interpolation.Lerp16 = LinLerp1D;
+
+ }
+ else {
+
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval1InputFloat;
+ else
+ Interpolation.Lerp16 = Eval1Input;
+ }
+ break;
+
+ case 2: // Duotone
+ if (IsFloat)
+ Interpolation.LerpFloat = BilinearInterpFloat;
+ else
+ Interpolation.Lerp16 = BilinearInterp16;
+ break;
+
+ case 3: // RGB et al
+
+ if (IsTrilinear) {
+
+ if (IsFloat)
+ Interpolation.LerpFloat = TrilinearInterpFloat;
+ else
+ Interpolation.Lerp16 = TrilinearInterp16;
+ }
+ else {
+
+ if (IsFloat)
+ Interpolation.LerpFloat = TetrahedralInterpFloat;
+ else {
+
+ Interpolation.Lerp16 = TetrahedralInterp16;
+ }
+ }
+ break;
+
+ case 4: // CMYK lut
+
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval4InputsFloat;
+ else
+ Interpolation.Lerp16 = Eval4Inputs;
+ break;
+
+ case 5: // 5 Inks
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval5InputsFloat;
+ else
+ Interpolation.Lerp16 = Eval5Inputs;
+ break;
+
+ case 6: // 6 Inks
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval6InputsFloat;
+ else
+ Interpolation.Lerp16 = Eval6Inputs;
+ break;
+
+ case 7: // 7 inks
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval7InputsFloat;
+ else
+ Interpolation.Lerp16 = Eval7Inputs;
+ break;
+
+ case 8: // 8 inks
+ if (IsFloat)
+ Interpolation.LerpFloat = Eval8InputsFloat;
+ else
+ Interpolation.Lerp16 = Eval8Inputs;
+ break;
+
+ break;
+
+ default:
+ Interpolation.Lerp16 = NULL;
+ }
+
+ return Interpolation;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio0.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio0.c
new file mode 100644
index 0000000000..6549d15d9e
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio0.c
@@ -0,0 +1,1883 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// Generic I/O, tag dictionary management, profile struct
+
+// IOhandlers are abstractions used by littleCMS to read from whatever file, stream,
+// memory block or any storage. Each IOhandler provides implementations for read,
+// write, seek and tell functions. LittleCMS code deals with IO across those objects.
+// In this way, is easier to add support for new storage media.
+
+// NULL stream, for taking care of used space -------------------------------------
+
+// NULL IOhandler basically does nothing but keep track on how many bytes have been
+// written. This is handy when creating profiles, where the file size is needed in the
+// header. Then, whole profile is serialized across NULL IOhandler and a second pass
+// writes the bytes to the pertinent IOhandler.
+
+typedef struct {
+ cmsUInt32Number Pointer; // Points to current location
+} FILENULL;
+
+static
+cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
+{
+ FILENULL* ResData = (FILENULL*) iohandler ->stream;
+
+ cmsUInt32Number len = size * count;
+ ResData -> Pointer += len;
+ return count;
+
+ cmsUNUSED_PARAMETER(Buffer);
+}
+
+static
+cmsBool NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
+{
+ FILENULL* ResData = (FILENULL*) iohandler ->stream;
+
+ ResData ->Pointer = offset;
+ return TRUE;
+}
+
+static
+cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler)
+{
+ FILENULL* ResData = (FILENULL*) iohandler ->stream;
+ return ResData -> Pointer;
+}
+
+static
+cmsBool NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr)
+{
+ FILENULL* ResData = (FILENULL*) iohandler ->stream;
+
+ ResData ->Pointer += size;
+ if (ResData ->Pointer > iohandler->UsedSpace)
+ iohandler->UsedSpace = ResData ->Pointer;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(Ptr);
+}
+
+static
+cmsBool NULLClose(cmsIOHANDLER* iohandler)
+{
+ FILENULL* ResData = (FILENULL*) iohandler ->stream;
+
+ _cmsFree(iohandler ->ContextID, ResData);
+ _cmsFree(iohandler ->ContextID, iohandler);
+ return TRUE;
+}
+
+// The NULL IOhandler creator
+cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID)
+{
+ struct _cms_io_handler* iohandler = NULL;
+ FILENULL* fm = NULL;
+
+ iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler));
+ if (iohandler == NULL) return NULL;
+
+ fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL));
+ if (fm == NULL) goto Error;
+
+ fm ->Pointer = 0;
+
+ iohandler ->ContextID = ContextID;
+ iohandler ->stream = (void*) fm;
+ iohandler ->UsedSpace = 0;
+ iohandler ->ReportedSize = 0;
+ iohandler ->PhysicalFile[0] = 0;
+
+ iohandler ->Read = NULLRead;
+ iohandler ->Seek = NULLSeek;
+ iohandler ->Close = NULLClose;
+ iohandler ->Tell = NULLTell;
+ iohandler ->Write = NULLWrite;
+
+ return iohandler;
+
+Error:
+ if (iohandler) _cmsFree(ContextID, iohandler);
+ return NULL;
+
+}
+
+
+// Memory-based stream --------------------------------------------------------------
+
+// Those functions implements an iohandler which takes a block of memory as storage medium.
+
+typedef struct {
+ cmsUInt8Number* Block; // Points to allocated memory
+ cmsUInt32Number Size; // Size of allocated memory
+ cmsUInt32Number Pointer; // Points to current location
+ int FreeBlockOnClose; // As title
+
+} FILEMEM;
+
+static
+cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
+{
+ FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
+ cmsUInt8Number* Ptr;
+ cmsUInt32Number len = size * count;
+
+ if (ResData -> Pointer + len > ResData -> Size){
+
+ len = (ResData -> Size - ResData -> Pointer);
+ cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size);
+ return 0;
+ }
+
+ Ptr = ResData -> Block;
+ Ptr += ResData -> Pointer;
+ memmove(Buffer, Ptr, len);
+ ResData -> Pointer += len;
+
+ return count;
+}
+
+// SEEK_CUR is assumed
+static
+cmsBool MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset)
+{
+ FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
+
+ if (offset > ResData ->Size) {
+ cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK, "Too few data; probably corrupted profile");
+ return FALSE;
+ }
+
+ ResData ->Pointer = offset;
+ return TRUE;
+}
+
+// Tell for memory
+static
+cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler)
+{
+ FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
+
+ if (ResData == NULL) return 0;
+ return ResData -> Pointer;
+}
+
+
+// Writes data to memory, also keeps used space for further reference.
+static
+cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr)
+{
+ FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
+
+ if (ResData == NULL) return FALSE; // Housekeeping
+
+ // Check for available space. Clip.
+ if (ResData->Pointer + size > ResData->Size) {
+ size = ResData ->Size - ResData->Pointer;
+ }
+
+ if (size == 0) return TRUE; // Write zero bytes is ok, but does nothing
+
+ memmove(ResData ->Block + ResData ->Pointer, Ptr, size);
+ ResData ->Pointer += size;
+
+ if (ResData ->Pointer > iohandler->UsedSpace)
+ iohandler->UsedSpace = ResData ->Pointer;
+
+ return TRUE;
+}
+
+
+static
+cmsBool MemoryClose(struct _cms_io_handler* iohandler)
+{
+ FILEMEM* ResData = (FILEMEM*) iohandler ->stream;
+
+ if (ResData ->FreeBlockOnClose) {
+
+ if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block);
+ }
+
+ _cmsFree(iohandler ->ContextID, ResData);
+ _cmsFree(iohandler ->ContextID, iohandler);
+
+ return TRUE;
+}
+
+// Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes
+// a copy of the memory block for letting user to free the memory after invoking open profile. In write
+// mode ("w"), Buffere points to the begin of memory block to be written.
+cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode)
+{
+ cmsIOHANDLER* iohandler = NULL;
+ FILEMEM* fm = NULL;
+
+ _cmsAssert(AccessMode != NULL);
+
+ iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
+ if (iohandler == NULL) return NULL;
+
+ switch (*AccessMode) {
+
+ case 'r':
+ fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
+ if (fm == NULL) goto Error;
+
+ if (Buffer == NULL) {
+ cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer");
+ goto Error;
+ }
+
+ fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size);
+ if (fm ->Block == NULL) {
+
+ _cmsFree(ContextID, fm);
+ _cmsFree(ContextID, iohandler);
+ cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", size);
+ return NULL;
+ }
+
+
+ memmove(fm->Block, Buffer, size);
+ fm ->FreeBlockOnClose = TRUE;
+ fm ->Size = size;
+ fm ->Pointer = 0;
+ iohandler -> ReportedSize = size;
+ break;
+
+ case 'w':
+ fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM));
+ if (fm == NULL) goto Error;
+
+ fm ->Block = (cmsUInt8Number*) Buffer;
+ fm ->FreeBlockOnClose = FALSE;
+ fm ->Size = size;
+ fm ->Pointer = 0;
+ iohandler -> ReportedSize = 0;
+ break;
+
+ default:
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode);
+ return NULL;
+ }
+
+ iohandler ->ContextID = ContextID;
+ iohandler ->stream = (void*) fm;
+ iohandler ->UsedSpace = 0;
+ iohandler ->PhysicalFile[0] = 0;
+
+ iohandler ->Read = MemoryRead;
+ iohandler ->Seek = MemorySeek;
+ iohandler ->Close = MemoryClose;
+ iohandler ->Tell = MemoryTell;
+ iohandler ->Write = MemoryWrite;
+
+ return iohandler;
+
+Error:
+ if (fm) _cmsFree(ContextID, fm);
+ if (iohandler) _cmsFree(ContextID, iohandler);
+ return NULL;
+}
+
+// File-based stream -------------------------------------------------------
+
+// Read count elements of size bytes each. Return number of elements read
+static
+cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count)
+{
+ cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream);
+
+ if (nReaded != count) {
+ cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size);
+ return 0;
+ }
+
+ return nReaded;
+}
+
+// Postion file pointer in the file
+static
+cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset)
+{
+ if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) {
+
+ cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+// Returns file pointer position
+static
+cmsUInt32Number FileTell(cmsIOHANDLER* iohandler)
+{
+ return (cmsUInt32Number) ftell((FILE*)iohandler ->stream);
+}
+
+// Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error
+static
+cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer)
+{
+ if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written
+
+ iohandler->UsedSpace += size;
+ return (fwrite(Buffer, size, 1, (FILE*) iohandler->stream) == 1);
+}
+
+// Closes the file
+static
+cmsBool FileClose(cmsIOHANDLER* iohandler)
+{
+ if (fclose((FILE*) iohandler ->stream) != 0) return FALSE;
+ _cmsFree(iohandler ->ContextID, iohandler);
+ return TRUE;
+}
+
+// Create a iohandler for disk based files.
+cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode)
+{
+ cmsIOHANDLER* iohandler = NULL;
+ FILE* fm = NULL;
+
+ _cmsAssert(FileName != NULL);
+ _cmsAssert(AccessMode != NULL);
+
+ iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
+ if (iohandler == NULL) return NULL;
+
+ switch (*AccessMode) {
+
+ case 'r':
+ fm = fopen(FileName, "rb");
+ if (fm == NULL) {
+ _cmsFree(ContextID, iohandler);
+ cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName);
+ return NULL;
+ }
+ iohandler -> ReportedSize = (cmsUInt32Number) cmsfilelength(fm);
+ break;
+
+ case 'w':
+ fm = fopen(FileName, "wb");
+ if (fm == NULL) {
+ _cmsFree(ContextID, iohandler);
+ cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName);
+ return NULL;
+ }
+ iohandler -> ReportedSize = 0;
+ break;
+
+ default:
+ _cmsFree(ContextID, iohandler);
+ cmsSignalError(ContextID, cmsERROR_FILE, "Unknown access mode '%c'", *AccessMode);
+ return NULL;
+ }
+
+ iohandler ->ContextID = ContextID;
+ iohandler ->stream = (void*) fm;
+ iohandler ->UsedSpace = 0;
+
+ // Keep track of the original file
+ strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1);
+ iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0;
+
+ iohandler ->Read = FileRead;
+ iohandler ->Seek = FileSeek;
+ iohandler ->Close = FileClose;
+ iohandler ->Tell = FileTell;
+ iohandler ->Write = FileWrite;
+
+ return iohandler;
+}
+
+// Create a iohandler for stream based files
+cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream)
+{
+ cmsIOHANDLER* iohandler = NULL;
+
+ iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER));
+ if (iohandler == NULL) return NULL;
+
+ iohandler -> ContextID = ContextID;
+ iohandler -> stream = (void*) Stream;
+ iohandler -> UsedSpace = 0;
+ iohandler -> ReportedSize = (cmsUInt32Number) cmsfilelength(Stream);
+ iohandler -> PhysicalFile[0] = 0;
+
+ iohandler ->Read = FileRead;
+ iohandler ->Seek = FileSeek;
+ iohandler ->Close = FileClose;
+ iohandler ->Tell = FileTell;
+ iohandler ->Write = FileWrite;
+
+ return iohandler;
+}
+
+
+
+// Close an open IO handler
+cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io)
+{
+ return io -> Close(io);
+}
+
+// -------------------------------------------------------------------------------------------------------
+
+#ifdef _WIN32_WCE
+time_t wceex_time(time_t *timer);
+struct tm * wceex_gmtime(const time_t *timer);
+
+#define time wceex_time
+#define gmtime wceex_gmtime
+#endif
+
+// Creates an empty structure holding all required parameters
+cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID)
+{
+ time_t now = time(NULL);
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE));
+ if (Icc == NULL) return NULL;
+
+ Icc ->ContextID = ContextID;
+
+ // Set it to empty
+ Icc -> TagCount = 0;
+
+ // Set default version
+ Icc ->Version = 0x02100000;
+
+ // Set creation date/time
+ memmove(&Icc ->Created, gmtime(&now), sizeof(Icc ->Created));
+
+ // Create a mutex if the user provided proper plugin. NULL otherwise
+ Icc ->UsrMutex = _cmsCreateMutex(ContextID);
+
+ // Return the handle
+ return (cmsHPROFILE) Icc;
+}
+
+cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+
+ if (Icc == NULL) return NULL;
+ return Icc -> ContextID;
+}
+
+
+// Return the number of tags
+cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ if (Icc == NULL) return -1;
+
+ return Icc->TagCount;
+}
+
+// Return the tag signature of a given tag number
+cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+
+ if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available
+ if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check
+
+ return Icc ->TagNames[n];
+}
+
+
+static
+int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < Profile -> TagCount; i++) {
+
+ if (sig == Profile -> TagNames[i])
+ return i;
+ }
+
+ return -1;
+}
+
+// Search for a specific tag in tag dictionary. Returns position or -1 if tag not found.
+// If followlinks is turned on, then the position of the linked tag is returned
+int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks)
+{
+ int n;
+ cmsTagSignature LinkedSig;
+
+ do {
+
+ // Search for given tag in ICC profile directory
+ n = SearchOneTag(Icc, sig);
+ if (n < 0)
+ return -1; // Not found
+
+ if (!lFollowLinks)
+ return n; // Found, don't follow links
+
+ // Is this a linked tag?
+ LinkedSig = Icc ->TagLinked[n];
+
+ // Yes, follow link
+ if (LinkedSig != (cmsTagSignature) 0) {
+ // fix bug mantis id#0055942
+ // assume that TRCTag and ColorantTag can't be linked.
+ // Xiaochuan Liu 2014-04-23
+ if ((sig == cmsSigRedTRCTag || sig == cmsSigGreenTRCTag || sig == cmsSigBlueTRCTag) &&
+ (LinkedSig == cmsSigRedColorantTag || LinkedSig == cmsSigGreenColorantTag || LinkedSig == cmsSigBlueColorantTag))
+ {
+ return n;
+ }
+ sig = LinkedSig;
+ }
+
+ } while (LinkedSig != (cmsTagSignature) 0);
+
+ return n;
+}
+
+// Deletes a tag entry
+
+static
+void _cmsDeleteTagByPos(_cmsICCPROFILE* Icc, int i)
+{
+ _cmsAssert(Icc != NULL);
+ _cmsAssert(i >= 0);
+
+
+ if (Icc -> TagPtrs[i] != NULL) {
+
+ // Free previous version
+ if (Icc ->TagSaveAsRaw[i]) {
+ _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
+ }
+ else {
+ cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
+
+ if (TypeHandler != NULL) {
+
+ cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
+ LocalTypeHandler.ContextID = Icc ->ContextID; // As an additional parameter
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+ LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]);
+ Icc ->TagPtrs[i] = NULL;
+ }
+ }
+
+ }
+}
+
+
+// Creates a new tag entry
+static
+cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos)
+{
+ int i;
+
+ // Search for the tag
+ i = _cmsSearchTag(Icc, sig, FALSE);
+ if (i >= 0) {
+
+ // Already exists? delete it
+ _cmsDeleteTagByPos(Icc, i);
+ *NewPos = i;
+ }
+ else {
+
+ // No, make a new one
+
+ if (Icc -> TagCount >= MAX_TABLE_TAG) {
+ cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG);
+ return FALSE;
+ }
+
+ *NewPos = Icc ->TagCount;
+ Icc -> TagCount++;
+ }
+
+ return TRUE;
+}
+
+
+// Check existance
+cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile;
+ return _cmsSearchTag(Icc, sig, FALSE) >= 0;
+}
+
+
+// Read profile header and validate it
+cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc)
+{
+ cmsTagEntry Tag;
+ cmsICCHeader Header;
+ cmsUInt32Number i, j;
+ cmsUInt32Number HeaderSize;
+ cmsIOHANDLER* io = Icc ->IOhandler;
+ cmsUInt32Number TagCount;
+
+
+ // Read the header
+ if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) {
+ return FALSE;
+ }
+
+ // Validate file as an ICC profile
+ if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) {
+ cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature");
+ return FALSE;
+ }
+
+ // Adjust endianess of the used parameters
+ Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass);
+ Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace);
+ Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs);
+
+ Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent);
+ Icc -> flags = _cmsAdjustEndianess32(Header.flags);
+ Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer);
+ Icc -> model = _cmsAdjustEndianess32(Header.model);
+ Icc -> creator = _cmsAdjustEndianess32(Header.creator);
+
+ _cmsAdjustEndianess64(&Icc -> attributes, &Header.attributes);
+ Icc -> Version = _cmsAdjustEndianess32(Header.version);
+
+ // Get size as reported in header
+ HeaderSize = _cmsAdjustEndianess32(Header.size);
+
+ // Make sure HeaderSize is lower than profile size
+ if (HeaderSize >= Icc ->IOhandler ->ReportedSize)
+ HeaderSize = Icc ->IOhandler ->ReportedSize;
+
+
+ // Get creation date/time
+ _cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created);
+
+ // The profile ID are 32 raw bytes
+ memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16);
+
+
+ // Read tag directory
+ if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE;
+ if (TagCount > MAX_TABLE_TAG) {
+
+ cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount);
+ return FALSE;
+ }
+
+
+ // Read tag directory
+ Icc -> TagCount = 0;
+ for (i=0; i < TagCount; i++) {
+
+ if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE;
+ if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE;
+ if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE;
+
+ // Perform some sanity check. Offset + size should fall inside file.
+ if (Tag.offset + Tag.size > HeaderSize ||
+ Tag.offset + Tag.size < Tag.offset)
+ continue;
+
+ Icc -> TagNames[Icc ->TagCount] = Tag.sig;
+ Icc -> TagOffsets[Icc ->TagCount] = Tag.offset;
+ Icc -> TagSizes[Icc ->TagCount] = Tag.size;
+
+ // Search for links
+ for (j=0; j < Icc ->TagCount; j++) {
+
+ if ((Icc ->TagOffsets[j] == Tag.offset) &&
+ (Icc ->TagSizes[j] == Tag.size)) {
+
+ Icc ->TagLinked[Icc ->TagCount] = Icc ->TagNames[j];
+ }
+
+ }
+
+ Icc ->TagCount++;
+ }
+
+ return TRUE;
+}
+
+// Saves profile header
+cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace)
+{
+ cmsICCHeader Header;
+ cmsUInt32Number i;
+ cmsTagEntry Tag;
+ cmsInt32Number Count = 0;
+
+ Header.size = _cmsAdjustEndianess32(UsedSpace);
+ Header.cmmId = _cmsAdjustEndianess32(lcmsSignature);
+ Header.version = _cmsAdjustEndianess32(Icc ->Version);
+
+ Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass);
+ Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace);
+ Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS);
+
+ // NOTE: in v4 Timestamp must be in UTC rather than in local time
+ _cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created);
+
+ Header.magic = _cmsAdjustEndianess32(cmsMagicNumber);
+
+#ifdef CMS_IS_WINDOWS_
+ Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMicrosoft);
+#else
+ Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(cmsSigMacintosh);
+#endif
+
+ Header.flags = _cmsAdjustEndianess32(Icc -> flags);
+ Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer);
+ Header.model = _cmsAdjustEndianess32(Icc -> model);
+
+ _cmsAdjustEndianess64(&Header.attributes, &Icc -> attributes);
+
+ // Rendering intent in the header (for embedded profiles)
+ Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent);
+
+ // Illuminant is always D50
+ Header.illuminant.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->X));
+ Header.illuminant.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y));
+ Header.illuminant.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z));
+
+ // Created by LittleCMS (that's me!)
+ Header.creator = _cmsAdjustEndianess32(lcmsSignature);
+
+ memset(&Header.reserved, 0, sizeof(Header.reserved));
+
+ // Set profile ID. Endianess is always big endian
+ memmove(&Header.profileID, &Icc ->ProfileID, 16);
+
+ // Dump the header
+ if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE;
+
+ // Saves Tag directory
+
+ // Get true count
+ for (i=0; i < Icc -> TagCount; i++) {
+ if (Icc ->TagNames[i] != 0)
+ Count++;
+ }
+
+ // Store number of tags
+ if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE;
+
+ for (i=0; i < Icc -> TagCount; i++) {
+
+ if (Icc ->TagNames[i] == 0) continue; // It is just a placeholder
+
+ Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagNames[i]);
+ Tag.offset = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagOffsets[i]);
+ Tag.size = _cmsAdjustEndianess32((cmsInt32Number) Icc -> TagSizes[i]);
+
+ if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE;
+ }
+
+ return TRUE;
+}
+
+// ----------------------------------------------------------------------- Set/Get several struct members
+
+
+cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc -> RenderingIntent;
+}
+
+void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> RenderingIntent = RenderingIntent;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return (cmsUInt32Number) Icc -> flags;
+}
+
+void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> flags = (cmsUInt32Number) Flags;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc ->manufacturer;
+}
+
+void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> manufacturer = manufacturer;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetHeaderCreator(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc ->creator;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc ->model;
+}
+
+void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> model = model;
+}
+
+void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number));
+}
+
+void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number));
+}
+
+void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ memmove(ProfileID, Icc ->ProfileID.ID8, 16);
+}
+
+void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ memmove(&Icc -> ProfileID, ProfileID, 16);
+}
+
+cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ memmove(Dest, &Icc ->Created, sizeof(struct tm));
+ return TRUE;
+}
+
+cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc -> PCS;
+}
+
+void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> PCS = pcs;
+}
+
+cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc -> ColorSpace;
+}
+
+void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> ColorSpace = sig;
+}
+
+cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc -> DeviceClass;
+}
+
+void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> DeviceClass = sig;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ return Icc -> Version;
+}
+
+void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ Icc -> Version = Version;
+}
+
+// Get an hexadecimal number with same digits as v
+static
+cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut)
+{
+ char Buff[100];
+ int i, len;
+ cmsUInt32Number out;
+
+ for (len=0; in > 0 && len < 100; len++) {
+
+ Buff[len] = (char) (in % BaseIn);
+ in /= BaseIn;
+ }
+
+ for (i=len-1, out=0; i >= 0; --i) {
+ out = out * BaseOut + Buff[i];
+ }
+
+ return out;
+}
+
+void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+
+ // 4.2 -> 0x4200000
+
+ Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0 + 0.5), 10, 16) << 16;
+}
+
+cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ cmsUInt32Number n = Icc -> Version >> 16;
+
+ return BaseToBase(n, 16, 10) / 100.0;
+}
+// --------------------------------------------------------------------------------------------------------------
+
+
+// Create profile from IOhandler
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io)
+{
+ _cmsICCPROFILE* NewIcc;
+ cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
+
+ if (hEmpty == NULL) return NULL;
+
+ NewIcc = (_cmsICCPROFILE*) hEmpty;
+
+ NewIcc ->IOhandler = io;
+ if (!_cmsReadHeader(NewIcc)) goto Error;
+ return hEmpty;
+
+Error:
+ cmsCloseProfile(hEmpty);
+ return NULL;
+}
+
+// Create profile from IOhandler
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandler2THR(cmsContext ContextID, cmsIOHANDLER* io, cmsBool write)
+{
+ _cmsICCPROFILE* NewIcc;
+ cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
+
+ if (hEmpty == NULL) return NULL;
+
+ NewIcc = (_cmsICCPROFILE*) hEmpty;
+
+ NewIcc ->IOhandler = io;
+ if (write) {
+
+ NewIcc -> IsWrite = TRUE;
+ return hEmpty;
+ }
+
+ if (!_cmsReadHeader(NewIcc)) goto Error;
+ return hEmpty;
+
+Error:
+ cmsCloseProfile(hEmpty);
+ return NULL;
+}
+
+
+// Create profile from disk file
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess)
+{
+ _cmsICCPROFILE* NewIcc;
+ cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
+
+ if (hEmpty == NULL) return NULL;
+
+ NewIcc = (_cmsICCPROFILE*) hEmpty;
+
+ NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess);
+ if (NewIcc ->IOhandler == NULL) goto Error;
+
+ if (*sAccess == 'W' || *sAccess == 'w') {
+
+ NewIcc -> IsWrite = TRUE;
+
+ return hEmpty;
+ }
+
+ if (!_cmsReadHeader(NewIcc)) goto Error;
+ return hEmpty;
+
+Error:
+ cmsCloseProfile(hEmpty);
+ return NULL;
+}
+
+
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess)
+{
+ return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess);
+}
+
+
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess)
+{
+ _cmsICCPROFILE* NewIcc;
+ cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID);
+
+ if (hEmpty == NULL) return NULL;
+
+ NewIcc = (_cmsICCPROFILE*) hEmpty;
+
+ NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile);
+ if (NewIcc ->IOhandler == NULL) goto Error;
+
+ if (*sAccess == 'w') {
+
+ NewIcc -> IsWrite = TRUE;
+ return hEmpty;
+ }
+
+ if (!_cmsReadHeader(NewIcc)) goto Error;
+ return hEmpty;
+
+Error:
+ cmsCloseProfile(hEmpty);
+ return NULL;
+
+}
+
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess)
+{
+ return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess);
+}
+
+
+// Open from memory block
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize)
+{
+ _cmsICCPROFILE* NewIcc;
+ cmsHPROFILE hEmpty;
+
+ hEmpty = cmsCreateProfilePlaceholder(ContextID);
+ if (hEmpty == NULL) return NULL;
+
+ NewIcc = (_cmsICCPROFILE*) hEmpty;
+
+ // Ok, in this case const void* is casted to void* just because open IO handler
+ // shares read and writting modes. Don't abuse this feature!
+ NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r");
+ if (NewIcc ->IOhandler == NULL) goto Error;
+
+ if (!_cmsReadHeader(NewIcc)) goto Error;
+
+ return hEmpty;
+
+Error:
+ cmsCloseProfile(hEmpty);
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize)
+{
+ return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize);
+}
+
+
+
+// Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig
+static
+cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig)
+{
+ cmsUInt8Number* Data;
+ cmsUInt32Number i;
+ cmsUInt32Number Begin;
+ cmsIOHANDLER* io = Icc ->IOhandler;
+ cmsTagDescriptor* TagDescriptor;
+ cmsTagTypeSignature TypeBase;
+ cmsTagTypeSignature Type;
+ cmsTagTypeHandler* TypeHandler;
+ cmsFloat64Number Version = cmsGetProfileVersion((cmsHPROFILE) Icc);
+ cmsTagTypeHandler LocalTypeHandler;
+
+ for (i=0; i < Icc -> TagCount; i++) {
+
+ if (Icc ->TagNames[i] == 0) continue;
+
+ // Linked tags are not written
+ if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue;
+
+ Icc -> TagOffsets[i] = Begin = io ->UsedSpace;
+
+ Data = (cmsUInt8Number*) Icc -> TagPtrs[i];
+
+ if (!Data) {
+
+ // Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user.
+ // In this case a blind copy of the block data is performed
+ if (FileOrig != NULL && Icc -> TagOffsets[i]) {
+
+ cmsUInt32Number TagSize = FileOrig -> TagSizes[i];
+ cmsUInt32Number TagOffset = FileOrig -> TagOffsets[i];
+ void* Mem;
+
+ if (!FileOrig ->IOhandler->Seek(FileOrig ->IOhandler, TagOffset)) return FALSE;
+
+ Mem = _cmsMalloc(Icc ->ContextID, TagSize);
+ if (Mem == NULL) return FALSE;
+
+ if (FileOrig ->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE;
+ if (!io ->Write(io, TagSize, Mem)) return FALSE;
+ _cmsFree(Icc ->ContextID, Mem);
+
+ Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
+
+
+ // Align to 32 bit boundary.
+ if (! _cmsWriteAlignment(io))
+ return FALSE;
+ }
+
+ continue;
+ }
+
+
+ // Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done)
+ if (Icc ->TagSaveAsRaw[i]) {
+
+ if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE;
+ }
+ else {
+
+ // Search for support on this tag
+ TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, Icc -> TagNames[i]);
+ if (TagDescriptor == NULL) continue; // Unsupported, ignore it
+
+ if (TagDescriptor ->DecideType != NULL) {
+
+ Type = TagDescriptor ->DecideType(Version, Data);
+ }
+ else {
+
+ Type = TagDescriptor ->SupportedTypes[0];
+ }
+
+ TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type);
+
+ if (TypeHandler == NULL) {
+ cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]);
+ continue;
+ }
+
+ TypeBase = TypeHandler ->Signature;
+ if (!_cmsWriteTypeBase(io, TypeBase))
+ return FALSE;
+
+ LocalTypeHandler = *TypeHandler;
+ LocalTypeHandler.ContextID = Icc ->ContextID;
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+ if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, io, Data, TagDescriptor ->ElemCount)) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) TypeBase);
+ cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String);
+ return FALSE;
+ }
+ }
+
+
+ Icc -> TagSizes[i] = (io ->UsedSpace - Begin);
+
+ // Align to 32 bit boundary.
+ if (! _cmsWriteAlignment(io))
+ return FALSE;
+ }
+
+
+ return TRUE;
+}
+
+
+// Fill the offset and size fields for all linked tags
+static
+cmsBool SetLinks( _cmsICCPROFILE* Icc)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < Icc -> TagCount; i++) {
+
+ cmsTagSignature lnk = Icc ->TagLinked[i];
+ if (lnk != (cmsTagSignature) 0) {
+
+ int j = _cmsSearchTag(Icc, lnk, FALSE);
+ if (j >= 0) {
+
+ Icc ->TagOffsets[i] = Icc ->TagOffsets[j];
+ Icc ->TagSizes[i] = Icc ->TagSizes[j];
+ }
+
+ }
+ }
+
+ return TRUE;
+}
+
+// Low-level save to IOHANDLER. It returns the number of bytes used to
+// store the profile, or zero on error. io may be NULL and in this case
+// no data is written--only sizes are calculated
+cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ _cmsICCPROFILE Keep;
+ cmsIOHANDLER* PrevIO = NULL;
+ cmsUInt32Number UsedSpace;
+ cmsContext ContextID;
+
+ _cmsAssert(hProfile != NULL);
+
+ memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
+
+ ContextID = cmsGetProfileContextID(hProfile);
+ PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
+ if (PrevIO == NULL) return 0;
+
+ // Pass #1 does compute offsets
+
+ if (!_cmsWriteHeader(Icc, 0)) goto Error;
+ if (!SaveTags(Icc, &Keep)) goto Error;
+
+ UsedSpace = PrevIO ->UsedSpace;
+
+ // Pass #2 does save to iohandler
+
+ if (io != NULL) {
+
+ Icc ->IOhandler = io;
+ if (!SetLinks(Icc)) goto Error;
+ if (!_cmsWriteHeader(Icc, UsedSpace)) goto Error;
+ if (!SaveTags(Icc, &Keep)) goto Error;
+ }
+
+ memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
+ if (!cmsCloseIOhandler(PrevIO)) return 0;
+
+ return UsedSpace;
+
+
+Error:
+ cmsCloseIOhandler(PrevIO);
+ memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
+ return 0;
+}
+
+#ifdef _WIN32_WCE
+int wceex_unlink(const char *filename);
+#ifndef remove
+# define remove wceex_unlink
+#endif
+#endif
+
+// Low-level save to disk.
+cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName)
+{
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w");
+ cmsBool rc;
+
+ if (io == NULL) return FALSE;
+
+ rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
+ rc &= cmsCloseIOhandler(io);
+
+ if (rc == FALSE) { // remove() is C99 per 7.19.4.1
+ remove(FileName); // We have to IGNORE return value in this case
+ }
+ return rc;
+}
+
+// Same as anterior, but for streams
+cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream)
+{
+ cmsBool rc;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream);
+
+ if (io == NULL) return FALSE;
+
+ rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
+ rc &= cmsCloseIOhandler(io);
+
+ return rc;
+}
+
+
+// Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only
+cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded)
+{
+ cmsBool rc;
+ cmsIOHANDLER* io;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ _cmsAssert(BytesNeeded != NULL);
+
+ // Should we just calculate the needed space?
+ if (MemPtr == NULL) {
+
+ *BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL);
+ return (*BytesNeeded == 0) ? FALSE : TRUE;
+ }
+
+ // That is a real write operation
+ io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w");
+ if (io == NULL) return FALSE;
+
+ rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0);
+ rc &= cmsCloseIOhandler(io);
+
+ return rc;
+}
+
+
+
+// Closes a profile freeing any involved resources
+cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ cmsBool rc = TRUE;
+ cmsUInt32Number i;
+
+ if (!Icc) return FALSE;
+
+ // Was open in write mode?
+ if (Icc ->IsWrite) {
+
+ Icc ->IsWrite = FALSE; // Assure no further writting
+ rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile);
+ }
+
+ for (i=0; i < Icc -> TagCount; i++) {
+
+ if (Icc -> TagPtrs[i]) {
+
+ cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];
+
+ if (TypeHandler != NULL) {
+ cmsTagTypeHandler LocalTypeHandler = *TypeHandler;
+
+ LocalTypeHandler.ContextID = Icc ->ContextID; // As an additional parameters
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+ LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]);
+ }
+ else
+ _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
+ }
+ }
+
+ if (Icc ->IOhandler != NULL) {
+ rc &= cmsCloseIOhandler(Icc->IOhandler);
+ }
+
+ _cmsDestroyMutex(Icc->ContextID, Icc->UsrMutex);
+
+ _cmsFree(Icc ->ContextID, Icc); // Free placeholder memory
+
+ return rc;
+}
+
+
+// -------------------------------------------------------------------------------------------------------------------
+
+
+// Returns TRUE if a given tag is supported by a plug-in
+static
+cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type)
+{
+ cmsUInt32Number i, nMaxTypes;
+
+ nMaxTypes = TagDescriptor->nSupportedTypes;
+ if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN)
+ nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN;
+
+ for (i=0; i < nMaxTypes; i++) {
+ if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+// That's the main read function
+void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ cmsIOHANDLER* io = Icc ->IOhandler;
+ cmsTagTypeHandler* TypeHandler;
+ cmsTagTypeHandler LocalTypeHandler;
+ cmsTagDescriptor* TagDescriptor;
+ cmsTagTypeSignature BaseType;
+ cmsUInt32Number Offset, TagSize;
+ cmsUInt32Number ElemCount;
+ int n;
+
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return NULL;
+
+ n = _cmsSearchTag(Icc, sig, TRUE);
+ if (n < 0) goto Error; // Not found, return NULL
+
+
+ // If the element is already in memory, return the pointer
+ if (Icc -> TagPtrs[n]) {
+
+ if (Icc ->TagSaveAsRaw[n]) goto Error; // We don't support read raw tags as cooked
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return Icc -> TagPtrs[n];
+ }
+
+ // We need to read it. Get the offset and size to the file
+ Offset = Icc -> TagOffsets[n];
+ TagSize = Icc -> TagSizes[n];
+
+ // Seek to its location
+ if (!io -> Seek(io, Offset))
+ goto Error;
+
+ // Search for support on this tag
+ TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
+ if (TagDescriptor == NULL) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, sig);
+
+ // An unknown element was found.
+ cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown tag type '%s' found.", String);
+ goto Error; // Unsupported.
+ }
+
+ // if supported, get type and check if in list
+ BaseType = _cmsReadTypeBase(io);
+ if (BaseType == 0) goto Error;
+
+ if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error;
+
+ TagSize -= 8; // Alredy read by the type base logic
+
+ // Get type handler
+ TypeHandler = _cmsGetTagTypeHandler(Icc ->ContextID, BaseType);
+ if (TypeHandler == NULL) goto Error;
+ LocalTypeHandler = *TypeHandler;
+
+
+ // Read the tag
+ Icc -> TagTypeHandlers[n] = TypeHandler;
+
+ LocalTypeHandler.ContextID = Icc ->ContextID;
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+ Icc -> TagPtrs[n] = LocalTypeHandler.ReadPtr(&LocalTypeHandler, io, &ElemCount, TagSize);
+
+ // The tag type is supported, but something wrong happend and we cannot read the tag.
+ // let know the user about this (although it is just a warning)
+ if (Icc -> TagPtrs[n] == NULL) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, sig);
+ cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
+ goto Error;
+ }
+
+ // This is a weird error that may be a symptom of something more serious, the number of
+ // stored item is actually less than the number of required elements.
+ if (ElemCount < TagDescriptor ->ElemCount) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, sig);
+ cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
+ String, TagDescriptor ->ElemCount, ElemCount);
+ }
+
+
+ // Return the data
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return Icc -> TagPtrs[n];
+
+
+ // Return error and unlock tha data
+Error:
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return NULL;
+}
+
+
+// Get true type of data
+cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ cmsTagTypeHandler* TypeHandler;
+ int n;
+
+ // Search for given tag in ICC profile directory
+ n = _cmsSearchTag(Icc, sig, TRUE);
+ if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL
+
+ // Get the handler. The true type is there
+ TypeHandler = Icc -> TagTypeHandlers[n];
+ return TypeHandler ->Signature;
+}
+
+
+// Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already
+// in that list, the previous version is deleted.
+cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ cmsTagTypeHandler* TypeHandler = NULL;
+ cmsTagTypeHandler LocalTypeHandler;
+ cmsTagDescriptor* TagDescriptor = NULL;
+ cmsTagTypeSignature Type;
+ int i;
+ cmsFloat64Number Version;
+ char TypeString[5], SigString[5];
+
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
+
+ // To delete tags.
+ if (data == NULL) {
+
+ // Delete the tag
+ i = _cmsSearchTag(Icc, sig, FALSE);
+ if (i >= 0) {
+
+ // Use zero as a mark of deleted
+ _cmsDeleteTagByPos(Icc, i);
+ Icc ->TagNames[i] = (cmsTagSignature) 0;
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TRUE;
+ }
+ // Didn't find the tag
+ goto Error;
+ }
+
+ if (!_cmsNewTag(Icc, sig, &i)) goto Error;
+
+ // This is not raw
+ Icc ->TagSaveAsRaw[i] = FALSE;
+
+ // This is not a link
+ Icc ->TagLinked[i] = (cmsTagSignature) 0;
+
+ // Get information about the TAG.
+ TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
+ if (TagDescriptor == NULL){
+ cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig);
+ goto Error;
+ }
+
+
+ // Now we need to know which type to use. It depends on the version.
+ Version = cmsGetProfileVersion(hProfile);
+
+ if (TagDescriptor ->DecideType != NULL) {
+
+ // Let the tag descriptor to decide the type base on depending on
+ // the data. This is useful for example on parametric curves, where
+ // curves specified by a table cannot be saved as parametric and needs
+ // to be casted to single v2-curves, even on v4 profiles.
+
+ Type = TagDescriptor ->DecideType(Version, data);
+ }
+ else {
+
+ Type = TagDescriptor ->SupportedTypes[0];
+ }
+
+ // Does the tag support this type?
+ if (!IsTypeSupported(TagDescriptor, Type)) {
+
+ _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
+ _cmsTagSignature2String(SigString, sig);
+
+ cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
+ goto Error;
+ }
+
+ // Does we have a handler for this type?
+ TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type);
+ if (TypeHandler == NULL) {
+
+ _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
+ _cmsTagSignature2String(SigString, sig);
+
+ cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString);
+ goto Error; // Should never happen
+ }
+
+
+ // Fill fields on icc structure
+ Icc ->TagTypeHandlers[i] = TypeHandler;
+ Icc ->TagNames[i] = sig;
+ Icc ->TagSizes[i] = 0;
+ Icc ->TagOffsets[i] = 0;
+
+ LocalTypeHandler = *TypeHandler;
+ LocalTypeHandler.ContextID = Icc ->ContextID;
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+ Icc ->TagPtrs[i] = LocalTypeHandler.DupPtr(&LocalTypeHandler, data, TagDescriptor ->ElemCount);
+
+ if (Icc ->TagPtrs[i] == NULL) {
+
+ _cmsTagSignature2String(TypeString, (cmsTagSignature) Type);
+ _cmsTagSignature2String(SigString, sig);
+ cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString);
+
+ goto Error;
+ }
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TRUE;
+
+Error:
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return FALSE;
+
+}
+
+// Read and write raw data. The only way those function would work and keep consistence with normal read and write
+// is to do an additional step of serialization. That means, readRaw would issue a normal read and then convert the obtained
+// data to raw bytes by using the "write" serialization logic. And vice-versa. I know this may end in situations where
+// raw data written does not exactly correspond with the raw data proposed to cmsWriteRaw data, but this approach allows
+// to write a tag as raw data and the read it as handled.
+
+cmsInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ void *Object;
+ int i;
+ cmsIOHANDLER* MemIO;
+ cmsTagTypeHandler* TypeHandler = NULL;
+ cmsTagTypeHandler LocalTypeHandler;
+ cmsTagDescriptor* TagDescriptor = NULL;
+ cmsUInt32Number rc;
+ cmsUInt32Number Offset, TagSize;
+
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
+
+ // Search for given tag in ICC profile directory
+ i = _cmsSearchTag(Icc, sig, TRUE);
+ if (i < 0) goto Error; // Not found,
+
+ // It is already read?
+ if (Icc -> TagPtrs[i] == NULL) {
+
+ // No yet, get original position
+ Offset = Icc ->TagOffsets[i];
+ TagSize = Icc ->TagSizes[i];
+
+ // read the data directly, don't keep copy
+ if (data != NULL) {
+
+ if (BufferSize < TagSize)
+ TagSize = BufferSize;
+
+ if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) goto Error;
+ if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) goto Error;
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TagSize;
+ }
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return Icc ->TagSizes[i];
+ }
+
+ // The data has been already read, or written. But wait!, maybe the user choosed to save as
+ // raw data. In this case, return the raw data directly
+ if (Icc ->TagSaveAsRaw[i]) {
+
+ if (data != NULL) {
+
+ TagSize = Icc ->TagSizes[i];
+ if (BufferSize < TagSize)
+ TagSize = BufferSize;
+
+ memmove(data, Icc ->TagPtrs[i], TagSize);
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TagSize;
+ }
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return Icc ->TagSizes[i];
+ }
+
+ // Already readed, or previously set by cmsWriteTag(). We need to serialize that
+ // data to raw in order to maintain consistency.
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ Object = cmsReadTag(hProfile, sig);
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
+
+ if (Object == NULL) goto Error;
+
+ // Now we need to serialize to a memory block: just use a memory iohandler
+
+ if (data == NULL) {
+ MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile));
+ } else{
+ MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w");
+ }
+ if (MemIO == NULL) goto Error;
+
+ // Obtain type handling for the tag
+ TypeHandler = Icc ->TagTypeHandlers[i];
+ TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig);
+ if (TagDescriptor == NULL) {
+ cmsCloseIOhandler(MemIO);
+ goto Error;
+ }
+
+ if (TypeHandler == NULL) goto Error;
+
+ // Serialize
+ LocalTypeHandler = *TypeHandler;
+ LocalTypeHandler.ContextID = Icc ->ContextID;
+ LocalTypeHandler.ICCVersion = Icc ->Version;
+
+ if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) {
+ cmsCloseIOhandler(MemIO);
+ goto Error;
+ }
+
+ if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) {
+ cmsCloseIOhandler(MemIO);
+ goto Error;
+ }
+
+ // Get Size and close
+ rc = MemIO ->Tell(MemIO);
+ cmsCloseIOhandler(MemIO); // Ignore return code this time
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return rc;
+
+Error:
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return 0;
+}
+
+// Similar to the anterior. This function allows to write directly to the ICC profile any data, without
+// checking anything. As a rule, mixing Raw with cooked doesn't work, so writting a tag as raw and then reading
+// it as cooked without serializing does result into an error. If that is wha you want, you will need to dump
+// the profile to memry or disk and then reopen it.
+cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ int i;
+
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0;
+
+ if (!_cmsNewTag(Icc, sig, &i)) {
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return FALSE;
+ }
+
+ // Mark the tag as being written as RAW
+ Icc ->TagSaveAsRaw[i] = TRUE;
+ Icc ->TagNames[i] = sig;
+ Icc ->TagLinked[i] = (cmsTagSignature) 0;
+
+ // Keep a copy of the block
+ Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size);
+ Icc ->TagSizes[i] = Size;
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TRUE;
+}
+
+// Using this function you can collapse several tag entries to the same block in the profile
+cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ int i;
+
+ if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE;
+
+ if (!_cmsNewTag(Icc, sig, &i)) {
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return FALSE;
+ }
+
+ // Keep necessary information
+ Icc ->TagSaveAsRaw[i] = FALSE;
+ Icc ->TagNames[i] = sig;
+ Icc ->TagLinked[i] = dest;
+
+ Icc ->TagPtrs[i] = NULL;
+ Icc ->TagSizes[i] = 0;
+ Icc ->TagOffsets[i] = 0;
+
+ _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
+ return TRUE;
+}
+
+
+// Returns the tag linked to sig, in the case two tags are sharing same resource
+cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig)
+{
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ int i;
+
+ // Search for given tag in ICC profile directory
+ i = _cmsSearchTag(Icc, sig, FALSE);
+ if (i < 0) return (cmsTagSignature) 0; // Not found, return 0
+
+ return Icc -> TagLinked[i];
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio1.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio1.c
new file mode 100644
index 0000000000..778aa2b4fc
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsio1.c
@@ -0,0 +1,1020 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
+
+// LUT tags
+static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual
+ cmsSigAToB1Tag, // Relative colorimetric
+ cmsSigAToB2Tag, // Saturation
+ cmsSigAToB1Tag }; // Absolute colorimetric
+
+static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual
+ cmsSigDToB1Tag, // Relative colorimetric
+ cmsSigDToB2Tag, // Saturation
+ cmsSigDToB3Tag }; // Absolute colorimetric
+
+static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual
+ cmsSigBToA1Tag, // Relative colorimetric
+ cmsSigBToA2Tag, // Saturation
+ cmsSigBToA1Tag }; // Absolute colorimetric
+
+static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual
+ cmsSigBToD1Tag, // Relative colorimetric
+ cmsSigBToD2Tag, // Saturation
+ cmsSigBToD3Tag }; // Absolute colorimetric
+
+
+// Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa
+#define InpAdj (1.0/MAX_ENCODEABLE_XYZ) // (65536.0/(65535.0*2.0))
+#define OutpAdj (MAX_ENCODEABLE_XYZ) // ((2.0*65535.0)/65536.0)
+
+// Several resources for gray conversions.
+static const cmsFloat64Number GrayInputMatrix[] = { (InpAdj*cmsD50X), (InpAdj*cmsD50Y), (InpAdj*cmsD50Z) };
+static const cmsFloat64Number OneToThreeInputMatrix[] = { 1, 1, 1 };
+static const cmsFloat64Number PickYMatrix[] = { 0, (OutpAdj*cmsD50Y), 0 };
+static const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };
+
+// Get a media white point fixing some issues found in certain old profiles
+cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
+{
+ cmsCIEXYZ* Tag;
+
+ _cmsAssert(Dest != NULL);
+
+ Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
+
+ // If no wp, take D50
+ if (Tag == NULL) {
+ *Dest = *cmsD50_XYZ();
+ return TRUE;
+ }
+
+ // V2 display profiles should give D50
+ if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
+
+ if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
+ *Dest = *cmsD50_XYZ();
+ return TRUE;
+ }
+ }
+
+ // All seems ok
+ *Dest = *Tag;
+ return TRUE;
+}
+
+
+// Chromatic adaptation matrix. Fix some issues as well
+cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
+{
+ cmsMAT3* Tag;
+
+ _cmsAssert(Dest != NULL);
+
+ Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
+
+ if (Tag != NULL) {
+ *Dest = *Tag;
+ return TRUE;
+ }
+
+ // No CHAD available, default it to identity
+ _cmsMAT3identity(Dest);
+
+ // V2 display profiles should give D50
+ if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
+
+ if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
+
+ cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
+
+ if (White == NULL) {
+
+ _cmsMAT3identity(Dest);
+ return TRUE;
+ }
+
+ return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());
+ }
+ }
+
+ return TRUE;
+}
+
+
+// Auxiliar, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
+static
+cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
+{
+ cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
+
+ _cmsAssert(r != NULL);
+
+ PtrRed = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);
+ PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);
+ PtrBlue = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);
+
+ if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
+ return FALSE;
+
+ _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);
+ _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);
+ _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);
+
+ return TRUE;
+}
+
+
+// Gray input pipeline
+static
+cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)
+{
+ cmsToneCurve *GrayTRC;
+ cmsPipeline* Lut;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
+ if (GrayTRC == NULL) return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 1, 3);
+ if (Lut == NULL)
+ goto Error;
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ // In this case we implement the profile as an identity matrix plus 3 tone curves
+ cmsUInt16Number Zero[2] = { 0x8080, 0x8080 };
+ cmsToneCurve* EmptyTab;
+ cmsToneCurve* LabCurves[3];
+
+ EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
+
+ if (EmptyTab == NULL)
+ goto Error;
+
+ LabCurves[0] = GrayTRC;
+ LabCurves[1] = EmptyTab;
+ LabCurves[2] = EmptyTab;
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, OneToThreeInputMatrix, NULL)) ||
+ !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves))) {
+ cmsFreeToneCurve(EmptyTab);
+ goto Error;
+ }
+
+ cmsFreeToneCurve(EmptyTab);
+
+ }
+ else {
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC)) ||
+ !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, GrayInputMatrix, NULL)))
+ goto Error;
+ }
+
+ return Lut;
+
+Error:
+ // memory pointed by GrayTRC is not a new malloc memory, so don't free it here,
+ // memory pointed by GrayTRC will be freed when hProfile is closed.
+ // test file :0047776_Pocket Medicine_ The Massachusetts General Hospital Handbook of Internal Medicine-2.pdf
+ // Xiaochuan Liu, 20140421
+ //cmsFreeToneCurve(GrayTRC);
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+// RGB Matrix shaper
+static
+cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsPipeline* Lut;
+ cmsMAT3 Mat;
+ cmsToneCurve *Shapes[3];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ int i, j;
+
+ if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
+
+ // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
+ // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
+ // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ Mat.v[i].n[j] *= InpAdj;
+
+
+ Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
+ Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
+ Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2])
+ return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||
+ !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))
+ goto Error;
+
+ // Note that it is certainly possible a single profile would have a LUT based
+ // tag for output working in lab and a matrix-shaper for the fallback cases.
+ // This is not allowed by the spec, but this code is tolerant to those cases
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))
+ goto Error;
+ }
+
+ }
+
+ return Lut;
+
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+
+
+// Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
+static
+cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
+{
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
+ cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
+ cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
+
+ if (Lut == NULL) return NULL;
+
+ // input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,
+ // these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
+ if ( spc == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
+ goto Error;
+ }
+ else if (spc == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ if ( PCS == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
+ goto Error;
+ }
+ else if( PCS == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ return Lut;
+
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+
+// Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
+// is adjusted here in order to create a LUT that takes care of all those details.
+// We add intent = -1 as a way to read matrix shaper always, no matter of other LUT
+cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = Device2PCS16[Intent];
+ cmsTagSignature tagFloat = Device2PCSFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ // On named color, take the appropiate tag
+ if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
+
+ cmsPipeline* Lut;
+ cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
+
+ if (nc == NULL) return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 0, 0);
+ if (Lut == NULL) {
+ cmsFreeNamedColorList(nc);
+ return NULL;
+ }
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||
+ !cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {
+ cmsPipelineFree(Lut);
+ return NULL;
+ }
+ return Lut;
+ }
+
+ // This is an attempt to reuse this funtion to retrieve the matrix-shaper as pipeline no
+ // matter other LUT are present and have precedence. Intent = -1 means just this.
+ if (Intent != -1) {
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V4, but the encoding range is no
+ // longer 0..1.0, so we need to add an stage depending on the color space
+ return _cmsReadFloatInputTag(hProfile, tagFloat);
+ }
+
+ // Revert to perceptual if no tag is found
+ if (!cmsIsTag(hProfile, tag16)) {
+ tag16 = Device2PCS16[0];
+ }
+
+ if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // First read the tag
+ cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // After reading it, we have now info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+
+ // We need to adjust data only for Lab16 on output
+ if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
+ return Lut;
+
+ // If the input is Lab, add also a conversion at the begin
+ if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
+ !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
+ goto Error;
+
+ // Add a matrix for conversion V2 to V4 Lab PCS
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
+ goto Error;
+
+ return Lut;
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+ }
+ }
+
+ // Lut was not found, try to create a matrix-shaper
+
+ // Check if this is a grayscale profile.
+ if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
+
+ // if so, build appropiate conversion tables.
+ // The tables are the PCS iluminant, scaled across GrayTRC
+ return BuildGrayInputMatrixPipeline(hProfile);
+ }
+
+ // Not gray, create a normal matrix-shaper
+ return BuildRGBInputMatrixShaper(hProfile);
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Gray output pipeline.
+// XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
+// given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
+// The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
+
+static
+cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
+{
+ cmsToneCurve *GrayTRC, *RevGrayTRC;
+ cmsPipeline* Lut;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
+ if (GrayTRC == NULL) return NULL;
+
+ RevGrayTRC = cmsReverseToneCurve(GrayTRC);
+ if (RevGrayTRC == NULL) return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 1);
+ if (Lut == NULL) {
+ cmsFreeToneCurve(RevGrayTRC);
+ return NULL;
+ }
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))
+ goto Error;
+ }
+ else {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL)))
+ goto Error;
+ }
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
+ goto Error;
+
+ cmsFreeToneCurve(RevGrayTRC);
+ return Lut;
+
+Error:
+ cmsFreeToneCurve(RevGrayTRC);
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+
+static
+cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsPipeline* Lut;
+ cmsToneCurve *Shapes[3], *InvShapes[3];
+ cmsMAT3 Mat, Inv;
+ int i, j;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
+ return NULL;
+
+ if (!_cmsMAT3inverse(&Mat, &Inv))
+ return NULL;
+
+ // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
+ // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
+ // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ Inv.v[i].n[j] *= OutpAdj;
+
+ Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
+ Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
+ Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2])
+ return NULL;
+
+ InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
+ InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
+ InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
+
+ if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
+ return NULL;
+ }
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ // Note that it is certainly possible a single profile would have a LUT based
+ // tag for output working in lab and a matrix-shaper for the fallback cases.
+ // This is not allowed by the spec, but this code is tolerant to those cases
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
+ goto Error;
+ }
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
+ !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
+ goto Error;
+ }
+
+ cmsFreeToneCurveTriple(InvShapes);
+ return Lut;
+Error:
+ cmsFreeToneCurveTriple(InvShapes);
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+
+// Change CLUT interpolation to trilinear
+static
+void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
+{
+ cmsStage* Stage;
+
+ for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
+ Stage != NULL;
+ Stage = cmsStageNext(Stage)) {
+
+ if (cmsStageType(Stage) == cmsSigCLutElemType) {
+
+ _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
+
+ CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
+ _cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);
+ }
+ }
+}
+
+
+// Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
+static
+cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
+{
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
+ cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
+ cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
+
+ if (Lut == NULL) return NULL;
+
+ // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
+ // and since the formatter has already accomodated to 0..1.0, we should undo this change
+ if ( PCS == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
+ goto Error;
+ }
+ else
+ if (PCS == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
+ if ( dataSpace == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
+ goto Error;
+ }
+ else if (dataSpace == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ return Lut;
+
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+// Create an output MPE LUT from agiven profile. Version mismatches are handled here
+cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = PCS2Device16[Intent];
+ cmsTagSignature tagFloat = PCS2DeviceFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+
+ if (Intent != -1) {
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V4
+ return _cmsReadFloatOutputTag(hProfile, tagFloat);
+ }
+
+ // Revert to perceptual if no tag is found
+ if (!cmsIsTag(hProfile, tag16)) {
+ tag16 = PCS2Device16[0];
+ }
+
+ if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // First read the tag
+ cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // After reading it, we have info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+ if (Lut == NULL) return NULL;
+
+ // Now it is time for a controversial stuff. I found that for 3D LUTS using
+ // Lab used as indexer space, trilinear interpolation should be used
+ if (cmsGetPCS(hProfile) == cmsSigLabData)
+ ChangeInterpolationToTrilinear(Lut);
+
+ // We need to adjust data only for Lab and Lut16 type
+ if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
+ return Lut;
+
+ // Add a matrix for conversion V4 to V2 Lab PCS
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
+ goto Error;
+
+ // If the output is Lab, add also a conversion at the end
+ if (cmsGetColorSpace(hProfile) == cmsSigLabData)
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
+ goto Error;
+
+ return Lut;
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+ }
+ }
+
+ // Lut not found, try to create a matrix-shaper
+
+ // Check if this is a grayscale profile.
+ if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
+
+ // if so, build appropiate conversion tables.
+ // The tables are the PCS iluminant, scaled across GrayTRC
+ return BuildGrayOutputPipeline(hProfile);
+ }
+
+ // Not gray, create a normal matrix-shaper, which only operates in XYZ space
+ return BuildRGBOutputMatrixShaper(hProfile);
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
+static
+cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
+{
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
+ cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
+ cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
+
+ if (Lut == NULL) return NULL;
+
+ if (spc == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
+ goto Error;
+ }
+ else
+ if (spc == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ if (PCS == cmsSigLabData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
+ goto Error;
+ }
+ else
+ if (PCS == cmsSigXYZData)
+ {
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
+ goto Error;
+ }
+
+ return Lut;
+Error:
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+// This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
+// tag name here may default to AToB0
+cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsPipeline* Lut;
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = Device2PCS16[Intent];
+ cmsTagSignature tagFloat = Device2PCSFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+
+ // On named color, take the appropiate tag
+ if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
+
+ cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
+
+ if (nc == NULL) return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 0, 0);
+ if (Lut == NULL)
+ goto Error;
+
+ if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
+ goto Error;
+
+ if (cmsGetColorSpace(hProfile) == cmsSigLabData)
+ if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
+ goto Error;
+
+ return Lut;
+Error:
+ cmsPipelineFree(Lut);
+ cmsFreeNamedColorList(nc);
+ return NULL;
+ }
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V
+ return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
+ }
+
+ tagFloat = Device2PCSFloat[0];
+ if (cmsIsTag(hProfile, tagFloat)) {
+
+ return cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
+ }
+
+ if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ tag16 = Device2PCS16[0];
+ if (!cmsIsTag(hProfile, tag16)) return NULL;
+ }
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // Read the tag
+ Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+ if (Lut == NULL) return NULL;
+
+ // Now it is time for a controversial stuff. I found that for 3D LUTS using
+ // Lab used as indexer space, trilinear interpolation should be used
+ if (cmsGetPCS(hProfile) == cmsSigLabData)
+ ChangeInterpolationToTrilinear(Lut);
+
+ // After reading it, we have info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // We need to adjust data for Lab16 on output
+ if (OriginalType != cmsSigLut16Type) return Lut;
+
+ // Here it is possible to get Lab on both sides
+
+ if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
+ if(!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
+ goto Error2;
+ }
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+ if(!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
+ goto Error2;
+ }
+
+ return Lut;
+
+Error2:
+ cmsPipelineFree(Lut);
+ return NULL;
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Returns TRUE if the profile is implemented as matrix-shaper
+cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
+{
+ switch (cmsGetColorSpace(hProfile)) {
+
+ case cmsSigGrayData:
+
+ return cmsIsTag(hProfile, cmsSigGrayTRCTag);
+
+ case cmsSigRgbData:
+
+ return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
+ cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
+ cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
+ cmsIsTag(hProfile, cmsSigRedTRCTag) &&
+ cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
+ cmsIsTag(hProfile, cmsSigBlueTRCTag));
+
+ default:
+
+ return FALSE;
+ }
+}
+
+// Returns TRUE if the intent is implemented as CLUT
+cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
+{
+ const cmsTagSignature* TagTable;
+
+ // For devicelinks, the supported intent is that one stated in the header
+ if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
+ return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
+ }
+
+ switch (UsedDirection) {
+
+ case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
+ case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
+
+ // For proofing, we need rel. colorimetric in output. Let's do some recursion
+ case LCMS_USED_AS_PROOF:
+ return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
+ cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
+
+ default:
+ cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
+ return FALSE;
+ }
+
+ return cmsIsTag(hProfile, TagTable[Intent]);
+
+}
+
+
+// Return info about supported intents
+cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
+ cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
+{
+
+ if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
+
+ // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
+ // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
+ // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
+ // the accuracy would be less than optimal in rel.col and v2 case.
+
+ return cmsIsMatrixShaper(hProfile);
+}
+
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Read both, profile sequence description and profile sequence id if present. Then combine both to
+// create qa unique structure holding both. Shame on ICC to store things in such complicated way.
+cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
+{
+ cmsSEQ* ProfileSeq;
+ cmsSEQ* ProfileId;
+ cmsSEQ* NewSeq;
+ cmsUInt32Number i;
+
+ // Take profile sequence description first
+ ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
+
+ // Take profile sequence ID
+ ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
+
+ if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
+
+ if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
+ if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
+
+ // We have to mix both together. For that they must agree
+ if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
+
+ NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
+
+ // Ok, proceed to the mixing
+ if (NewSeq != NULL) {
+ for (i=0; i < ProfileSeq ->n; i++) {
+
+ memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
+ NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
+ }
+ }
+ return NewSeq;
+}
+
+// Dump the contents of profile sequence in both tags (if v4 available)
+cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
+{
+ if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
+
+ if (cmsGetProfileVersion(hProfile) >= 4.0) {
+
+ if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+// Auxiliar, read and duplicate a MLU if found.
+static
+cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
+{
+ cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
+ if (mlu == NULL) return NULL;
+
+ return cmsMLUdup(mlu);
+}
+
+// Create a sequence description out of an array of profiles
+cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
+{
+ cmsUInt32Number i;
+ cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
+
+ if (seq == NULL) return NULL;
+
+ for (i=0; i < nProfiles; i++) {
+
+ cmsPSEQDESC* ps = &seq ->seq[i];
+ cmsHPROFILE h = hProfiles[i];
+ cmsTechnologySignature* techpt;
+
+ cmsGetHeaderAttributes(h, &ps ->attributes);
+ cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
+ ps ->deviceMfg = cmsGetHeaderManufacturer(h);
+ ps ->deviceModel = cmsGetHeaderModel(h);
+
+ techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
+ if (techpt == NULL)
+ ps ->technology = (cmsTechnologySignature) 0;
+ else
+ ps ->technology = *techpt;
+
+ ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
+ ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
+ ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
+
+ }
+
+ return seq;
+}
+
+// -------------------------------------------------------------------------------------------------------------------
+
+
+static
+const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
+{
+ cmsTagSignature sig;
+
+ switch (Info) {
+
+ case cmsInfoDescription:
+ sig = cmsSigProfileDescriptionTag;
+ break;
+
+ case cmsInfoManufacturer:
+ sig = cmsSigDeviceMfgDescTag;
+ break;
+
+ case cmsInfoModel:
+ sig = cmsSigDeviceModelDescTag;
+ break;
+
+ case cmsInfoCopyright:
+ sig = cmsSigCopyrightTag;
+ break;
+
+ default: return NULL;
+ }
+
+
+ return (cmsMLU*) cmsReadTag(hProfile, sig);
+}
+
+
+
+cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ wchar_t* Buffer, cmsUInt32Number BufferSize)
+{
+ const cmsMLU* mlu = GetInfo(hProfile, Info);
+ if (mlu == NULL) return 0;
+
+ return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
+}
+
+
+cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ char* Buffer, cmsUInt32Number BufferSize)
+{
+ const cmsMLU* mlu = GetInfo(hProfile, Info);
+ if (mlu == NULL) return 0;
+
+ return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmslut.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmslut.c
new file mode 100644
index 0000000000..c4f93467ca
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmslut.c
@@ -0,0 +1,1793 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+
+// Allocates an empty multi profile element
+cmsStage* CMSEXPORT _cmsStageAllocPlaceholder(cmsContext ContextID,
+ cmsStageSignature Type,
+ cmsUInt32Number InputChannels,
+ cmsUInt32Number OutputChannels,
+ _cmsStageEvalFn EvalPtr,
+ _cmsStageDupElemFn DupElemPtr,
+ _cmsStageFreeElemFn FreePtr,
+ void* Data)
+{
+ cmsStage* ph = (cmsStage*) _cmsMallocZero(ContextID, sizeof(cmsStage));
+
+ if (ph == NULL) return NULL;
+
+
+ ph ->ContextID = ContextID;
+
+ ph ->Type = Type;
+ ph ->Implements = Type; // By default, no clue on what is implementing
+
+ ph ->InputChannels = InputChannels;
+ ph ->OutputChannels = OutputChannels;
+ ph ->EvalPtr = EvalPtr;
+ ph ->DupElemPtr = DupElemPtr;
+ ph ->FreePtr = FreePtr;
+ ph ->Data = Data;
+
+ return ph;
+}
+
+
+static
+void EvaluateIdentity(const cmsFloat32Number In[],
+ cmsFloat32Number Out[],
+ const cmsStage *mpe)
+{
+ memmove(Out, In, mpe ->InputChannels * sizeof(cmsFloat32Number));
+}
+
+
+cmsStage* CMSEXPORT cmsStageAllocIdentity(cmsContext ContextID, cmsUInt32Number nChannels)
+{
+ return _cmsStageAllocPlaceholder(ContextID,
+ cmsSigIdentityElemType,
+ nChannels, nChannels,
+ EvaluateIdentity,
+ NULL,
+ NULL,
+ NULL);
+ }
+
+// Conversion functions. From floating point to 16 bits
+static
+void FromFloatTo16(const cmsFloat32Number In[], cmsUInt16Number Out[], cmsUInt32Number n)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < n; i++) {
+ Out[i] = _cmsQuickSaturateWord(In[i] * 65535.0);
+ }
+}
+
+// From 16 bits to floating point
+static
+void From16ToFloat(const cmsUInt16Number In[], cmsFloat32Number Out[], cmsUInt32Number n)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < n; i++) {
+ Out[i] = (cmsFloat32Number) In[i] / 65535.0F;
+ }
+}
+
+
+// This function is quite useful to analyze the structure of a LUT and retrieve the MPE elements
+// that conform the LUT. It should be called with the LUT, the number of expected elements and
+// then a list of expected types followed with a list of cmsFloat64Number pointers to MPE elements. If
+// the function founds a match with current pipeline, it fills the pointers and returns TRUE
+// if not, returns FALSE without touching anything. Setting pointers to NULL does bypass
+// the storage process.
+cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(const cmsPipeline* Lut, cmsUInt32Number n, ...)
+{
+ va_list args;
+ cmsUInt32Number i;
+ cmsStage* mpe;
+ cmsStageSignature Type;
+ void** ElemPtr;
+
+ // Make sure same number of elements
+ if (cmsPipelineStageCount(Lut) != n) return FALSE;
+
+ va_start(args, n);
+
+ // Iterate across asked types
+ mpe = Lut ->Elements;
+ for (i=0; i < n; i++) {
+
+ // Get asked type
+ Type = (cmsStageSignature)va_arg(args, cmsStageSignature);
+ if (mpe ->Type != Type) {
+
+ va_end(args); // Mismatch. We are done.
+ return FALSE;
+ }
+ mpe = mpe ->Next;
+ }
+
+ // Found a combination, fill pointers if not NULL
+ mpe = Lut ->Elements;
+ for (i=0; i < n; i++) {
+
+ ElemPtr = va_arg(args, void**);
+ if (ElemPtr != NULL)
+ *ElemPtr = mpe;
+
+ mpe = mpe ->Next;
+ }
+
+ va_end(args);
+ return TRUE;
+}
+
+// Below there are implementations for several types of elements. Each type may be implemented by a
+// evaluation function, a duplication function, a function to free resources and a constructor.
+
+// *************************************************************************************************
+// Type cmsSigCurveSetElemType (curves)
+// *************************************************************************************************
+
+cmsToneCurve** _cmsStageGetPtrToCurveSet(const cmsStage* mpe)
+{
+ _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data;
+
+ return Data ->TheCurves;
+}
+
+static
+void EvaluateCurves(const cmsFloat32Number In[],
+ cmsFloat32Number Out[],
+ const cmsStage *mpe)
+{
+ _cmsStageToneCurvesData* Data;
+ cmsUInt32Number i;
+
+ _cmsAssert(mpe != NULL);
+
+ Data = (_cmsStageToneCurvesData*) mpe ->Data;
+ if (Data == NULL) return;
+
+ if (Data ->TheCurves == NULL) return;
+
+ for (i=0; i < Data ->nCurves; i++) {
+ Out[i] = cmsEvalToneCurveFloat(Data ->TheCurves[i], In[i]);
+ }
+}
+
+static
+void CurveSetElemTypeFree(cmsStage* mpe)
+{
+ _cmsStageToneCurvesData* Data;
+ cmsUInt32Number i;
+
+ _cmsAssert(mpe != NULL);
+
+ Data = (_cmsStageToneCurvesData*) mpe ->Data;
+ if (Data == NULL) return;
+
+ if (Data ->TheCurves != NULL) {
+ for (i=0; i < Data ->nCurves; i++) {
+ if (Data ->TheCurves[i] != NULL)
+ cmsFreeToneCurve(Data ->TheCurves[i]);
+ }
+ }
+ _cmsFree(mpe ->ContextID, Data ->TheCurves);
+ _cmsFree(mpe ->ContextID, Data);
+}
+
+
+static
+void* CurveSetDup(cmsStage* mpe)
+{
+ _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data;
+ _cmsStageToneCurvesData* NewElem;
+ cmsUInt32Number i;
+
+ NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageToneCurvesData));
+ if (NewElem == NULL) return NULL;
+
+ NewElem ->nCurves = Data ->nCurves;
+ NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(mpe ->ContextID, NewElem ->nCurves, sizeof(cmsToneCurve*));
+
+ if (NewElem ->TheCurves == NULL) goto Error;
+
+ for (i=0; i < NewElem ->nCurves; i++) {
+
+ // Duplicate each curve. It may fail.
+ NewElem ->TheCurves[i] = cmsDupToneCurve(Data ->TheCurves[i]);
+ if (NewElem ->TheCurves[i] == NULL) goto Error;
+
+
+ }
+ return (void*) NewElem;
+
+Error:
+
+ if (NewElem ->TheCurves != NULL) {
+ for (i=0; i < NewElem ->nCurves; i++) {
+ if (NewElem ->TheCurves[i])
+ cmsFreeToneCurve(NewElem ->TheCurves[i]);
+ }
+ }
+ _cmsFree(mpe ->ContextID, NewElem ->TheCurves);
+ _cmsFree(mpe ->ContextID, NewElem);
+ return NULL;
+}
+
+
+// Curves == NULL forces identity curves
+cmsStage* CMSEXPORT cmsStageAllocToneCurves(cmsContext ContextID, cmsUInt32Number nChannels, cmsToneCurve* const Curves[])
+{
+ cmsUInt32Number i;
+ _cmsStageToneCurvesData* NewElem;
+ cmsStage* NewMPE;
+
+
+ NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCurveSetElemType, nChannels, nChannels,
+ EvaluateCurves, CurveSetDup, CurveSetElemTypeFree, NULL );
+ if (NewMPE == NULL) return NULL;
+
+ NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(ContextID, sizeof(_cmsStageToneCurvesData));
+ if (NewElem == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ NewMPE ->Data = (void*) NewElem;
+
+ NewElem ->nCurves = nChannels;
+ NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(ContextID, nChannels, sizeof(cmsToneCurve*));
+ if (NewElem ->TheCurves == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ for (i=0; i < nChannels; i++) {
+
+ if (Curves == NULL) {
+ NewElem ->TheCurves[i] = cmsBuildGamma(ContextID, 1.0);
+ }
+ else {
+ NewElem ->TheCurves[i] = cmsDupToneCurve(Curves[i]);
+ }
+
+ if (NewElem ->TheCurves[i] == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ }
+
+ return NewMPE;
+}
+
+
+// Create a bunch of identity curves
+cmsStage* _cmsStageAllocIdentityCurves(cmsContext ContextID, int nChannels)
+{
+ cmsStage* mpe = cmsStageAllocToneCurves(ContextID, nChannels, NULL);
+
+ if (mpe == NULL) return NULL;
+ mpe ->Implements = cmsSigIdentityElemType;
+ return mpe;
+}
+
+
+// *************************************************************************************************
+// Type cmsSigMatrixElemType (Matrices)
+// *************************************************************************************************
+
+
+// Special care should be taken here because precision loss. A temporary cmsFloat64Number buffer is being used
+static
+void EvaluateMatrix(const cmsFloat32Number In[],
+ cmsFloat32Number Out[],
+ const cmsStage *mpe)
+{
+ cmsUInt32Number i, j;
+ _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
+ cmsFloat64Number Tmp;
+
+ // Input is already in 0..1.0 notation
+ for (i=0; i < mpe ->OutputChannels; i++) {
+
+ Tmp = 0;
+ for (j=0; j < mpe->InputChannels; j++) {
+ Tmp += In[j] * Data->Double[i*mpe->InputChannels + j];
+ }
+
+ if (Data ->Offset != NULL)
+ Tmp += Data->Offset[i];
+
+ Out[i] = (cmsFloat32Number) Tmp;
+ }
+
+
+ // Output in 0..1.0 domain
+}
+
+
+// Duplicate a yet-existing matrix element
+static
+void* MatrixElemDup(cmsStage* mpe)
+{
+ _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
+ _cmsStageMatrixData* NewElem;
+ cmsUInt32Number sz;
+
+ NewElem = (_cmsStageMatrixData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageMatrixData));
+ if (NewElem == NULL) return NULL;
+
+ sz = mpe ->InputChannels * mpe ->OutputChannels;
+
+ NewElem ->Double = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID, Data ->Double, sz * sizeof(cmsFloat64Number)) ;
+
+ if (Data ->Offset)
+ NewElem ->Offset = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID,
+ Data ->Offset, mpe -> OutputChannels * sizeof(cmsFloat64Number)) ;
+
+ return (void*) NewElem;
+}
+
+
+static
+void MatrixElemTypeFree(cmsStage* mpe)
+{
+ _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
+ if (Data == NULL)
+ return;
+ if (Data ->Double)
+ _cmsFree(mpe ->ContextID, Data ->Double);
+
+ if (Data ->Offset)
+ _cmsFree(mpe ->ContextID, Data ->Offset);
+
+ _cmsFree(mpe ->ContextID, mpe ->Data);
+}
+
+
+
+cmsStage* CMSEXPORT cmsStageAllocMatrix(cmsContext ContextID, cmsUInt32Number Rows, cmsUInt32Number Cols,
+ const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset)
+{
+ cmsUInt32Number i, n;
+ _cmsStageMatrixData* NewElem;
+ cmsStage* NewMPE;
+
+ n = Rows * Cols;
+
+ // Check for overflow
+ if (n == 0) return NULL;
+ if (n >= UINT_MAX / Cols) return NULL;
+ if (n >= UINT_MAX / Rows) return NULL;
+ if (n < Rows || n < Cols) return NULL;
+
+ NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows,
+ EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL );
+ if (NewMPE == NULL) return NULL;
+
+
+ NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData));
+ if (NewElem == NULL) return NULL;
+
+
+ NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number));
+
+ if (NewElem->Double == NULL) {
+ MatrixElemTypeFree(NewMPE);
+ return NULL;
+ }
+
+ for (i=0; i < n; i++) {
+ NewElem ->Double[i] = Matrix[i];
+ }
+
+
+ if (Offset != NULL) {
+
+ NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Cols, sizeof(cmsFloat64Number));
+ if (NewElem->Offset == NULL) {
+ MatrixElemTypeFree(NewMPE);
+ return NULL;
+ }
+
+ for (i=0; i < Cols; i++) {
+ NewElem ->Offset[i] = Offset[i];
+ }
+
+ }
+
+ NewMPE ->Data = (void*) NewElem;
+ return NewMPE;
+}
+
+
+// *************************************************************************************************
+// Type cmsSigCLutElemType
+// *************************************************************************************************
+
+
+// Evaluate in true floating point
+static
+void EvaluateCLUTfloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
+{
+ _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
+
+ Data -> Params ->Interpolation.LerpFloat(In, Out, Data->Params);
+}
+
+
+// Convert to 16 bits, evaluate, and back to floating point
+static
+void EvaluateCLUTfloatIn16(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
+{
+ _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
+ cmsUInt16Number In16[MAX_STAGE_CHANNELS], Out16[MAX_STAGE_CHANNELS];
+
+ _cmsAssert(mpe ->InputChannels <= MAX_STAGE_CHANNELS);
+ _cmsAssert(mpe ->OutputChannels <= MAX_STAGE_CHANNELS);
+
+ FromFloatTo16(In, In16, mpe ->InputChannels);
+ Data -> Params ->Interpolation.Lerp16(In16, Out16, Data->Params);
+ From16ToFloat(Out16, Out, mpe ->OutputChannels);
+}
+
+
+// Given an hypercube of b dimensions, with Dims[] number of nodes by dimension, calculate the total amount of nodes
+static
+cmsUInt32Number CubeSize(const cmsUInt32Number Dims[], cmsUInt32Number b)
+{
+ cmsUInt32Number rv, dim;
+
+ _cmsAssert(Dims != NULL);
+
+ for (rv = 1; b > 0; b--) {
+
+ dim = Dims[b-1];
+ if (dim == 0) return 0; // Error
+
+ rv *= dim;
+
+ // Check for overflow
+ if (rv > UINT_MAX / dim) return 0;
+ }
+
+ return rv;
+}
+
+static
+void* CLUTElemDup(cmsStage* mpe)
+{
+ _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
+ _cmsStageCLutData* NewElem;
+
+
+ NewElem = (_cmsStageCLutData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageCLutData));
+ if (NewElem == NULL) return NULL;
+
+ NewElem ->nEntries = Data ->nEntries;
+ NewElem ->HasFloatValues = Data ->HasFloatValues;
+
+ if (Data ->Tab.T) {
+
+ if (Data ->HasFloatValues) {
+ NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.TFloat, Data ->nEntries * sizeof (cmsFloat32Number));
+ if (NewElem ->Tab.TFloat == NULL)
+ goto Error;
+ } else {
+ NewElem ->Tab.T = (cmsUInt16Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.T, Data ->nEntries * sizeof (cmsUInt16Number));
+ if (NewElem ->Tab.TFloat == NULL)
+ goto Error;
+ }
+ }
+
+ NewElem ->Params = _cmsComputeInterpParamsEx(mpe ->ContextID,
+ Data ->Params ->nSamples,
+ Data ->Params ->nInputs,
+ Data ->Params ->nOutputs,
+ NewElem ->Tab.T,
+ Data ->Params ->dwFlags);
+ if (NewElem->Params != NULL)
+ return (void*) NewElem;
+ Error:
+ if (NewElem->Tab.T)
+ // This works for both types
+ _cmsFree(mpe ->ContextID, NewElem -> Tab.T);
+ _cmsFree(mpe ->ContextID, NewElem);
+ return NULL;
+}
+
+
+static
+void CLutElemTypeFree(cmsStage* mpe)
+{
+
+ _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
+
+ // Already empty
+ if (Data == NULL) return;
+
+ // This works for both types
+ if (Data -> Tab.T)
+ _cmsFree(mpe ->ContextID, Data -> Tab.T);
+
+ _cmsFreeInterpParams(Data ->Params);
+ _cmsFree(mpe ->ContextID, mpe ->Data);
+}
+
+
+// Allocates a 16-bit multidimensional CLUT. This is evaluated at 16-bit precision. Table may have different
+// granularity on each dimension.
+cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID,
+ const cmsUInt32Number clutPoints[],
+ cmsUInt32Number inputChan,
+ cmsUInt32Number outputChan,
+ const cmsUInt16Number* Table)
+{
+ cmsUInt32Number i, n;
+ _cmsStageCLutData* NewElem;
+ cmsStage* NewMPE;
+
+ _cmsAssert(clutPoints != NULL);
+
+ if (inputChan > MAX_INPUT_DIMENSIONS) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS);
+ return NULL;
+ }
+
+ NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan,
+ EvaluateCLUTfloatIn16, CLUTElemDup, CLutElemTypeFree, NULL );
+
+ if (NewMPE == NULL) return NULL;
+
+ NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData));
+ if (NewElem == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ NewMPE ->Data = (void*) NewElem;
+
+ NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan);
+ NewElem -> HasFloatValues = FALSE;
+
+ if (n == 0) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+
+ NewElem ->Tab.T = (cmsUInt16Number*) _cmsCalloc(ContextID, n, sizeof(cmsUInt16Number));
+ if (NewElem ->Tab.T == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ if (Table != NULL) {
+ for (i=0; i < n; i++) {
+ NewElem ->Tab.T[i] = Table[i];
+ }
+ }
+
+ NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.T, CMS_LERP_FLAGS_16BITS);
+ if (NewElem ->Params == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ return NewMPE;
+}
+
+cmsStage* CMSEXPORT cmsStageAllocCLut16bit(cmsContext ContextID,
+ cmsUInt32Number nGridPoints,
+ cmsUInt32Number inputChan,
+ cmsUInt32Number outputChan,
+ const cmsUInt16Number* Table)
+{
+ cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+ int i;
+
+ // Our resulting LUT would be same gridpoints on all dimensions
+ for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
+ Dimensions[i] = nGridPoints;
+
+ return cmsStageAllocCLut16bitGranular(ContextID, Dimensions, inputChan, outputChan, Table);
+}
+
+
+cmsStage* CMSEXPORT cmsStageAllocCLutFloat(cmsContext ContextID,
+ cmsUInt32Number nGridPoints,
+ cmsUInt32Number inputChan,
+ cmsUInt32Number outputChan,
+ const cmsFloat32Number* Table)
+{
+ cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+ int i;
+
+ // Our resulting LUT would be same gridpoints on all dimensions
+ for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
+ Dimensions[i] = nGridPoints;
+
+ return cmsStageAllocCLutFloatGranular(ContextID, Dimensions, inputChan, outputChan, Table);
+}
+
+
+
+cmsStage* CMSEXPORT cmsStageAllocCLutFloatGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table)
+{
+ cmsUInt32Number i, n;
+ _cmsStageCLutData* NewElem;
+ cmsStage* NewMPE;
+
+ _cmsAssert(clutPoints != NULL);
+
+ if (inputChan > MAX_INPUT_DIMENSIONS) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS);
+ return NULL;
+ }
+
+ NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan,
+ EvaluateCLUTfloat, CLUTElemDup, CLutElemTypeFree, NULL);
+ if (NewMPE == NULL) return NULL;
+
+
+ NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData));
+ if (NewElem == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ NewMPE ->Data = (void*) NewElem;
+
+ // There is a potential integer overflow on conputing n and nEntries.
+ NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan);
+ NewElem -> HasFloatValues = TRUE;
+
+ if (n == 0) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat32Number));
+ if (NewElem ->Tab.TFloat == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ if (Table != NULL) {
+ for (i=0; i < n; i++) {
+ NewElem ->Tab.TFloat[i] = Table[i];
+ }
+ }
+
+ NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT);
+ if (NewElem ->Params == NULL) {
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ return NewMPE;
+}
+
+
+static
+int IdentitySampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
+{
+ int nChan = *(int*) Cargo;
+ int i;
+
+ for (i=0; i < nChan; i++)
+ Out[i] = In[i];
+
+ return 1;
+}
+
+// Creates an MPE that just copies input to output
+cmsStage* _cmsStageAllocIdentityCLut(cmsContext ContextID, int nChan)
+{
+ cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+ cmsStage* mpe ;
+ int i;
+
+ for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
+ Dimensions[i] = 2;
+
+ mpe = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, nChan, nChan, NULL);
+ if (mpe == NULL) return NULL;
+
+ if (!cmsStageSampleCLut16bit(mpe, IdentitySampler, &nChan, 0)) {
+ cmsStageFree(mpe);
+ return NULL;
+ }
+
+ mpe ->Implements = cmsSigIdentityElemType;
+ return mpe;
+}
+
+
+
+// Quantize a value 0 <= i < MaxSamples to 0..0xffff
+cmsUInt16Number _cmsQuantizeVal(cmsFloat64Number i, int MaxSamples)
+{
+ cmsFloat64Number x;
+
+ x = ((cmsFloat64Number) i * 65535.) / (cmsFloat64Number) (MaxSamples - 1);
+ return _cmsQuickSaturateWord(x);
+}
+
+
+// This routine does a sweep on whole input space, and calls its callback
+// function on knots. returns TRUE if all ok, FALSE otherwise.
+cmsBool CMSEXPORT cmsStageSampleCLut16bit(cmsStage* mpe, cmsSAMPLER16 Sampler, void * Cargo, cmsUInt32Number dwFlags)
+{
+ int i, t, nTotalPoints, index, rest;
+ int nInputs, nOutputs;
+ cmsUInt32Number* nSamples;
+ cmsUInt16Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS];
+ _cmsStageCLutData* clut;
+
+ if (mpe == NULL) return FALSE;
+
+ clut = (_cmsStageCLutData*) mpe->Data;
+
+ if (clut == NULL) return FALSE;
+
+ nSamples = clut->Params ->nSamples;
+ nInputs = clut->Params ->nInputs;
+ nOutputs = clut->Params ->nOutputs;
+
+ if (nInputs <= 0) return FALSE;
+ if (nOutputs <= 0) return FALSE;
+ if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE;
+ if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE;
+
+ nTotalPoints = CubeSize(nSamples, nInputs);
+ if (nTotalPoints == 0) return FALSE;
+
+ index = 0;
+ for (i = 0; i < nTotalPoints; i++) {
+
+ rest = i;
+ for (t = nInputs-1; t >=0; --t) {
+
+ cmsUInt32Number Colorant = rest % nSamples[t];
+
+ rest /= nSamples[t];
+
+ In[t] = _cmsQuantizeVal(Colorant, nSamples[t]);
+ }
+
+ if (clut ->Tab.T != NULL) {
+ for (t=0; t < nOutputs; t++)
+ Out[t] = clut->Tab.T[index + t];
+ }
+
+ if (!Sampler(In, Out, Cargo))
+ return FALSE;
+
+ if (!(dwFlags & SAMPLER_INSPECT)) {
+
+ if (clut ->Tab.T != NULL) {
+ for (t=0; t < nOutputs; t++)
+ clut->Tab.T[index + t] = Out[t];
+ }
+ }
+
+ index += nOutputs;
+ }
+
+ return TRUE;
+}
+
+// Same as anterior, but for floting point
+cmsBool CMSEXPORT cmsStageSampleCLutFloat(cmsStage* mpe, cmsSAMPLERFLOAT Sampler, void * Cargo, cmsUInt32Number dwFlags)
+{
+ int i, t, nTotalPoints, index, rest;
+ int nInputs, nOutputs;
+ cmsUInt32Number* nSamples;
+ cmsFloat32Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS];
+ _cmsStageCLutData* clut = (_cmsStageCLutData*) mpe->Data;
+
+ nSamples = clut->Params ->nSamples;
+ nInputs = clut->Params ->nInputs;
+ nOutputs = clut->Params ->nOutputs;
+
+ if (nInputs <= 0) return FALSE;
+ if (nOutputs <= 0) return FALSE;
+ if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE;
+ if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE;
+
+ nTotalPoints = CubeSize(nSamples, nInputs);
+ if (nTotalPoints == 0) return FALSE;
+
+ index = 0;
+ for (i = 0; i < nTotalPoints; i++) {
+
+ rest = i;
+ for (t = nInputs-1; t >=0; --t) {
+
+ cmsUInt32Number Colorant = rest % nSamples[t];
+
+ rest /= nSamples[t];
+
+ In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, nSamples[t]) / 65535.0);
+ }
+
+ if (clut ->Tab.TFloat != NULL) {
+ for (t=0; t < nOutputs; t++)
+ Out[t] = clut->Tab.TFloat[index + t];
+ }
+
+ if (!Sampler(In, Out, Cargo))
+ return FALSE;
+
+ if (!(dwFlags & SAMPLER_INSPECT)) {
+
+ if (clut ->Tab.TFloat != NULL) {
+ for (t=0; t < nOutputs; t++)
+ clut->Tab.TFloat[index + t] = Out[t];
+ }
+ }
+
+ index += nOutputs;
+ }
+
+ return TRUE;
+}
+
+
+
+// This routine does a sweep on whole input space, and calls its callback
+// function on knots. returns TRUE if all ok, FALSE otherwise.
+cmsBool CMSEXPORT cmsSliceSpace16(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
+ cmsSAMPLER16 Sampler, void * Cargo)
+{
+ int i, t, nTotalPoints, rest;
+ cmsUInt16Number In[cmsMAXCHANNELS];
+
+ if (nInputs >= cmsMAXCHANNELS) return FALSE;
+
+ nTotalPoints = CubeSize(clutPoints, nInputs);
+ if (nTotalPoints == 0) return FALSE;
+
+ for (i = 0; i < nTotalPoints; i++) {
+
+ rest = i;
+ for (t = nInputs-1; t >=0; --t) {
+
+ cmsUInt32Number Colorant = rest % clutPoints[t];
+
+ rest /= clutPoints[t];
+ In[t] = _cmsQuantizeVal(Colorant, clutPoints[t]);
+
+ }
+
+ if (!Sampler(In, NULL, Cargo))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+cmsInt32Number CMSEXPORT cmsSliceSpaceFloat(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
+ cmsSAMPLERFLOAT Sampler, void * Cargo)
+{
+ int i, t, nTotalPoints, rest;
+ cmsFloat32Number In[cmsMAXCHANNELS];
+
+ if (nInputs >= cmsMAXCHANNELS) return FALSE;
+
+ nTotalPoints = CubeSize(clutPoints, nInputs);
+ if (nTotalPoints == 0) return FALSE;
+
+ for (i = 0; i < nTotalPoints; i++) {
+
+ rest = i;
+ for (t = nInputs-1; t >=0; --t) {
+
+ cmsUInt32Number Colorant = rest % clutPoints[t];
+
+ rest /= clutPoints[t];
+ In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, clutPoints[t]) / 65535.0);
+
+ }
+
+ if (!Sampler(In, NULL, Cargo))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+// ********************************************************************************
+// Type cmsSigLab2XYZElemType
+// ********************************************************************************
+
+
+static
+void EvaluateLab2XYZ(const cmsFloat32Number In[],
+ cmsFloat32Number Out[],
+ const cmsStage *mpe)
+{
+ cmsCIELab Lab;
+ cmsCIEXYZ XYZ;
+ const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ;
+
+ // V4 rules
+ Lab.L = In[0] * 100.0;
+ Lab.a = In[1] * 255.0 - 128.0;
+ Lab.b = In[2] * 255.0 - 128.0;
+
+ cmsLab2XYZ(NULL, &XYZ, &Lab);
+
+ // From XYZ, range 0..19997 to 0..1.0, note that 1.99997 comes from 0xffff
+ // encoded as 1.15 fixed point, so 1 + (32767.0 / 32768.0)
+
+ Out[0] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.X / XYZadj);
+ Out[1] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Y / XYZadj);
+ Out[2] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Z / XYZadj);
+ return;
+
+ cmsUNUSED_PARAMETER(mpe);
+}
+
+
+// No dup or free routines needed, as the structure has no pointers in it.
+cmsStage* _cmsStageAllocLab2XYZ(cmsContext ContextID)
+{
+ return _cmsStageAllocPlaceholder(ContextID, cmsSigLab2XYZElemType, 3, 3, EvaluateLab2XYZ, NULL, NULL, NULL);
+}
+
+// ********************************************************************************
+
+// v2 L=100 is supposed to be placed on 0xFF00. There is no reasonable
+// number of gridpoints that would make exact match. However, a prelinearization
+// of 258 entries, would map 0xFF00 exactly on entry 257, and this is good to avoid scum dot.
+// Almost all what we need but unfortunately, the rest of entries should be scaled by
+// (255*257/256) and this is not exact.
+
+cmsStage* _cmsStageAllocLabV2ToV4curves(cmsContext ContextID)
+{
+ cmsStage* mpe;
+ cmsToneCurve* LabTable[3];
+ int i, j;
+
+ LabTable[0] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
+ LabTable[1] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
+ LabTable[2] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
+
+ for (j=0; j < 3; j++) {
+
+ if (LabTable[j] == NULL) {
+ cmsFreeToneCurveTriple(LabTable);
+ return NULL;
+ }
+
+ // We need to map * (0xffff / 0xff00), thats same as (257 / 256)
+ // So we can use 258-entry tables to do the trick (i / 257) * (255 * 257) * (257 / 256);
+ for (i=0; i < 257; i++) {
+
+ LabTable[j]->Table16[i] = (cmsUInt16Number) ((i * 0xffff + 0x80) >> 8);
+ }
+
+ LabTable[j] ->Table16[257] = 0xffff;
+ }
+
+ mpe = cmsStageAllocToneCurves(ContextID, 3, LabTable);
+ cmsFreeToneCurveTriple(LabTable);
+
+ if (mpe == NULL) return NULL;
+ mpe ->Implements = cmsSigLabV2toV4;
+ return mpe;
+}
+
+// ********************************************************************************
+
+// Matrix-based conversion, which is more accurate, but slower and cannot properly be saved in devicelink profiles
+cmsStage* _cmsStageAllocLabV2ToV4(cmsContext ContextID)
+{
+ static const cmsFloat64Number V2ToV4[] = { 65535.0/65280.0, 0, 0,
+ 0, 65535.0/65280.0, 0,
+ 0, 0, 65535.0/65280.0
+ };
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V2ToV4, NULL);
+
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigLabV2toV4;
+ return mpe;
+}
+
+
+// Reverse direction
+cmsStage* _cmsStageAllocLabV4ToV2(cmsContext ContextID)
+{
+ static const cmsFloat64Number V4ToV2[] = { 65280.0/65535.0, 0, 0,
+ 0, 65280.0/65535.0, 0,
+ 0, 0, 65280.0/65535.0
+ };
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V4ToV2, NULL);
+
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigLabV4toV2;
+ return mpe;
+}
+
+
+// To Lab to float. Note that the MPE gives numbers in normal Lab range
+// and we need 0..1.0 range for the formatters
+// L* : 0...100 => 0...1.0 (L* / 100)
+// ab* : -128..+127 to 0..1 ((ab* + 128) / 255)
+
+cmsStage* _cmsStageNormalizeFromLabFloat(cmsContext ContextID)
+{
+ static const cmsFloat64Number a1[] = {
+ 1.0/100.0, 0, 0,
+ 0, 1.0/255.0, 0,
+ 0, 0, 1.0/255.0
+ };
+
+ static const cmsFloat64Number o1[] = {
+ 0,
+ 128.0/255.0,
+ 128.0/255.0
+ };
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1);
+
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigLab2FloatPCS;
+ return mpe;
+}
+
+// Fom XYZ to floating point PCS
+cmsStage* _cmsStageNormalizeFromXyzFloat(cmsContext ContextID)
+{
+#define n (32768.0/65535.0)
+ static const cmsFloat64Number a1[] = {
+ n, 0, 0,
+ 0, n, 0,
+ 0, 0, n
+ };
+#undef n
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL);
+
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigXYZ2FloatPCS;
+ return mpe;
+}
+
+cmsStage* _cmsStageNormalizeToLabFloat(cmsContext ContextID)
+{
+ static const cmsFloat64Number a1[] = {
+ 100.0, 0, 0,
+ 0, 255.0, 0,
+ 0, 0, 255.0
+ };
+
+ static const cmsFloat64Number o1[] = {
+ 0,
+ -128.0,
+ -128.0
+ };
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1);
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigFloatPCS2Lab;
+ return mpe;
+}
+
+cmsStage* _cmsStageNormalizeToXyzFloat(cmsContext ContextID)
+{
+#define n (65535.0/32768.0)
+
+ static const cmsFloat64Number a1[] = {
+ n, 0, 0,
+ 0, n, 0,
+ 0, 0, n
+ };
+#undef n
+
+ cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL);
+ if (mpe == NULL) return mpe;
+ mpe ->Implements = cmsSigFloatPCS2XYZ;
+ return mpe;
+}
+
+
+
+// ********************************************************************************
+// Type cmsSigXYZ2LabElemType
+// ********************************************************************************
+
+static
+void EvaluateXYZ2Lab(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
+{
+ cmsCIELab Lab;
+ cmsCIEXYZ XYZ;
+ const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ;
+
+ // From 0..1.0 to XYZ
+
+ XYZ.X = In[0] * XYZadj;
+ XYZ.Y = In[1] * XYZadj;
+ XYZ.Z = In[2] * XYZadj;
+
+ cmsXYZ2Lab(NULL, &Lab, &XYZ);
+
+ // From V4 Lab to 0..1.0
+
+ Out[0] = (cmsFloat32Number) (Lab.L / 100.0);
+ Out[1] = (cmsFloat32Number) ((Lab.a + 128.0) / 255.0);
+ Out[2] = (cmsFloat32Number) ((Lab.b + 128.0) / 255.0);
+ return;
+
+ cmsUNUSED_PARAMETER(mpe);
+}
+
+cmsStage* _cmsStageAllocXYZ2Lab(cmsContext ContextID)
+{
+ return _cmsStageAllocPlaceholder(ContextID, cmsSigXYZ2LabElemType, 3, 3, EvaluateXYZ2Lab, NULL, NULL, NULL);
+
+}
+
+// ********************************************************************************
+
+// For v4, S-Shaped curves are placed in a/b axis to increase resolution near gray
+
+cmsStage* _cmsStageAllocLabPrelin(cmsContext ContextID)
+{
+ cmsToneCurve* LabTable[3];
+ cmsFloat64Number Params[1] = {2.4} ;
+
+ LabTable[0] = cmsBuildGamma(ContextID, 1.0);
+ LabTable[1] = cmsBuildParametricToneCurve(ContextID, 108, Params);
+ LabTable[2] = cmsBuildParametricToneCurve(ContextID, 108, Params);
+
+ return cmsStageAllocToneCurves(ContextID, 3, LabTable);
+}
+
+
+// Free a single MPE
+void CMSEXPORT cmsStageFree(cmsStage* mpe)
+{
+ if (mpe ->FreePtr)
+ mpe ->FreePtr(mpe);
+
+ _cmsFree(mpe ->ContextID, mpe);
+}
+
+
+cmsUInt32Number CMSEXPORT cmsStageInputChannels(const cmsStage* mpe)
+{
+ return mpe ->InputChannels;
+}
+
+cmsUInt32Number CMSEXPORT cmsStageOutputChannels(const cmsStage* mpe)
+{
+ return mpe ->OutputChannels;
+}
+
+cmsStageSignature CMSEXPORT cmsStageType(const cmsStage* mpe)
+{
+ return mpe -> Type;
+}
+
+void* CMSEXPORT cmsStageData(const cmsStage* mpe)
+{
+ return mpe -> Data;
+}
+
+cmsStage* CMSEXPORT cmsStageNext(const cmsStage* mpe)
+{
+ return mpe -> Next;
+}
+
+
+// Duplicates an MPE
+cmsStage* CMSEXPORT cmsStageDup(cmsStage* mpe)
+{
+ cmsStage* NewMPE;
+
+ if (mpe == NULL) return NULL;
+ NewMPE = _cmsStageAllocPlaceholder(mpe ->ContextID,
+ mpe ->Type,
+ mpe ->InputChannels,
+ mpe ->OutputChannels,
+ mpe ->EvalPtr,
+ mpe ->DupElemPtr,
+ mpe ->FreePtr,
+ NULL);
+ if (NewMPE == NULL) return NULL;
+
+ NewMPE ->Implements = mpe ->Implements;
+
+ if (mpe ->DupElemPtr) {
+
+ NewMPE ->Data = mpe ->DupElemPtr(mpe);
+
+ if (NewMPE->Data == NULL) {
+
+ cmsStageFree(NewMPE);
+ return NULL;
+ }
+
+ } else {
+
+ NewMPE ->Data = NULL;
+ }
+
+ return NewMPE;
+}
+
+
+// ***********************************************************************************************************
+
+// This function sets up the channel count
+
+static
+void BlessLUT(cmsPipeline* lut)
+{
+ // We can set the input/ouput channels only if we have elements.
+ if (lut ->Elements != NULL) {
+
+ cmsStage *First, *Last;
+
+ First = cmsPipelineGetPtrToFirstStage(lut);
+ Last = cmsPipelineGetPtrToLastStage(lut);
+
+ if (First != NULL)lut ->InputChannels = First ->InputChannels;
+ if (Last != NULL) lut ->OutputChannels = Last ->OutputChannels;
+ }
+}
+
+
+// Default to evaluate the LUT on 16 bit-basis. Precision is retained.
+static
+void _LUTeval16(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register const void* D)
+{
+ cmsPipeline* lut = (cmsPipeline*) D;
+ cmsStage *mpe;
+ cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
+ int Phase = 0, NextPhase;
+
+ From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels);
+
+ for (mpe = lut ->Elements;
+ mpe != NULL;
+ mpe = mpe ->Next) {
+
+ NextPhase = Phase ^ 1;
+ mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
+ Phase = NextPhase;
+ }
+
+
+ FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels);
+}
+
+
+
+// Does evaluate the LUT on cmsFloat32Number-basis.
+static
+void _LUTevalFloat(register const cmsFloat32Number In[], register cmsFloat32Number Out[], const void* D)
+{
+ cmsPipeline* lut = (cmsPipeline*) D;
+ cmsStage *mpe;
+ cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS];
+ int Phase = 0, NextPhase;
+
+ memmove(&Storage[Phase][0], In, lut ->InputChannels * sizeof(cmsFloat32Number));
+
+ for (mpe = lut ->Elements;
+ mpe != NULL;
+ mpe = mpe ->Next) {
+
+ NextPhase = Phase ^ 1;
+ mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
+ Phase = NextPhase;
+ }
+
+ memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number));
+}
+
+
+
+
+// LUT Creation & Destruction
+
+cmsPipeline* CMSEXPORT cmsPipelineAlloc(cmsContext ContextID, cmsUInt32Number InputChannels, cmsUInt32Number OutputChannels)
+{
+ cmsPipeline* NewLUT;
+
+ if (InputChannels >= cmsMAXCHANNELS ||
+ OutputChannels >= cmsMAXCHANNELS) return NULL;
+
+ NewLUT = (cmsPipeline*) _cmsMallocZero(ContextID, sizeof(cmsPipeline));
+ if (NewLUT == NULL) return NULL;
+
+
+ NewLUT -> InputChannels = InputChannels;
+ NewLUT -> OutputChannels = OutputChannels;
+
+ NewLUT ->Eval16Fn = _LUTeval16;
+ NewLUT ->EvalFloatFn = _LUTevalFloat;
+ NewLUT ->DupDataFn = NULL;
+ NewLUT ->FreeDataFn = NULL;
+ NewLUT ->Data = NewLUT;
+ NewLUT ->ContextID = ContextID;
+
+ BlessLUT(NewLUT);
+
+ return NewLUT;
+}
+
+cmsContext CMSEXPORT cmsGetPipelineContextID(const cmsPipeline* lut)
+{
+ _cmsAssert(lut != NULL);
+ return lut ->ContextID;
+}
+
+cmsUInt32Number CMSEXPORT cmsPipelineInputChannels(const cmsPipeline* lut)
+{
+ _cmsAssert(lut != NULL);
+ return lut ->InputChannels;
+}
+
+cmsUInt32Number CMSEXPORT cmsPipelineOutputChannels(const cmsPipeline* lut)
+{
+ _cmsAssert(lut != NULL);
+ return lut ->OutputChannels;
+}
+
+// Free a profile elements LUT
+void CMSEXPORT cmsPipelineFree(cmsPipeline* lut)
+{
+ cmsStage *mpe, *Next;
+
+ if (lut == NULL) return;
+
+ for (mpe = lut ->Elements;
+ mpe != NULL;
+ mpe = Next) {
+
+ Next = mpe ->Next;
+ cmsStageFree(mpe);
+ }
+
+ if (lut ->FreeDataFn) lut ->FreeDataFn(lut ->ContextID, lut ->Data);
+
+ _cmsFree(lut ->ContextID, lut);
+}
+
+
+// Default to evaluate the LUT on 16 bit-basis.
+void CMSEXPORT cmsPipelineEval16(const cmsUInt16Number In[], cmsUInt16Number Out[], const cmsPipeline* lut)
+{
+ _cmsAssert(lut != NULL);
+ lut ->Eval16Fn(In, Out, lut->Data);
+}
+
+
+// Does evaluate the LUT on cmsFloat32Number-basis.
+void CMSEXPORT cmsPipelineEvalFloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsPipeline* lut)
+{
+ _cmsAssert(lut != NULL);
+ lut ->EvalFloatFn(In, Out, lut);
+}
+
+
+
+// Duplicates a LUT
+cmsPipeline* CMSEXPORT cmsPipelineDup(const cmsPipeline* lut)
+{
+ cmsPipeline* NewLUT;
+ cmsStage *NewMPE, *Anterior = NULL, *mpe;
+ cmsBool First = TRUE;
+
+ if (lut == NULL) return NULL;
+
+ NewLUT = cmsPipelineAlloc(lut ->ContextID, lut ->InputChannels, lut ->OutputChannels);
+ if (NewLUT == NULL) return NULL;
+
+ for (mpe = lut ->Elements;
+ mpe != NULL;
+ mpe = mpe ->Next) {
+
+ NewMPE = cmsStageDup(mpe);
+
+ if (NewMPE == NULL) {
+ cmsPipelineFree(NewLUT);
+ return NULL;
+ }
+
+ if (First) {
+ NewLUT ->Elements = NewMPE;
+ First = FALSE;
+ }
+ else {
+ Anterior ->Next = NewMPE;
+ }
+
+ Anterior = NewMPE;
+ }
+
+ NewLUT ->Eval16Fn = lut ->Eval16Fn;
+ NewLUT ->EvalFloatFn = lut ->EvalFloatFn;
+ NewLUT ->DupDataFn = lut ->DupDataFn;
+ NewLUT ->FreeDataFn = lut ->FreeDataFn;
+
+ if (NewLUT ->DupDataFn != NULL)
+ NewLUT ->Data = NewLUT ->DupDataFn(lut ->ContextID, lut->Data);
+
+
+ NewLUT ->SaveAs8Bits = lut ->SaveAs8Bits;
+
+ BlessLUT(NewLUT);
+ return NewLUT;
+}
+
+
+int CMSEXPORT cmsPipelineInsertStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage* mpe)
+{
+ cmsStage* Anterior = NULL, *pt;
+
+ if (lut == NULL || mpe == NULL)
+ return FALSE;
+
+ switch (loc) {
+
+ case cmsAT_BEGIN:
+ mpe ->Next = lut ->Elements;
+ lut ->Elements = mpe;
+ break;
+
+ case cmsAT_END:
+
+ if (lut ->Elements == NULL)
+ lut ->Elements = mpe;
+ else {
+
+ for (pt = lut ->Elements;
+ pt != NULL;
+ pt = pt -> Next) Anterior = pt;
+
+ Anterior ->Next = mpe;
+ mpe ->Next = NULL;
+ }
+ break;
+ default:;
+ return FALSE;
+ }
+
+ BlessLUT(lut);
+ return TRUE;
+}
+
+// Unlink an element and return the pointer to it
+void CMSEXPORT cmsPipelineUnlinkStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe)
+{
+ cmsStage *Anterior, *pt, *Last;
+ cmsStage *Unlinked = NULL;
+
+
+ // If empty LUT, there is nothing to remove
+ if (lut ->Elements == NULL) {
+ if (mpe) *mpe = NULL;
+ return;
+ }
+
+ // On depending on the strategy...
+ switch (loc) {
+
+ case cmsAT_BEGIN:
+ {
+ cmsStage* elem = lut ->Elements;
+
+ lut ->Elements = elem -> Next;
+ elem ->Next = NULL;
+ Unlinked = elem;
+
+ }
+ break;
+
+ case cmsAT_END:
+ Anterior = Last = NULL;
+ for (pt = lut ->Elements;
+ pt != NULL;
+ pt = pt -> Next) {
+ Anterior = Last;
+ Last = pt;
+ }
+
+ Unlinked = Last; // Next already points to NULL
+
+ // Truncate the chain
+ if (Anterior)
+ Anterior ->Next = NULL;
+ else
+ lut ->Elements = NULL;
+ break;
+ default:;
+ }
+
+ if (mpe)
+ *mpe = Unlinked;
+ else
+ cmsStageFree(Unlinked);
+
+ BlessLUT(lut);
+}
+
+
+// Concatenate two LUT into a new single one
+cmsBool CMSEXPORT cmsPipelineCat(cmsPipeline* l1, const cmsPipeline* l2)
+{
+ cmsStage* mpe;
+
+ // If both LUTS does not have elements, we need to inherit
+ // the number of channels
+ if (l1 ->Elements == NULL && l2 ->Elements == NULL) {
+ l1 ->InputChannels = l2 ->InputChannels;
+ l1 ->OutputChannels = l2 ->OutputChannels;
+ }
+
+ // Cat second
+ for (mpe = l2 ->Elements;
+ mpe != NULL;
+ mpe = mpe ->Next) {
+
+ // We have to dup each element
+ if (!cmsPipelineInsertStage(l1, cmsAT_END, cmsStageDup(mpe)))
+ return FALSE;
+ }
+
+ BlessLUT(l1);
+ return TRUE;
+}
+
+
+cmsBool CMSEXPORT cmsPipelineSetSaveAs8bitsFlag(cmsPipeline* lut, cmsBool On)
+{
+ cmsBool Anterior = lut ->SaveAs8Bits;
+
+ lut ->SaveAs8Bits = On;
+ return Anterior;
+}
+
+
+cmsStage* CMSEXPORT cmsPipelineGetPtrToFirstStage(const cmsPipeline* lut)
+{
+ return lut ->Elements;
+}
+
+cmsStage* CMSEXPORT cmsPipelineGetPtrToLastStage(const cmsPipeline* lut)
+{
+ cmsStage *mpe, *Anterior = NULL;
+
+ for (mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next)
+ Anterior = mpe;
+
+ return Anterior;
+}
+
+cmsUInt32Number CMSEXPORT cmsPipelineStageCount(const cmsPipeline* lut)
+{
+ cmsStage *mpe;
+ cmsUInt32Number n;
+
+ for (n=0, mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next)
+ n++;
+
+ return n;
+}
+
+// This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional
+// duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality.
+void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsPipeline* Lut,
+ _cmsOPTeval16Fn Eval16,
+ void* PrivateData,
+ _cmsFreeUserDataFn FreePrivateDataFn,
+ _cmsDupUserDataFn DupPrivateDataFn)
+{
+
+ Lut ->Eval16Fn = Eval16;
+ Lut ->DupDataFn = DupPrivateDataFn;
+ Lut ->FreeDataFn = FreePrivateDataFn;
+ Lut ->Data = PrivateData;
+}
+
+
+// ----------------------------------------------------------- Reverse interpolation
+// Here's how it goes. The derivative Df(x) of the function f is the linear
+// transformation that best approximates f near the point x. It can be represented
+// by a matrix A whose entries are the partial derivatives of the components of f
+// with respect to all the coordinates. This is know as the Jacobian
+//
+// The best linear approximation to f is given by the matrix equation:
+//
+// y-y0 = A (x-x0)
+//
+// So, if x0 is a good "guess" for the zero of f, then solving for the zero of this
+// linear approximation will give a "better guess" for the zero of f. Thus let y=0,
+// and since y0=f(x0) one can solve the above equation for x. This leads to the
+// Newton's method formula:
+//
+// xn+1 = xn - A-1 f(xn)
+//
+// where xn+1 denotes the (n+1)-st guess, obtained from the n-th guess xn in the
+// fashion described above. Iterating this will give better and better approximations
+// if you have a "good enough" initial guess.
+
+
+#define JACOBIAN_EPSILON 0.001f
+#define INVERSION_MAX_ITERATIONS 30
+
+// Increment with reflexion on boundary
+static
+void IncDelta(cmsFloat32Number *Val)
+{
+ if (*Val < (1.0 - JACOBIAN_EPSILON))
+
+ *Val += JACOBIAN_EPSILON;
+
+ else
+ *Val -= JACOBIAN_EPSILON;
+
+}
+
+
+
+// Euclidean distance between two vectors of n elements each one
+static
+cmsFloat32Number EuclideanDistance(cmsFloat32Number a[], cmsFloat32Number b[], int n)
+{
+ cmsFloat32Number sum = 0;
+ int i;
+
+ for (i=0; i < n; i++) {
+ cmsFloat32Number dif = b[i] - a[i];
+ sum += dif * dif;
+ }
+
+ return sqrtf(sum);
+}
+
+
+// Evaluate a LUT in reverse direction. It only searches on 3->3 LUT. Uses Newton method
+//
+// x1 <- x - [J(x)]^-1 * f(x)
+//
+// lut: The LUT on where to do the search
+// Target: LabK, 3 values of Lab plus destination K which is fixed
+// Result: The obtained CMYK
+// Hint: Location where begin the search
+
+cmsBool CMSEXPORT cmsPipelineEvalReverseFloat(cmsFloat32Number Target[],
+ cmsFloat32Number Result[],
+ cmsFloat32Number Hint[],
+ const cmsPipeline* lut)
+{
+ cmsUInt32Number i, j;
+ cmsFloat64Number error, LastError = 1E20;
+ cmsFloat32Number fx[4], x[4], xd[4], fxd[4];
+ cmsVEC3 tmp, tmp2;
+ cmsMAT3 Jacobian;
+
+ // Only 3->3 and 4->3 are supported
+ if (lut ->InputChannels != 3 && lut ->InputChannels != 4) return FALSE;
+ if (lut ->OutputChannels != 3) return FALSE;
+
+ // Take the hint as starting point if specified
+ if (Hint == NULL) {
+
+ // Begin at any point, we choose 1/3 of CMY axis
+ x[0] = x[1] = x[2] = 0.3f;
+ }
+ else {
+
+ // Only copy 3 channels from hint...
+ for (j=0; j < 3; j++)
+ x[j] = Hint[j];
+ }
+
+ // If Lut is 4-dimensions, then grab target[3], which is fixed
+ if (lut ->InputChannels == 4) {
+ x[3] = Target[3];
+ }
+ else x[3] = 0; // To keep lint happy
+
+
+ // Iterate
+ for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) {
+
+ // Get beginning fx
+ cmsPipelineEvalFloat(x, fx, lut);
+
+ // Compute error
+ error = EuclideanDistance(fx, Target, 3);
+
+ // If not convergent, return last safe value
+ if (error >= LastError)
+ break;
+
+ // Keep latest values
+ LastError = error;
+ for (j=0; j < lut ->InputChannels; j++)
+ Result[j] = x[j];
+
+ // Found an exact match?
+ if (error <= 0)
+ break;
+
+ // Obtain slope (the Jacobian)
+ for (j = 0; j < 3; j++) {
+
+ xd[0] = x[0];
+ xd[1] = x[1];
+ xd[2] = x[2];
+ xd[3] = x[3]; // Keep fixed channel
+
+ IncDelta(&xd[j]);
+
+ cmsPipelineEvalFloat(xd, fxd, lut);
+
+ Jacobian.v[0].n[j] = ((fxd[0] - fx[0]) / JACOBIAN_EPSILON);
+ Jacobian.v[1].n[j] = ((fxd[1] - fx[1]) / JACOBIAN_EPSILON);
+ Jacobian.v[2].n[j] = ((fxd[2] - fx[2]) / JACOBIAN_EPSILON);
+ }
+
+ // Solve system
+ tmp2.n[0] = fx[0] - Target[0];
+ tmp2.n[1] = fx[1] - Target[1];
+ tmp2.n[2] = fx[2] - Target[2];
+
+ if (!_cmsMAT3solve(&tmp, &Jacobian, &tmp2))
+ return FALSE;
+
+ // Move our guess
+ x[0] -= (cmsFloat32Number) tmp.n[0];
+ x[1] -= (cmsFloat32Number) tmp.n[1];
+ x[2] -= (cmsFloat32Number) tmp.n[2];
+
+ // Some clipping....
+ for (j=0; j < 3; j++) {
+ if (x[j] < 0) x[j] = 0;
+ else
+ if (x[j] > 1.0) x[j] = 1.0;
+ }
+ }
+
+ return TRUE;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmd5.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmd5.c
new file mode 100644
index 0000000000..a4758ff662
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmd5.c
@@ -0,0 +1,343 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+#ifdef CMS_USE_BIG_ENDIAN
+
+static
+void byteReverse(cmsUInt8Number * buf, cmsUInt32Number longs)
+{
+ do {
+
+ cmsUInt32Number t = _cmsAdjustEndianess32(*(cmsUInt32Number *) buf);
+ *(cmsUInt32Number *) buf = t;
+ buf += sizeof(cmsUInt32Number);
+
+ } while (--longs);
+
+}
+
+#else
+#define byteReverse(buf, len)
+#endif
+
+
+typedef struct {
+
+ cmsUInt32Number buf[4];
+ cmsUInt32Number bits[2];
+ cmsUInt8Number in[64];
+ cmsContext ContextID;
+
+} _cmsMD5;
+
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
+
+#define STEP(f, w, x, y, z, data, s) \
+ ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
+
+
+static
+void MD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])
+
+{
+ register cmsUInt32Number a, b, c, d;
+
+ a = buf[0];
+ b = buf[1];
+ c = buf[2];
+ d = buf[3];
+
+ STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+ STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+ STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
+ STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+ STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+ STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+ STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
+ STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
+ STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
+ STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+ STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+ STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+ STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
+ STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
+ STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
+ STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+ STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+ STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
+ STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+ STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+ STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+ STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
+ STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+ STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+ STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+ STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+ STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+ STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+ STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+ STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+ STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+ STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+ STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+ STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
+ STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+ STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+ STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+ STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+ STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+ STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+ STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+ STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+ STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+ STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
+ STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+ STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+ STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+ STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+ STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
+ STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
+ STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+ STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+ STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+ STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+ STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+ STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+ STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+ STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+ STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
+ STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+ STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+ STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+ STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+ STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+ buf[0] += a;
+ buf[1] += b;
+ buf[2] += c;
+ buf[3] += d;
+}
+
+
+// Create a MD5 object
+static
+cmsHANDLE MD5alloc(cmsContext ContextID)
+{
+ _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
+ if (ctx == NULL) return NULL;
+
+ ctx ->ContextID = ContextID;
+
+ ctx->buf[0] = 0x67452301;
+ ctx->buf[1] = 0xefcdab89;
+ ctx->buf[2] = 0x98badcfe;
+ ctx->buf[3] = 0x10325476;
+
+ ctx->bits[0] = 0;
+ ctx->bits[1] = 0;
+
+ return (cmsHANDLE) ctx;
+}
+
+
+static
+void MD5add(cmsHANDLE Handle, cmsUInt8Number* buf, cmsUInt32Number len)
+{
+ _cmsMD5* ctx = (_cmsMD5*) Handle;
+ cmsUInt32Number t;
+
+ t = ctx->bits[0];
+ if ((ctx->bits[0] = t + (len << 3)) < t)
+ ctx->bits[1]++;
+
+ ctx->bits[1] += len >> 29;
+
+ t = (t >> 3) & 0x3f;
+
+ if (t) {
+
+ cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
+
+ t = 64 - t;
+ if (len < t) {
+ memmove(p, buf, len);
+ return;
+ }
+
+ memmove(p, buf, t);
+ byteReverse(ctx->in, 16);
+
+ MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
+ buf += t;
+ len -= t;
+ }
+
+ while (len >= 64) {
+ memmove(ctx->in, buf, 64);
+ byteReverse(ctx->in, 16);
+ MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
+ buf += 64;
+ len -= 64;
+ }
+
+ memmove(ctx->in, buf, len);
+}
+
+// Destroy the object and return the checksum
+static
+void MD5finish(cmsProfileID* ProfileID, cmsHANDLE Handle)
+{
+ _cmsMD5* ctx = (_cmsMD5*) Handle;
+ cmsUInt32Number count;
+ cmsUInt8Number *p;
+
+ count = (ctx->bits[0] >> 3) & 0x3F;
+
+ p = ctx->in + count;
+ *p++ = 0x80;
+
+ count = 64 - 1 - count;
+
+ if (count < 8) {
+
+ memset(p, 0, count);
+ byteReverse(ctx->in, 16);
+ MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
+
+ memset(ctx->in, 0, 56);
+ } else {
+ memset(p, 0, count - 8);
+ }
+ byteReverse(ctx->in, 14);
+
+ ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
+ ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
+
+ MD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
+
+ byteReverse((cmsUInt8Number *) ctx->buf, 4);
+ memmove(ProfileID ->ID8, ctx->buf, 16);
+
+ _cmsFree(ctx ->ContextID, ctx);
+}
+
+
+
+// Assuming io points to an ICC profile, compute and store MD5 checksum
+// In the header, rendering intentent, attributes and ID should be set to zero
+// before computing MD5 checksum (per 6.1.13 in ICC spec)
+
+cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)
+{
+ cmsContext ContextID;
+ cmsUInt32Number BytesNeeded;
+ cmsUInt8Number* Mem = NULL;
+ cmsHANDLE MD5 = NULL;
+ _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
+ _cmsICCPROFILE Keep;
+
+ _cmsAssert(hProfile != NULL);
+
+ ContextID = cmsGetProfileContextID(hProfile);
+
+ // Save a copy of the profile header
+ memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
+
+ // Set RI, attributes and ID
+ memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
+ Icc ->RenderingIntent = 0;
+ memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
+
+ // Compute needed storage
+ if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;
+
+ // Allocate memory
+ Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
+ if (Mem == NULL) goto Error;
+
+ // Save to temporary storage
+ if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;
+
+ // Create MD5 object
+ MD5 = MD5alloc(ContextID);
+ if (MD5 == NULL) goto Error;
+
+ // Add all bytes
+ MD5add(MD5, Mem, BytesNeeded);
+
+ // Temp storage is no longer needed
+ _cmsFree(ContextID, Mem);
+
+ // Restore header
+ memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
+
+ // And store the ID
+ MD5finish(&Icc ->ProfileID, MD5);
+ return TRUE;
+
+Error:
+
+ // Free resources as something went wrong
+ // "MD5" cannot be other than NULL here, so no need to free it
+ if (Mem != NULL) _cmsFree(ContextID, Mem);
+ memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
+ return FALSE;
+}
+
+cmsBool CMSEXPORT cmsMD5computeIDExt(const void* buf, unsigned long size, unsigned char ProfileID[16])
+{
+ cmsHANDLE MD5;
+ cmsUInt8Number* Mem;
+
+ if (buf == NULL)
+ return FALSE;
+ MD5 = NULL;
+ Mem = (cmsUInt8Number*)_cmsMalloc(NULL,size);
+ memmove(Mem,buf,size);
+ // Create MD5 object
+ MD5 = MD5alloc(NULL);
+ if (MD5 == NULL) goto Error;
+
+ // Add all bytes
+ MD5add(MD5, Mem, size);
+
+ // Temp storage is no longer needed
+ _cmsFree(NULL, Mem);
+
+ // And store the ID
+ MD5finish((cmsProfileID*)ProfileID, MD5);
+ return TRUE;
+Error:
+ if (MD5 != NULL) _cmsFree(NULL, MD5);
+ return FALSE;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmtrx.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmtrx.c
new file mode 100644
index 0000000000..fb7b91caf1
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsmtrx.c
@@ -0,0 +1,175 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+
+#define DSWAP(x, y) {cmsFloat64Number tmp = (x); (x)=(y); (y)=tmp;}
+
+
+// Initiate a vector
+void CMSEXPORT _cmsVEC3init(cmsVEC3* r, cmsFloat64Number x, cmsFloat64Number y, cmsFloat64Number z)
+{
+ r -> n[VX] = x;
+ r -> n[VY] = y;
+ r -> n[VZ] = z;
+}
+
+// Vector substraction
+void CMSEXPORT _cmsVEC3minus(cmsVEC3* r, const cmsVEC3* a, const cmsVEC3* b)
+{
+ r -> n[VX] = a -> n[VX] - b -> n[VX];
+ r -> n[VY] = a -> n[VY] - b -> n[VY];
+ r -> n[VZ] = a -> n[VZ] - b -> n[VZ];
+}
+
+// Vector cross product
+void CMSEXPORT _cmsVEC3cross(cmsVEC3* r, const cmsVEC3* u, const cmsVEC3* v)
+{
+ r ->n[VX] = u->n[VY] * v->n[VZ] - v->n[VY] * u->n[VZ];
+ r ->n[VY] = u->n[VZ] * v->n[VX] - v->n[VZ] * u->n[VX];
+ r ->n[VZ] = u->n[VX] * v->n[VY] - v->n[VX] * u->n[VY];
+}
+
+// Vector dot product
+cmsFloat64Number CMSEXPORT _cmsVEC3dot(const cmsVEC3* u, const cmsVEC3* v)
+{
+ return u->n[VX] * v->n[VX] + u->n[VY] * v->n[VY] + u->n[VZ] * v->n[VZ];
+}
+
+// Euclidean length
+cmsFloat64Number CMSEXPORT _cmsVEC3length(const cmsVEC3* a)
+{
+ return sqrt(a ->n[VX] * a ->n[VX] +
+ a ->n[VY] * a ->n[VY] +
+ a ->n[VZ] * a ->n[VZ]);
+}
+
+// Euclidean distance
+cmsFloat64Number CMSEXPORT _cmsVEC3distance(const cmsVEC3* a, const cmsVEC3* b)
+{
+ cmsFloat64Number d1 = a ->n[VX] - b ->n[VX];
+ cmsFloat64Number d2 = a ->n[VY] - b ->n[VY];
+ cmsFloat64Number d3 = a ->n[VZ] - b ->n[VZ];
+
+ return sqrt(d1*d1 + d2*d2 + d3*d3);
+}
+
+
+
+// 3x3 Identity
+void CMSEXPORT _cmsMAT3identity(cmsMAT3* a)
+{
+ _cmsVEC3init(&a-> v[0], 1.0, 0.0, 0.0);
+ _cmsVEC3init(&a-> v[1], 0.0, 1.0, 0.0);
+ _cmsVEC3init(&a-> v[2], 0.0, 0.0, 1.0);
+}
+
+static
+cmsBool CloseEnough(cmsFloat64Number a, cmsFloat64Number b)
+{
+ return fabs(b - a) < (1.0 / 65535.0);
+}
+
+
+cmsBool CMSEXPORT _cmsMAT3isIdentity(const cmsMAT3* a)
+{
+ cmsMAT3 Identity;
+ int i, j;
+
+ _cmsMAT3identity(&Identity);
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ if (!CloseEnough(a ->v[i].n[j], Identity.v[i].n[j])) return FALSE;
+
+ return TRUE;
+}
+
+
+// Multiply two matrices
+void CMSEXPORT _cmsMAT3per(cmsMAT3* r, const cmsMAT3* a, const cmsMAT3* b)
+{
+#define ROWCOL(i, j) \
+ a->v[i].n[0]*b->v[0].n[j] + a->v[i].n[1]*b->v[1].n[j] + a->v[i].n[2]*b->v[2].n[j]
+
+ _cmsVEC3init(&r-> v[0], ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2));
+ _cmsVEC3init(&r-> v[1], ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2));
+ _cmsVEC3init(&r-> v[2], ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2));
+
+#undef ROWCOL //(i, j)
+}
+
+
+
+// Inverse of a matrix b = a^(-1)
+cmsBool CMSEXPORT _cmsMAT3inverse(const cmsMAT3* a, cmsMAT3* b)
+{
+ cmsFloat64Number det, c0, c1, c2;
+
+ c0 = a -> v[1].n[1]*a -> v[2].n[2] - a -> v[1].n[2]*a -> v[2].n[1];
+ c1 = -a -> v[1].n[0]*a -> v[2].n[2] + a -> v[1].n[2]*a -> v[2].n[0];
+ c2 = a -> v[1].n[0]*a -> v[2].n[1] - a -> v[1].n[1]*a -> v[2].n[0];
+
+ det = a -> v[0].n[0]*c0 + a -> v[0].n[1]*c1 + a -> v[0].n[2]*c2;
+
+ if (fabs(det) < MATRIX_DET_TOLERANCE) return FALSE; // singular matrix; can't invert
+
+ b -> v[0].n[0] = c0/det;
+ b -> v[0].n[1] = (a -> v[0].n[2]*a -> v[2].n[1] - a -> v[0].n[1]*a -> v[2].n[2])/det;
+ b -> v[0].n[2] = (a -> v[0].n[1]*a -> v[1].n[2] - a -> v[0].n[2]*a -> v[1].n[1])/det;
+ b -> v[1].n[0] = c1/det;
+ b -> v[1].n[1] = (a -> v[0].n[0]*a -> v[2].n[2] - a -> v[0].n[2]*a -> v[2].n[0])/det;
+ b -> v[1].n[2] = (a -> v[0].n[2]*a -> v[1].n[0] - a -> v[0].n[0]*a -> v[1].n[2])/det;
+ b -> v[2].n[0] = c2/det;
+ b -> v[2].n[1] = (a -> v[0].n[1]*a -> v[2].n[0] - a -> v[0].n[0]*a -> v[2].n[1])/det;
+ b -> v[2].n[2] = (a -> v[0].n[0]*a -> v[1].n[1] - a -> v[0].n[1]*a -> v[1].n[0])/det;
+
+ return TRUE;
+}
+
+
+// Solve a system in the form Ax = b
+cmsBool CMSEXPORT _cmsMAT3solve(cmsVEC3* x, cmsMAT3* a, cmsVEC3* b)
+{
+ cmsMAT3 m, a_1;
+
+ memmove(&m, a, sizeof(cmsMAT3));
+
+ if (!_cmsMAT3inverse(&m, &a_1)) return FALSE; // Singular matrix
+
+ _cmsMAT3eval(x, &a_1, b);
+ return TRUE;
+}
+
+// Evaluate a vector across a matrix
+void CMSEXPORT _cmsMAT3eval(cmsVEC3* r, const cmsMAT3* a, const cmsVEC3* v)
+{
+ r->n[VX] = a->v[0].n[VX]*v->n[VX] + a->v[0].n[VY]*v->n[VY] + a->v[0].n[VZ]*v->n[VZ];
+ r->n[VY] = a->v[1].n[VX]*v->n[VX] + a->v[1].n[VY]*v->n[VY] + a->v[1].n[VZ]*v->n[VZ];
+ r->n[VZ] = a->v[2].n[VX]*v->n[VX] + a->v[2].n[VY]*v->n[VY] + a->v[2].n[VZ]*v->n[VZ];
+}
+
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsnamed.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsnamed.c
new file mode 100644
index 0000000000..acfd1c8cf9
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsnamed.c
@@ -0,0 +1,929 @@
+//---------------------------------------------------------------------------------
+//
+// 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"
+
+// Multilocalized unicode objects. That is an attempt to encapsulate i18n.
+
+
+// Allocates an empty multi localizad unicode object
+cmsMLU* CMSEXPORT cmsMLUalloc(cmsContext ContextID, cmsUInt32Number nItems)
+{
+ cmsMLU* mlu;
+
+ // nItems should be positive if given
+ if (nItems <= 0) nItems = 2;
+
+ // Create the container
+ mlu = (cmsMLU*) _cmsMallocZero(ContextID, sizeof(cmsMLU));
+ if (mlu == NULL) return NULL;
+
+ mlu ->ContextID = ContextID;
+
+ // Create entry array
+ mlu ->Entries = (_cmsMLUentry*) _cmsCalloc(ContextID, nItems, sizeof(_cmsMLUentry));
+ if (mlu ->Entries == NULL) {
+ _cmsFree(ContextID, mlu);
+ return NULL;
+ }
+
+ // Ok, keep indexes up to date
+ mlu ->AllocatedEntries = nItems;
+ mlu ->UsedEntries = 0;
+
+ return mlu;
+}
+
+
+// Grows a mempool table for a MLU. Each time this function is called, mempool size is multiplied times two.
+static
+cmsBool GrowMLUpool(cmsMLU* mlu)
+{
+ cmsUInt32Number size;
+ void *NewPtr;
+
+ // Sanity check
+ if (mlu == NULL) return FALSE;
+
+ if (mlu ->PoolSize == 0)
+ size = 256;
+ else
+ size = mlu ->PoolSize * 2;
+
+ // Check for overflow
+ if (size < mlu ->PoolSize) return FALSE;
+
+ // Reallocate the pool
+ NewPtr = _cmsRealloc(mlu ->ContextID, mlu ->MemPool, size);
+ if (NewPtr == NULL) return FALSE;
+
+
+ mlu ->MemPool = NewPtr;
+ mlu ->PoolSize = size;
+
+ return TRUE;
+}
+
+
+// Grows a entry table for a MLU. Each time this function is called, table size is multiplied times two.
+static
+cmsBool GrowMLUtable(cmsMLU* mlu)
+{
+ int AllocatedEntries;
+ _cmsMLUentry *NewPtr;
+
+ // Sanity check
+ if (mlu == NULL) return FALSE;
+
+ AllocatedEntries = mlu ->AllocatedEntries * 2;
+
+ // Check for overflow
+ if (AllocatedEntries / 2 != mlu ->AllocatedEntries) return FALSE;
+
+ // Reallocate the memory
+ NewPtr = (_cmsMLUentry*)_cmsRealloc(mlu ->ContextID, mlu ->Entries, AllocatedEntries*sizeof(_cmsMLUentry));
+ if (NewPtr == NULL) return FALSE;
+
+ mlu ->Entries = NewPtr;
+ mlu ->AllocatedEntries = AllocatedEntries;
+
+ return TRUE;
+}
+
+
+// Search for a specific entry in the structure. Language and Country are used.
+static
+int SearchMLUEntry(cmsMLU* mlu, cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
+{
+ int i;
+
+ // Sanity check
+ if (mlu == NULL) return -1;
+
+ // Iterate whole table
+ for (i=0; i < mlu ->UsedEntries; i++) {
+
+ if (mlu ->Entries[i].Country == CountryCode &&
+ mlu ->Entries[i].Language == LanguageCode) return i;
+ }
+
+ // Not found
+ return -1;
+}
+
+// Add a block of characters to the intended MLU. Language and country are specified.
+// Only one entry for Language/country pair is allowed.
+static
+cmsBool AddMLUBlock(cmsMLU* mlu, cmsUInt32Number size, const wchar_t *Block,
+ cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode)
+{
+ cmsUInt32Number Offset;
+ cmsUInt8Number* Ptr;
+
+ // Sanity check
+ if (mlu == NULL) return FALSE;
+
+ // Is there any room available?
+ if (mlu ->UsedEntries >= mlu ->AllocatedEntries) {
+ if (!GrowMLUtable(mlu)) return FALSE;
+ }
+
+ // Only one ASCII string
+ if (SearchMLUEntry(mlu, LanguageCode, CountryCode) >= 0) return FALSE; // Only one is allowed!
+
+ // Check for size
+ while ((mlu ->PoolSize - mlu ->PoolUsed) < size) {
+
+ if (!GrowMLUpool(mlu)) return FALSE;
+ }
+
+ Offset = mlu ->PoolUsed;
+
+ Ptr = (cmsUInt8Number*) mlu ->MemPool;
+ if (Ptr == NULL) return FALSE;
+
+ // Set the entry
+ memmove(Ptr + Offset, Block, size);
+ mlu ->PoolUsed += size;
+
+ mlu ->Entries[mlu ->UsedEntries].StrW = Offset;
+ mlu ->Entries[mlu ->UsedEntries].Len = size;
+ mlu ->Entries[mlu ->UsedEntries].Country = CountryCode;
+ mlu ->Entries[mlu ->UsedEntries].Language = LanguageCode;
+ mlu ->UsedEntries++;
+
+ return TRUE;
+}
+
+
+// Add an ASCII entry.
+cmsBool CMSEXPORT cmsMLUsetASCII(cmsMLU* mlu, const char LanguageCode[3], const char CountryCode[3], const char* ASCIIString)
+{
+ cmsUInt32Number i, len = (cmsUInt32Number) strlen(ASCIIString)+1;
+ wchar_t* WStr;
+ cmsBool rc;
+ cmsUInt16Number Lang = _cmsAdjustEndianess16(*(cmsUInt16Number*) LanguageCode);
+ cmsUInt16Number Cntry = _cmsAdjustEndianess16(*(cmsUInt16Number*) CountryCode);
+
+ if (mlu == NULL) return FALSE;
+
+ WStr = (wchar_t*) _cmsCalloc(mlu ->ContextID, len, sizeof(wchar_t));
+ if (WStr == NULL) return FALSE;
+
+ for (i=0; i < len; i++)
+ WStr[i] = (wchar_t) ASCIIString[i];
+
+ rc = AddMLUBlock(mlu, len * sizeof(wchar_t), WStr, Lang, Cntry);
+
+ _cmsFree(mlu ->ContextID, WStr);
+ return rc;
+
+}
+
+// We don't need any wcs support library
+static
+cmsUInt32Number mywcslen(const wchar_t *s)
+{
+ const wchar_t *p;
+
+ p = s;
+ while (*p)
+ p++;
+
+ return (cmsUInt32Number)(p - s);
+}
+
+
+// Add a wide entry
+cmsBool CMSEXPORT cmsMLUsetWide(cmsMLU* mlu, const char Language[3], const char Country[3], const wchar_t* WideString)
+{
+ cmsUInt16Number Lang = _cmsAdjustEndianess16(*(cmsUInt16Number*) Language);
+ cmsUInt16Number Cntry = _cmsAdjustEndianess16(*(cmsUInt16Number*) Country);
+ cmsUInt32Number len;
+
+ if (mlu == NULL) return FALSE;
+ if (WideString == NULL) return FALSE;
+
+ len = (cmsUInt32Number) (mywcslen(WideString) + 1) * sizeof(wchar_t);
+ return AddMLUBlock(mlu, len, WideString, Lang, Cntry);
+}
+
+// Duplicating a MLU is as easy as copying all members
+cmsMLU* CMSEXPORT cmsMLUdup(const cmsMLU* mlu)
+{
+ cmsMLU* NewMlu = NULL;
+
+ // Duplicating a NULL obtains a NULL
+ if (mlu == NULL) return NULL;
+
+ NewMlu = cmsMLUalloc(mlu ->ContextID, mlu ->UsedEntries);
+ if (NewMlu == NULL) return NULL;
+
+ // Should never happen
+ if (NewMlu ->AllocatedEntries < mlu ->UsedEntries)
+ goto Error;
+
+ // Sanitize...
+ if (NewMlu ->Entries == NULL || mlu ->Entries == NULL) goto Error;
+
+ memmove(NewMlu ->Entries, mlu ->Entries, mlu ->UsedEntries * sizeof(_cmsMLUentry));
+ NewMlu ->UsedEntries = mlu ->UsedEntries;
+
+ // The MLU may be empty
+ if (mlu ->PoolUsed == 0) {
+ NewMlu ->MemPool = NULL;
+ }
+ else {
+ // It is not empty
+ NewMlu ->MemPool = _cmsMalloc(mlu ->ContextID, mlu ->PoolUsed);
+ if (NewMlu ->MemPool == NULL) goto Error;
+ }
+
+ NewMlu ->PoolSize = mlu ->PoolUsed;
+
+ if (NewMlu ->MemPool == NULL || mlu ->MemPool == NULL) goto Error;
+
+ memmove(NewMlu ->MemPool, mlu->MemPool, mlu ->PoolUsed);
+ NewMlu ->PoolUsed = mlu ->PoolUsed;
+
+ return NewMlu;
+
+Error:
+
+ if (NewMlu != NULL) cmsMLUfree(NewMlu);
+ return NULL;
+}
+
+// Free any used memory
+void CMSEXPORT cmsMLUfree(cmsMLU* mlu)
+{
+ if (mlu) {
+
+ if (mlu -> Entries) _cmsFree(mlu ->ContextID, mlu->Entries);
+ if (mlu -> MemPool) _cmsFree(mlu ->ContextID, mlu->MemPool);
+
+ _cmsFree(mlu ->ContextID, mlu);
+ }
+}
+
+
+// The algorithm first searches for an exact match of country and language, if not found it uses
+// the Language. If none is found, first entry is used instead.
+static
+const wchar_t* _cmsMLUgetWide(const cmsMLU* mlu,
+ cmsUInt32Number *len,
+ cmsUInt16Number LanguageCode, cmsUInt16Number CountryCode,
+ cmsUInt16Number* UsedLanguageCode, cmsUInt16Number* UsedCountryCode)
+{
+ int i;
+ int Best = -1;
+ _cmsMLUentry* v;
+
+ if (mlu == NULL) return NULL;
+
+ if (mlu -> AllocatedEntries <= 0) return NULL;
+
+ for (i=0; i < mlu ->UsedEntries; i++) {
+
+ v = mlu ->Entries + i;
+
+ if (v -> Language == LanguageCode) {
+
+ if (Best == -1) Best = i;
+
+ if (v -> Country == CountryCode) {
+
+ if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
+ if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
+
+ if (len != NULL) *len = v ->Len;
+
+ return (wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v -> StrW); // Found exact match
+ }
+ }
+ }
+
+ // No string found. Return First one
+ if (Best == -1)
+ Best = 0;
+
+ v = mlu ->Entries + Best;
+
+ if (UsedLanguageCode != NULL) *UsedLanguageCode = v ->Language;
+ if (UsedCountryCode != NULL) *UsedCountryCode = v ->Country;
+
+ if (len != NULL) *len = v ->Len;
+
+ return(wchar_t*) ((cmsUInt8Number*) mlu ->MemPool + v ->StrW);
+}
+
+
+// Obtain an ASCII representation of the wide string. Setting buffer to NULL returns the len
+cmsUInt32Number CMSEXPORT cmsMLUgetASCII(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ char* Buffer, cmsUInt32Number BufferSize)
+{
+ const wchar_t *Wide;
+ cmsUInt32Number StrLen = 0;
+ cmsUInt32Number ASCIIlen, i;
+
+ cmsUInt16Number Lang = _cmsAdjustEndianess16(*(cmsUInt16Number*) LanguageCode);
+ cmsUInt16Number Cntry = _cmsAdjustEndianess16(*(cmsUInt16Number*) CountryCode);
+
+ // Sanitize
+ if (mlu == NULL) return 0;
+
+ // Get WideChar
+ Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
+ if (Wide == NULL) return 0;
+
+ ASCIIlen = StrLen / sizeof(wchar_t);
+
+ // Maybe we want only to know the len?
+ if (Buffer == NULL) return ASCIIlen + 1; // Note the zero at the end
+
+ // No buffer size means no data
+ if (BufferSize <= 0) return 0;
+
+ // Some clipping may be required
+ if (BufferSize < ASCIIlen + 1)
+ ASCIIlen = BufferSize - 1;
+
+ // Precess each character
+ for (i=0; i < ASCIIlen; i++) {
+
+ if (Wide[i] == 0)
+ Buffer[i] = 0;
+ else
+ Buffer[i] = (char) Wide[i];
+ }
+
+ // We put a termination "\0"
+ Buffer[ASCIIlen] = 0;
+ return ASCIIlen + 1;
+}
+
+// Obtain a wide representation of the MLU, on depending on current locale settings
+cmsUInt32Number CMSEXPORT cmsMLUgetWide(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ wchar_t* Buffer, cmsUInt32Number BufferSize)
+{
+ const wchar_t *Wide;
+ cmsUInt32Number StrLen = 0;
+
+ cmsUInt16Number Lang = _cmsAdjustEndianess16(*(cmsUInt16Number*) LanguageCode);
+ cmsUInt16Number Cntry = _cmsAdjustEndianess16(*(cmsUInt16Number*) CountryCode);
+
+ // Sanitize
+ if (mlu == NULL) return 0;
+
+ Wide = _cmsMLUgetWide(mlu, &StrLen, Lang, Cntry, NULL, NULL);
+ if (Wide == NULL) return 0;
+
+ // Maybe we want only to know the len?
+ if (Buffer == NULL) return StrLen + sizeof(wchar_t);
+
+ // No buffer size means no data
+ if (BufferSize <= 0) return 0;
+
+ // Some clipping may be required
+ if (BufferSize < StrLen + sizeof(wchar_t))
+ StrLen = BufferSize - + sizeof(wchar_t);
+
+ memmove(Buffer, Wide, StrLen);
+ Buffer[StrLen / sizeof(wchar_t)] = 0;
+
+ return StrLen + sizeof(wchar_t);
+}
+
+
+// Get also the language and country
+CMSAPI cmsBool CMSEXPORT cmsMLUgetTranslation(const cmsMLU* mlu,
+ const char LanguageCode[3], const char CountryCode[3],
+ char ObtainedLanguage[3], char ObtainedCountry[3])
+{
+ const wchar_t *Wide;
+
+ cmsUInt16Number Lang = _cmsAdjustEndianess16(*(cmsUInt16Number*) LanguageCode);
+ cmsUInt16Number Cntry = _cmsAdjustEndianess16(*(cmsUInt16Number*) CountryCode);
+ cmsUInt16Number ObtLang, ObtCode;
+
+ // Sanitize
+ if (mlu == NULL) return FALSE;
+
+ Wide = _cmsMLUgetWide(mlu, NULL, Lang, Cntry, &ObtLang, &ObtCode);
+ if (Wide == NULL) return FALSE;
+
+ // Get used language and code
+ *(cmsUInt16Number *)ObtainedLanguage = _cmsAdjustEndianess16(ObtLang);
+ *(cmsUInt16Number *)ObtainedCountry = _cmsAdjustEndianess16(ObtCode);
+
+ ObtainedLanguage[2] = ObtainedCountry[2] = 0;
+ return TRUE;
+}
+
+
+
+// Get the number of translations in the MLU object
+cmsUInt32Number CMSEXPORT cmsMLUtranslationsCount(const cmsMLU* mlu)
+{
+ if (mlu == NULL) return 0;
+ return mlu->UsedEntries;
+}
+
+// Get the language and country codes for a specific MLU index
+cmsBool CMSEXPORT cmsMLUtranslationsCodes(const cmsMLU* mlu,
+ cmsUInt32Number idx,
+ char LanguageCode[3],
+ char CountryCode[3])
+{
+ _cmsMLUentry *entry;
+
+ if (mlu == NULL) return FALSE;
+
+ if (idx >= (cmsUInt32Number) mlu->UsedEntries) return FALSE;
+
+ entry = &mlu->Entries[idx];
+
+ *(cmsUInt16Number *)LanguageCode = _cmsAdjustEndianess16(entry->Language);
+ *(cmsUInt16Number *)CountryCode = _cmsAdjustEndianess16(entry->Country);
+
+ return TRUE;
+}
+
+
+// Named color lists --------------------------------------------------------------------------------------------
+
+// Grow the list to keep at least NumElements
+static
+cmsBool GrowNamedColorList(cmsNAMEDCOLORLIST* v)
+{
+ cmsUInt32Number size;
+ _cmsNAMEDCOLOR * NewPtr;
+
+ if (v == NULL) return FALSE;
+
+ if (v ->Allocated == 0)
+ size = 64; // Initial guess
+ else
+ size = v ->Allocated * 2;
+
+ // Keep a maximum color lists can grow, 100K entries seems reasonable
+ if (size > 1024*100) return FALSE;
+
+ NewPtr = (_cmsNAMEDCOLOR*) _cmsRealloc(v ->ContextID, v ->List, size * sizeof(_cmsNAMEDCOLOR));
+ if (NewPtr == NULL)
+ return FALSE;
+
+ v ->List = NewPtr;
+ v ->Allocated = size;
+ return TRUE;
+}
+
+// Allocate a list for n elements
+cmsNAMEDCOLORLIST* CMSEXPORT cmsAllocNamedColorList(cmsContext ContextID, cmsUInt32Number n, cmsUInt32Number ColorantCount, const char* Prefix, const char* Suffix)
+{
+ cmsNAMEDCOLORLIST* v = (cmsNAMEDCOLORLIST*) _cmsMallocZero(ContextID, sizeof(cmsNAMEDCOLORLIST));
+
+ if (v == NULL) return NULL;
+
+ v ->List = NULL;
+ v ->nColors = 0;
+ v ->ContextID = ContextID;
+
+ while (v -> Allocated < n)
+ GrowNamedColorList(v);
+
+ strncpy(v ->Prefix, Prefix, sizeof(v ->Prefix)-1);
+ strncpy(v ->Suffix, Suffix, sizeof(v ->Suffix)-1);
+ v->Prefix[32] = v->Suffix[32] = 0;
+
+ v -> ColorantCount = ColorantCount;
+
+ return v;
+}
+
+// Free a list
+void CMSEXPORT cmsFreeNamedColorList(cmsNAMEDCOLORLIST* v)
+{
+ if (v == NULL) return;
+ if (v ->List) _cmsFree(v ->ContextID, v ->List);
+ _cmsFree(v ->ContextID, v);
+}
+
+cmsNAMEDCOLORLIST* CMSEXPORT cmsDupNamedColorList(const cmsNAMEDCOLORLIST* v)
+{
+ cmsNAMEDCOLORLIST* NewNC;
+
+ if (v == NULL) return NULL;
+
+ NewNC= cmsAllocNamedColorList(v ->ContextID, v -> nColors, v ->ColorantCount, v ->Prefix, v ->Suffix);
+ if (NewNC == NULL) return NULL;
+
+ // For really large tables we need this
+ while (NewNC ->Allocated < v ->Allocated)
+ GrowNamedColorList(NewNC);
+
+ memmove(NewNC ->Prefix, v ->Prefix, sizeof(v ->Prefix));
+ memmove(NewNC ->Suffix, v ->Suffix, sizeof(v ->Suffix));
+ NewNC ->ColorantCount = v ->ColorantCount;
+ memmove(NewNC->List, v ->List, v->nColors * sizeof(_cmsNAMEDCOLOR));
+ NewNC ->nColors = v ->nColors;
+ return NewNC;
+}
+
+
+// Append a color to a list. List pointer may change if reallocated
+cmsBool CMSEXPORT cmsAppendNamedColor(cmsNAMEDCOLORLIST* NamedColorList,
+ const char* Name,
+ cmsUInt16Number PCS[3], cmsUInt16Number Colorant[cmsMAXCHANNELS])
+{
+ cmsUInt32Number i;
+
+ if (NamedColorList == NULL) return FALSE;
+
+ if (NamedColorList ->nColors + 1 > NamedColorList ->Allocated) {
+ if (!GrowNamedColorList(NamedColorList)) return FALSE;
+ }
+
+ for (i=0; i < NamedColorList ->ColorantCount; i++)
+ NamedColorList ->List[NamedColorList ->nColors].DeviceColorant[i] = Colorant == NULL? 0 : Colorant[i];
+
+ for (i=0; i < 3; i++)
+ NamedColorList ->List[NamedColorList ->nColors].PCS[i] = PCS == NULL ? 0 : PCS[i];
+
+ if (Name != NULL) {
+
+ strncpy(NamedColorList ->List[NamedColorList ->nColors].Name, Name, cmsMAX_PATH-1);
+ NamedColorList ->List[NamedColorList ->nColors].Name[cmsMAX_PATH-1] = 0;
+
+ }
+ else
+ NamedColorList ->List[NamedColorList ->nColors].Name[0] = 0;
+
+
+ NamedColorList ->nColors++;
+ return TRUE;
+}
+
+// Returns number of elements
+cmsUInt32Number CMSEXPORT cmsNamedColorCount(const cmsNAMEDCOLORLIST* NamedColorList)
+{
+ if (NamedColorList == NULL) return 0;
+ return NamedColorList ->nColors;
+}
+
+// Info aboout a given color
+cmsBool CMSEXPORT cmsNamedColorInfo(const cmsNAMEDCOLORLIST* NamedColorList, cmsUInt32Number nColor,
+ char* Name,
+ char* Prefix,
+ char* Suffix,
+ cmsUInt16Number* PCS,
+ cmsUInt16Number* Colorant)
+{
+ if (NamedColorList == NULL) return FALSE;
+
+ if (nColor >= cmsNamedColorCount(NamedColorList)) return FALSE;
+
+ if (Name) strcpy(Name, NamedColorList->List[nColor].Name);
+ if (Prefix) strcpy(Prefix, NamedColorList->Prefix);
+ if (Suffix) strcpy(Suffix, NamedColorList->Suffix);
+ if (PCS)
+ memmove(PCS, NamedColorList ->List[nColor].PCS, 3*sizeof(cmsUInt16Number));
+
+ if (Colorant)
+ memmove(Colorant, NamedColorList ->List[nColor].DeviceColorant,
+ sizeof(cmsUInt16Number) * NamedColorList ->ColorantCount);
+
+
+ return TRUE;
+}
+
+// Search for a given color name (no prefix or suffix)
+cmsInt32Number CMSEXPORT cmsNamedColorIndex(const cmsNAMEDCOLORLIST* NamedColorList, const char* Name)
+{
+ int i, n;
+
+ if (NamedColorList == NULL) return -1;
+ n = cmsNamedColorCount(NamedColorList);
+ for (i=0; i < n; i++) {
+ if (cmsstrcasecmp(Name, NamedColorList->List[i].Name) == 0)
+ return i;
+ }
+
+ return -1;
+}
+
+// MPE support -----------------------------------------------------------------------------------------------------------------
+
+static
+void FreeNamedColorList(cmsStage* mpe)
+{
+ cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
+ cmsFreeNamedColorList(List);
+}
+
+static
+void* DupNamedColorList(cmsStage* mpe)
+{
+ cmsNAMEDCOLORLIST* List = (cmsNAMEDCOLORLIST*) mpe ->Data;
+ return cmsDupNamedColorList(List);
+}
+
+static
+void EvalNamedColorPCS(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
+{
+ cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
+ cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
+
+ if (index >= NamedColorList-> nColors) {
+ cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range; ignored", index);
+ }
+ else {
+
+ // Named color always uses Lab
+ Out[0] = (cmsFloat32Number) (NamedColorList->List[index].PCS[0] / 65535.0);
+ Out[1] = (cmsFloat32Number) (NamedColorList->List[index].PCS[1] / 65535.0);
+ Out[2] = (cmsFloat32Number) (NamedColorList->List[index].PCS[2] / 65535.0);
+ }
+}
+
+static
+void EvalNamedColor(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
+{
+ cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) mpe ->Data;
+ cmsUInt16Number index = (cmsUInt16Number) _cmsQuickSaturateWord(In[0] * 65535.0);
+ cmsUInt32Number j;
+
+ if (index >= NamedColorList-> nColors) {
+ cmsSignalError(NamedColorList ->ContextID, cmsERROR_RANGE, "Color %d out of range; ignored", index);
+ }
+ else {
+ for (j=0; j < NamedColorList ->ColorantCount; j++)
+ Out[j] = (cmsFloat32Number) (NamedColorList->List[index].DeviceColorant[j] / 65535.0);
+ }
+}
+
+
+// Named color lookup element
+cmsStage* _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS)
+{
+ return _cmsStageAllocPlaceholder(NamedColorList ->ContextID,
+ cmsSigNamedColorElemType,
+ 1, UsePCS ? 3 : NamedColorList ->ColorantCount,
+ UsePCS ? EvalNamedColorPCS : EvalNamedColor,
+ DupNamedColorList,
+ FreeNamedColorList,
+ cmsDupNamedColorList(NamedColorList));
+
+}
+
+
+// Retrieve the named color list from a transform. Should be first element in the LUT
+cmsNAMEDCOLORLIST* CMSEXPORT cmsGetNamedColorList(cmsHTRANSFORM xform)
+{
+ _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
+ cmsStage* mpe = v ->Lut->Elements;
+
+ if (mpe ->Type != cmsSigNamedColorElemType) return NULL;
+ return (cmsNAMEDCOLORLIST*) mpe ->Data;
+}
+
+
+// Profile sequence description routines -------------------------------------------------------------------------------------
+
+cmsSEQ* CMSEXPORT cmsAllocProfileSequenceDescription(cmsContext ContextID, cmsUInt32Number n)
+{
+ cmsSEQ* Seq;
+ cmsUInt32Number i;
+
+ if (n == 0) return NULL;
+
+ // In a absolutely arbitrary way, I hereby decide to allow a maxim of 255 profiles linked
+ // in a devicelink. It makes not sense anyway and may be used for exploits, so let's close the door!
+ if (n > 255) return NULL;
+
+ Seq = (cmsSEQ*) _cmsMallocZero(ContextID, sizeof(cmsSEQ));
+ if (Seq == NULL) return NULL;
+
+ Seq -> ContextID = ContextID;
+ Seq -> seq = (cmsPSEQDESC*) _cmsCalloc(ContextID, n, sizeof(cmsPSEQDESC));
+ Seq -> n = n;
+
+ if (Seq -> seq == NULL) {
+ _cmsFree(ContextID, Seq);
+ return NULL;
+ }
+
+ for (i=0; i < n; i++) {
+ Seq -> seq[i].Manufacturer = NULL;
+ Seq -> seq[i].Model = NULL;
+ Seq -> seq[i].Description = NULL;
+ }
+
+ return Seq;
+}
+
+void CMSEXPORT cmsFreeProfileSequenceDescription(cmsSEQ* pseq)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < pseq ->n; i++) {
+ if (pseq ->seq[i].Manufacturer != NULL) cmsMLUfree(pseq ->seq[i].Manufacturer);
+ if (pseq ->seq[i].Model != NULL) cmsMLUfree(pseq ->seq[i].Model);
+ if (pseq ->seq[i].Description != NULL) cmsMLUfree(pseq ->seq[i].Description);
+ }
+
+ if (pseq ->seq != NULL) _cmsFree(pseq ->ContextID, pseq ->seq);
+ _cmsFree(pseq -> ContextID, pseq);
+}
+
+cmsSEQ* CMSEXPORT cmsDupProfileSequenceDescription(const cmsSEQ* pseq)
+{
+ cmsSEQ *NewSeq;
+ cmsUInt32Number i;
+
+ if (pseq == NULL)
+ return NULL;
+
+ NewSeq = (cmsSEQ*) _cmsMalloc(pseq -> ContextID, sizeof(cmsSEQ));
+ if (NewSeq == NULL) return NULL;
+
+
+ NewSeq -> seq = (cmsPSEQDESC*) _cmsCalloc(pseq ->ContextID, pseq ->n, sizeof(cmsPSEQDESC));
+ if (NewSeq ->seq == NULL) goto Error;
+
+ NewSeq -> ContextID = pseq ->ContextID;
+ NewSeq -> n = pseq ->n;
+
+ for (i=0; i < pseq->n; i++) {
+
+ memmove(&NewSeq ->seq[i].attributes, &pseq ->seq[i].attributes, sizeof(cmsUInt64Number));
+
+ NewSeq ->seq[i].deviceMfg = pseq ->seq[i].deviceMfg;
+ NewSeq ->seq[i].deviceModel = pseq ->seq[i].deviceModel;
+ memmove(&NewSeq ->seq[i].ProfileID, &pseq ->seq[i].ProfileID, sizeof(cmsProfileID));
+ NewSeq ->seq[i].technology = pseq ->seq[i].technology;
+
+ NewSeq ->seq[i].Manufacturer = cmsMLUdup(pseq ->seq[i].Manufacturer);
+ NewSeq ->seq[i].Model = cmsMLUdup(pseq ->seq[i].Model);
+ NewSeq ->seq[i].Description = cmsMLUdup(pseq ->seq[i].Description);
+
+ }
+
+ return NewSeq;
+
+Error:
+
+ cmsFreeProfileSequenceDescription(NewSeq);
+ return NULL;
+}
+
+// Dictionaries --------------------------------------------------------------------------------------------------------
+
+// Dictionaries are just very simple linked lists
+
+
+typedef struct _cmsDICT_struct {
+ cmsDICTentry* head;
+ cmsContext ContextID;
+} _cmsDICT;
+
+
+// Allocate an empty dictionary
+cmsHANDLE CMSEXPORT cmsDictAlloc(cmsContext ContextID)
+{
+ _cmsDICT* dict = (_cmsDICT*) _cmsMallocZero(ContextID, sizeof(_cmsDICT));
+ if (dict == NULL) return NULL;
+
+ dict ->ContextID = ContextID;
+ return (cmsHANDLE) dict;
+
+}
+
+// Dispose resources
+void CMSEXPORT cmsDictFree(cmsHANDLE hDict)
+{
+ _cmsDICT* dict = (_cmsDICT*) hDict;
+ cmsDICTentry *entry, *next;
+
+ _cmsAssert(dict != NULL);
+
+ // Walk the list freeing all nodes
+ entry = dict ->head;
+ while (entry != NULL) {
+
+ if (entry ->DisplayName != NULL) cmsMLUfree(entry ->DisplayName);
+ if (entry ->DisplayValue != NULL) cmsMLUfree(entry ->DisplayValue);
+ if (entry ->Name != NULL) _cmsFree(dict ->ContextID, entry -> Name);
+ if (entry ->Value != NULL) _cmsFree(dict ->ContextID, entry -> Value);
+
+ // Don't fall in the habitual trap...
+ next = entry ->Next;
+ _cmsFree(dict ->ContextID, entry);
+
+ entry = next;
+ }
+
+ _cmsFree(dict ->ContextID, dict);
+}
+
+
+// Duplicate a wide char string
+static
+wchar_t* DupWcs(cmsContext ContextID, const wchar_t* ptr)
+{
+ if (ptr == NULL) return NULL;
+ return (wchar_t*) _cmsDupMem(ContextID, ptr, (mywcslen(ptr) + 1) * sizeof(wchar_t));
+}
+
+// Add a new entry to the linked list
+cmsBool CMSEXPORT cmsDictAddEntry(cmsHANDLE hDict, const wchar_t* Name, const wchar_t* Value, const cmsMLU *DisplayName, const cmsMLU *DisplayValue)
+{
+ _cmsDICT* dict = (_cmsDICT*) hDict;
+ cmsDICTentry *entry;
+
+ _cmsAssert(dict != NULL);
+ _cmsAssert(Name != NULL);
+
+ entry = (cmsDICTentry*) _cmsMallocZero(dict ->ContextID, sizeof(cmsDICTentry));
+ if (entry == NULL) return FALSE;
+
+ entry ->DisplayName = cmsMLUdup(DisplayName);
+ entry ->DisplayValue = cmsMLUdup(DisplayValue);
+ entry ->Name = DupWcs(dict ->ContextID, Name);
+ entry ->Value = DupWcs(dict ->ContextID, Value);
+
+ entry ->Next = dict ->head;
+ dict ->head = entry;
+
+ return TRUE;
+}
+
+
+// Duplicates an existing dictionary
+cmsHANDLE CMSEXPORT cmsDictDup(cmsHANDLE hDict)
+{
+ _cmsDICT* old_dict = (_cmsDICT*) hDict;
+ cmsHANDLE hNew;
+ cmsDICTentry *entry;
+
+ _cmsAssert(old_dict != NULL);
+
+ hNew = cmsDictAlloc(old_dict ->ContextID);
+ if (hNew == NULL) return NULL;
+
+ // Walk the list freeing all nodes
+ entry = old_dict ->head;
+ while (entry != NULL) {
+
+ if (!cmsDictAddEntry(hNew, entry ->Name, entry ->Value, entry ->DisplayName, entry ->DisplayValue)) {
+
+ cmsDictFree(hNew);
+ return NULL;
+ }
+
+ entry = entry -> Next;
+ }
+
+ return hNew;
+}
+
+// Get a pointer to the linked list
+const cmsDICTentry* CMSEXPORT cmsDictGetEntryList(cmsHANDLE hDict)
+{
+ _cmsDICT* dict = (_cmsDICT*) hDict;
+
+ if (dict == NULL) return NULL;
+ return dict ->head;
+}
+
+// Helper For external languages
+const cmsDICTentry* CMSEXPORT cmsDictNextEntry(const cmsDICTentry* e)
+{
+ if (e == NULL) return NULL;
+ return e ->Next;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsopt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsopt.c
new file mode 100644
index 0000000000..f885ef3f60
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsopt.c
@@ -0,0 +1,1795 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2011 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"
+
+
+//----------------------------------------------------------------------------------
+
+// Optimization for 8 bits, Shaper-CLUT (3 inputs only)
+typedef struct {
+
+ cmsContext ContextID;
+
+ const cmsInterpParams* p; // Tetrahedrical interpolation parameters. This is a not-owned pointer.
+
+ cmsUInt16Number rx[256], ry[256], rz[256];
+ cmsUInt32Number X0[256], Y0[256], Z0[256]; // Precomputed nodes and offsets for 8-bit input data
+
+
+} Prelin8Data;
+
+
+// Generic optimization for 16 bits Shaper-CLUT-Shaper (any inputs)
+typedef struct {
+
+ cmsContext ContextID;
+
+ // Number of channels
+ int nInputs;
+ int nOutputs;
+
+ _cmsInterpFn16 EvalCurveIn16[MAX_INPUT_DIMENSIONS]; // The maximum number of input channels is known in advance
+ cmsInterpParams* ParamsCurveIn16[MAX_INPUT_DIMENSIONS];
+
+ _cmsInterpFn16 EvalCLUT; // The evaluator for 3D grid
+ const cmsInterpParams* CLUTparams; // (not-owned pointer)
+
+
+ _cmsInterpFn16* EvalCurveOut16; // Points to an array of curve evaluators in 16 bits (not-owned pointer)
+ cmsInterpParams** ParamsCurveOut16; // Points to an array of references to interpolation params (not-owned pointer)
+
+
+} Prelin16Data;
+
+
+// Optimization for matrix-shaper in 8 bits. Numbers are operated in n.14 signed, tables are stored in 1.14 fixed
+
+typedef cmsInt32Number cmsS1Fixed14Number; // Note that this may hold more than 16 bits!
+
+#define DOUBLE_TO_1FIXED14(x) ((cmsS1Fixed14Number) floor((x) * 16384.0 + 0.5))
+
+typedef struct {
+
+ cmsContext ContextID;
+
+ cmsS1Fixed14Number Shaper1R[256]; // from 0..255 to 1.14 (0.0...1.0)
+ cmsS1Fixed14Number Shaper1G[256];
+ cmsS1Fixed14Number Shaper1B[256];
+
+ cmsS1Fixed14Number Mat[3][3]; // n.14 to n.14 (needs a saturation after that)
+ cmsS1Fixed14Number Off[3];
+
+ cmsUInt16Number Shaper2R[16385]; // 1.14 to 0..255
+ cmsUInt16Number Shaper2G[16385];
+ cmsUInt16Number Shaper2B[16385];
+
+} MatShaper8Data;
+
+// Curves, optimization is shared between 8 and 16 bits
+typedef struct {
+
+ cmsContext ContextID;
+
+ int nCurves; // Number of curves
+ int nElements; // Elements in curves
+ cmsUInt16Number** Curves; // Points to a dynamically allocated array
+
+} Curves16Data;
+
+
+// Simple optimizations ----------------------------------------------------------------------------------------------------------
+
+
+// Remove an element in linked chain
+static
+void _RemoveElement(cmsStage** head)
+{
+ cmsStage* mpe = *head;
+ cmsStage* next = mpe ->Next;
+ *head = next;
+ cmsStageFree(mpe);
+}
+
+// Remove all identities in chain. Note that pt actually is a double pointer to the element that holds the pointer.
+static
+cmsBool _Remove1Op(cmsPipeline* Lut, cmsStageSignature UnaryOp)
+{
+ cmsStage** pt = &Lut ->Elements;
+ cmsBool AnyOpt = FALSE;
+
+ while (*pt != NULL) {
+
+ if ((*pt) ->Implements == UnaryOp) {
+ _RemoveElement(pt);
+ AnyOpt = TRUE;
+ }
+ else
+ pt = &((*pt) -> Next);
+ }
+
+ return AnyOpt;
+}
+
+// Same, but only if two adjacent elements are found
+static
+cmsBool _Remove2Op(cmsPipeline* Lut, cmsStageSignature Op1, cmsStageSignature Op2)
+{
+ cmsStage** pt1;
+ cmsStage** pt2;
+ cmsBool AnyOpt = FALSE;
+
+ pt1 = &Lut ->Elements;
+ if (*pt1 == NULL) return AnyOpt;
+
+ while (*pt1 != NULL) {
+
+ pt2 = &((*pt1) -> Next);
+ if (*pt2 == NULL) return AnyOpt;
+
+ if ((*pt1) ->Implements == Op1 && (*pt2) ->Implements == Op2) {
+ _RemoveElement(pt2);
+ _RemoveElement(pt1);
+ AnyOpt = TRUE;
+ }
+ else
+ pt1 = &((*pt1) -> Next);
+ }
+
+ return AnyOpt;
+}
+
+// Preoptimize just gets rif of no-ops coming paired. Conversion from v2 to v4 followed
+// by a v4 to v2 and vice-versa. The elements are then discarded.
+static
+cmsBool PreOptimize(cmsPipeline* Lut)
+{
+ cmsBool AnyOpt = FALSE, Opt;
+
+ do {
+
+ Opt = FALSE;
+
+ // Remove all identities
+ Opt |= _Remove1Op(Lut, cmsSigIdentityElemType);
+
+ // Remove XYZ2Lab followed by Lab2XYZ
+ Opt |= _Remove2Op(Lut, cmsSigXYZ2LabElemType, cmsSigLab2XYZElemType);
+
+ // Remove Lab2XYZ followed by XYZ2Lab
+ Opt |= _Remove2Op(Lut, cmsSigLab2XYZElemType, cmsSigXYZ2LabElemType);
+
+ // Remove V4 to V2 followed by V2 to V4
+ Opt |= _Remove2Op(Lut, cmsSigLabV4toV2, cmsSigLabV2toV4);
+
+ // Remove V2 to V4 followed by V4 to V2
+ Opt |= _Remove2Op(Lut, cmsSigLabV2toV4, cmsSigLabV4toV2);
+
+ // Remove float pcs Lab conversions
+ Opt |= _Remove2Op(Lut, cmsSigLab2FloatPCS, cmsSigFloatPCS2Lab);
+
+ // Remove float pcs Lab conversions
+ Opt |= _Remove2Op(Lut, cmsSigXYZ2FloatPCS, cmsSigFloatPCS2XYZ);
+
+ if (Opt) AnyOpt = TRUE;
+
+ } while (Opt);
+
+ return AnyOpt;
+}
+
+static
+void Eval16nop1D(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const struct _cms_interp_struc* p)
+{
+ Output[0] = Input[0];
+
+ cmsUNUSED_PARAMETER(p);
+}
+
+static
+void PrelinEval16(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const void* D)
+{
+ Prelin16Data* p16 = (Prelin16Data*) D;
+ cmsUInt16Number StageABC[MAX_INPUT_DIMENSIONS];
+ cmsUInt16Number StageDEF[cmsMAXCHANNELS];
+ int i;
+
+ for (i=0; i < p16 ->nInputs; i++) {
+
+ p16 ->EvalCurveIn16[i](&Input[i], &StageABC[i], p16 ->ParamsCurveIn16[i]);
+ }
+
+ p16 ->EvalCLUT(StageABC, StageDEF, p16 ->CLUTparams);
+
+ for (i=0; i < p16 ->nOutputs; i++) {
+
+ p16 ->EvalCurveOut16[i](&StageDEF[i], &Output[i], p16 ->ParamsCurveOut16[i]);
+ }
+}
+
+
+static
+void PrelinOpt16free(cmsContext ContextID, void* ptr)
+{
+ Prelin16Data* p16 = (Prelin16Data*) ptr;
+
+ _cmsFree(ContextID, p16 ->EvalCurveOut16);
+ _cmsFree(ContextID, p16 ->ParamsCurveOut16);
+
+ _cmsFree(ContextID, p16);
+}
+
+static
+void* Prelin16dup(cmsContext ContextID, const void* ptr)
+{
+ Prelin16Data* p16 = (Prelin16Data*) ptr;
+ Prelin16Data* Duped = _cmsDupMem(ContextID, p16, sizeof(Prelin16Data));
+
+ if (Duped == NULL) return NULL;
+
+ Duped ->EvalCurveOut16 = (_cmsInterpFn16*)_cmsDupMem(ContextID, p16 ->EvalCurveOut16, p16 ->nOutputs * sizeof(_cmsInterpFn16));
+ Duped ->ParamsCurveOut16 = (cmsInterpParams**)_cmsDupMem(ContextID, p16 ->ParamsCurveOut16, p16 ->nOutputs * sizeof(cmsInterpParams* ));
+
+ return Duped;
+}
+
+
+static
+Prelin16Data* PrelinOpt16alloc(cmsContext ContextID,
+ const cmsInterpParams* ColorMap,
+ int nInputs, cmsToneCurve** In,
+ int nOutputs, cmsToneCurve** Out )
+{
+ int i;
+ Prelin16Data* p16 = _cmsMallocZero(ContextID, sizeof(Prelin16Data));
+ if (p16 == NULL) return NULL;
+
+ p16 ->nInputs = nInputs;
+ p16 -> nOutputs = nOutputs;
+
+
+ for (i=0; i < nInputs; i++) {
+
+ if (In == NULL) {
+ p16 -> ParamsCurveIn16[i] = NULL;
+ p16 -> EvalCurveIn16[i] = Eval16nop1D;
+
+ }
+ else {
+ p16 -> ParamsCurveIn16[i] = In[i] ->InterpParams;
+ p16 -> EvalCurveIn16[i] = p16 ->ParamsCurveIn16[i]->Interpolation.Lerp16;
+ }
+ }
+
+ p16 ->CLUTparams = ColorMap;
+ p16 ->EvalCLUT = ColorMap ->Interpolation.Lerp16;
+
+
+ p16 -> EvalCurveOut16 = (_cmsInterpFn16*) _cmsCalloc(ContextID, nOutputs, sizeof(_cmsInterpFn16));
+ p16 -> ParamsCurveOut16 = (cmsInterpParams**) _cmsCalloc(ContextID, nOutputs, sizeof(cmsInterpParams* ));
+
+ for (i=0; i < nOutputs; i++) {
+
+ if (Out == NULL) {
+ p16 ->ParamsCurveOut16[i] = NULL;
+ p16 -> EvalCurveOut16[i] = Eval16nop1D;
+ }
+ else {
+
+ p16 ->ParamsCurveOut16[i] = Out[i] ->InterpParams;
+ p16 -> EvalCurveOut16[i] = p16 ->ParamsCurveOut16[i]->Interpolation.Lerp16;
+ }
+ }
+
+ return p16;
+}
+
+
+
+// Resampling ---------------------------------------------------------------------------------
+
+#define PRELINEARIZATION_POINTS 4096
+
+// Sampler implemented by another LUT. This is a clean way to precalculate the devicelink 3D CLUT for
+// almost any transform. We use floating point precision and then convert from floating point to 16 bits.
+static
+int XFormSampler16(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ cmsPipeline* Lut = (cmsPipeline*) Cargo;
+ cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
+ cmsUInt32Number i;
+
+ _cmsAssert(Lut -> InputChannels < cmsMAXCHANNELS);
+ _cmsAssert(Lut -> OutputChannels < cmsMAXCHANNELS);
+
+ // From 16 bit to floating point
+ for (i=0; i < Lut ->InputChannels; i++)
+ InFloat[i] = (cmsFloat32Number) (In[i] / 65535.0);
+
+ // Evaluate in floating point
+ cmsPipelineEvalFloat(InFloat, OutFloat, Lut);
+
+ // Back to 16 bits representation
+ for (i=0; i < Lut ->OutputChannels; i++)
+ Out[i] = _cmsQuickSaturateWord(OutFloat[i] * 65535.0);
+
+ // Always succeed
+ return TRUE;
+}
+
+// Try to see if the curves of a given MPE are linear
+static
+cmsBool AllCurvesAreLinear(cmsStage* mpe)
+{
+ cmsToneCurve** Curves;
+ cmsUInt32Number i, n;
+
+ Curves = _cmsStageGetPtrToCurveSet(mpe);
+ if (Curves == NULL) return FALSE;
+
+ n = cmsStageOutputChannels(mpe);
+
+ for (i=0; i < n; i++) {
+ if (!cmsIsToneCurveLinear(Curves[i])) return FALSE;
+ }
+
+ return TRUE;
+}
+
+// This function replaces a specific node placed in "At" by the "Value" numbers. Its purpose
+// is to fix scum dot on broken profiles/transforms. Works on 1, 3 and 4 channels
+static
+cmsBool PatchLUT(cmsStage* CLUT, cmsUInt16Number At[], cmsUInt16Number Value[],
+ int nChannelsOut, int nChannelsIn)
+{
+ _cmsStageCLutData* Grid = (_cmsStageCLutData*) CLUT ->Data;
+ cmsInterpParams* p16 = Grid ->Params;
+ cmsFloat64Number px, py, pz, pw;
+ int x0, y0, z0, w0;
+ int i, index;
+
+ if (CLUT -> Type != cmsSigCLutElemType) {
+ cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) Attempt to PatchLUT on non-lut stage");
+ return FALSE;
+ }
+
+ if (nChannelsIn != 1 && nChannelsIn != 3 && nChannelsIn != 4) {
+ cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
+ return FALSE;
+ }
+ if (nChannelsIn == 4) {
+
+ px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
+ py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
+ pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
+ pw = ((cmsFloat64Number) At[3] * (p16->Domain[3])) / 65535.0;
+
+ x0 = (int) floor(px);
+ y0 = (int) floor(py);
+ z0 = (int) floor(pz);
+ w0 = (int) floor(pw);
+
+ if (((px - x0) != 0) ||
+ ((py - y0) != 0) ||
+ ((pz - z0) != 0) ||
+ ((pw - w0) != 0)) return FALSE; // Not on exact node
+
+ index = p16 -> opta[3] * x0 +
+ p16 -> opta[2] * y0 +
+ p16 -> opta[1] * z0 +
+ p16 -> opta[0] * w0;
+ }
+ else
+ if (nChannelsIn == 3) {
+
+ px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
+ py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
+ pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
+
+ x0 = (int) floor(px);
+ y0 = (int) floor(py);
+ z0 = (int) floor(pz);
+
+ if (((px - x0) != 0) ||
+ ((py - y0) != 0) ||
+ ((pz - z0) != 0)) return FALSE; // Not on exact node
+
+ index = p16 -> opta[2] * x0 +
+ p16 -> opta[1] * y0 +
+ p16 -> opta[0] * z0;
+ }
+ else
+ if (nChannelsIn == 1) {
+
+ px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
+
+ x0 = (int) floor(px);
+
+ if (((px - x0) != 0)) return FALSE; // Not on exact node
+
+ index = p16 -> opta[0] * x0;
+ }
+ else {
+ cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
+ return FALSE;
+ }
+
+ for (i=0; i < nChannelsOut; i++)
+ Grid -> Tab.T[index + i] = Value[i];
+
+ return TRUE;
+}
+
+// Auxiliar, to see if two values are equal or very different
+static
+cmsBool WhitesAreEqual(int n, cmsUInt16Number White1[], cmsUInt16Number White2[] )
+{
+ int i;
+
+ for (i=0; i < n; i++) {
+
+ if (abs(White1[i] - White2[i]) > 0xf000) return TRUE; // Values are so extremly different that the fixup should be avoided
+ if (White1[i] != White2[i]) return FALSE;
+ }
+ return TRUE;
+}
+
+
+// Locate the node for the white point and fix it to pure white in order to avoid scum dot.
+static
+cmsBool FixWhiteMisalignment(cmsPipeline* Lut, cmsColorSpaceSignature EntryColorSpace, cmsColorSpaceSignature ExitColorSpace)
+{
+ cmsUInt16Number *WhitePointIn, *WhitePointOut;
+ cmsUInt16Number WhiteIn[cmsMAXCHANNELS], WhiteOut[cmsMAXCHANNELS], ObtainedOut[cmsMAXCHANNELS];
+ cmsUInt32Number i, nOuts, nIns;
+ cmsStage *PreLin = NULL, *CLUT = NULL, *PostLin = NULL;
+
+ if (!_cmsEndPointsBySpace(EntryColorSpace,
+ &WhitePointIn, NULL, &nIns)) return FALSE;
+
+ if (!_cmsEndPointsBySpace(ExitColorSpace,
+ &WhitePointOut, NULL, &nOuts)) return FALSE;
+
+ // It needs to be fixed?
+ if (Lut ->InputChannels != nIns) return FALSE;
+ if (Lut ->OutputChannels != nOuts) return FALSE;
+
+ cmsPipelineEval16(WhitePointIn, ObtainedOut, Lut);
+
+ if (WhitesAreEqual(nOuts, WhitePointOut, ObtainedOut)) return TRUE; // whites already match
+
+ // Check if the LUT comes as Prelin, CLUT or Postlin. We allow all combinations
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &PreLin, &CLUT, &PostLin))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCurveSetElemType, cmsSigCLutElemType, &PreLin, &CLUT))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCLutElemType, cmsSigCurveSetElemType, &CLUT, &PostLin))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCLutElemType, &CLUT))
+ return FALSE;
+
+ // We need to interpolate white points of both, pre and post curves
+ if (PreLin) {
+
+ cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PreLin);
+
+ for (i=0; i < nIns; i++) {
+ WhiteIn[i] = cmsEvalToneCurve16(Curves[i], WhitePointIn[i]);
+ }
+ }
+ else {
+ for (i=0; i < nIns; i++)
+ WhiteIn[i] = WhitePointIn[i];
+ }
+
+ // If any post-linearization, we need to find how is represented white before the curve, do
+ // a reverse interpolation in this case.
+ if (PostLin) {
+
+ cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PostLin);
+
+ for (i=0; i < nOuts; i++) {
+
+ cmsToneCurve* InversePostLin = cmsReverseToneCurve(Curves[i]);
+ if (InversePostLin == NULL) {
+ WhiteOut[i] = WhitePointOut[i];
+
+ } else {
+
+ WhiteOut[i] = cmsEvalToneCurve16(InversePostLin, WhitePointOut[i]);
+ cmsFreeToneCurve(InversePostLin);
+ }
+ }
+ }
+ else {
+ for (i=0; i < nOuts; i++)
+ WhiteOut[i] = WhitePointOut[i];
+ }
+
+ // Ok, proceed with patching. May fail and we don't care if it fails
+ PatchLUT(CLUT, WhiteIn, WhiteOut, nOuts, nIns);
+
+ return TRUE;
+}
+
+// -----------------------------------------------------------------------------------------------------------------------------------------------
+// This function creates simple LUT from complex ones. The generated LUT has an optional set of
+// prelinearization curves, a CLUT of nGridPoints and optional postlinearization tables.
+// These curves have to exist in the original LUT in order to be used in the simplified output.
+// Caller may also use the flags to allow this feature.
+// LUTS with all curves will be simplified to a single curve. Parametric curves are lost.
+// This function should be used on 16-bits LUTS only, as floating point losses precision when simplified
+// -----------------------------------------------------------------------------------------------------------------------------------------------
+
+static
+cmsBool OptimizeByResampling(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
+{
+ cmsPipeline* Src = NULL;
+ cmsPipeline* Dest = NULL;
+ cmsStage* mpe;
+ cmsStage* CLUT;
+ cmsStage *KeepPreLin = NULL, *KeepPostLin = NULL;
+ int nGridPoints;
+ cmsColorSpaceSignature ColorSpace, OutputColorSpace;
+ cmsStage *NewPreLin = NULL;
+ cmsStage *NewPostLin = NULL;
+ _cmsStageCLutData* DataCLUT;
+ cmsToneCurve** DataSetIn;
+ cmsToneCurve** DataSetOut;
+ Prelin16Data* p16;
+
+ // This is a loosy optimization! does not apply in floating-point cases
+ if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
+
+ ColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*InputFormat));
+ OutputColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*OutputFormat));
+ nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
+
+ // For empty LUTs, 2 points are enough
+ if (cmsPipelineStageCount(*Lut) == 0)
+ nGridPoints = 2;
+
+ Src = *Lut;
+
+ // Named color pipelines cannot be optimized either
+ for (mpe = cmsPipelineGetPtrToFirstStage(Src);
+ mpe != NULL;
+ mpe = cmsStageNext(mpe)) {
+ if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
+ }
+
+ // Allocate an empty LUT
+ Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
+ if (!Dest) return FALSE;
+
+ // Prelinearization tables are kept unless indicated by flags
+ if (*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION) {
+
+ // Get a pointer to the prelinearization element
+ cmsStage* PreLin = cmsPipelineGetPtrToFirstStage(Src);
+
+ // Check if suitable
+ if (PreLin ->Type == cmsSigCurveSetElemType) {
+
+ // Maybe this is a linear tram, so we can avoid the whole stuff
+ if (!AllCurvesAreLinear(PreLin)) {
+
+ // All seems ok, proceed.
+ NewPreLin = cmsStageDup(PreLin);
+ if(!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, NewPreLin))
+ goto Error;
+
+ // Remove prelinearization. Since we have duplicated the curve
+ // in destination LUT, the sampling shoud be applied after this stage.
+ cmsPipelineUnlinkStage(Src, cmsAT_BEGIN, &KeepPreLin);
+ }
+ }
+ }
+
+ // Allocate the CLUT
+ CLUT = cmsStageAllocCLut16bit(Src ->ContextID, nGridPoints, Src ->InputChannels, Src->OutputChannels, NULL);
+ if (CLUT == NULL) return FALSE;
+
+ // Add the CLUT to the destination LUT
+ if (!cmsPipelineInsertStage(Dest, cmsAT_END, CLUT)) {
+ goto Error;
+ }
+
+ // Postlinearization tables are kept unless indicated by flags
+ if (*dwFlags & cmsFLAGS_CLUT_POST_LINEARIZATION) {
+
+ // Get a pointer to the postlinearization if present
+ cmsStage* PostLin = cmsPipelineGetPtrToLastStage(Src);
+
+ // Check if suitable
+ if (cmsStageType(PostLin) == cmsSigCurveSetElemType) {
+
+ // Maybe this is a linear tram, so we can avoid the whole stuff
+ if (!AllCurvesAreLinear(PostLin)) {
+
+ // All seems ok, proceed.
+ NewPostLin = cmsStageDup(PostLin);
+ if (!cmsPipelineInsertStage(Dest, cmsAT_END, NewPostLin))
+ goto Error;
+
+ // In destination LUT, the sampling shoud be applied after this stage.
+ cmsPipelineUnlinkStage(Src, cmsAT_END, &KeepPostLin);
+ }
+ }
+ }
+
+ // Now its time to do the sampling. We have to ignore pre/post linearization
+ // The source LUT whithout pre/post curves is passed as parameter.
+ if (!cmsStageSampleCLut16bit(CLUT, XFormSampler16, (void*) Src, 0)) {
+Error:
+ // Ops, something went wrong, Restore stages
+ if (KeepPreLin != NULL) {
+ if (!cmsPipelineInsertStage(Src, cmsAT_BEGIN, KeepPreLin)) {
+ _cmsAssert(0); // This never happens
+ }
+ }
+ if (KeepPostLin != NULL) {
+ if (!cmsPipelineInsertStage(Src, cmsAT_END, KeepPostLin)) {
+ _cmsAssert(0); // This never happens
+ }
+ }
+ cmsPipelineFree(Dest);
+ return FALSE;
+ }
+
+ // Done.
+
+ if (KeepPreLin != NULL) cmsStageFree(KeepPreLin);
+ if (KeepPostLin != NULL) cmsStageFree(KeepPostLin);
+ cmsPipelineFree(Src);
+
+ DataCLUT = (_cmsStageCLutData*) CLUT ->Data;
+
+ if (NewPreLin == NULL) DataSetIn = NULL;
+ else DataSetIn = ((_cmsStageToneCurvesData*) NewPreLin ->Data) ->TheCurves;
+
+ if (NewPostLin == NULL) DataSetOut = NULL;
+ else DataSetOut = ((_cmsStageToneCurvesData*) NewPostLin ->Data) ->TheCurves;
+
+
+ if (DataSetIn == NULL && DataSetOut == NULL) {
+
+ _cmsPipelineSetOptimizationParameters(Dest, (_cmsOPTeval16Fn) DataCLUT->Params->Interpolation.Lerp16, DataCLUT->Params, NULL, NULL);
+ }
+ else {
+
+ p16 = PrelinOpt16alloc(Dest ->ContextID,
+ DataCLUT ->Params,
+ Dest ->InputChannels,
+ DataSetIn,
+ Dest ->OutputChannels,
+ DataSetOut);
+
+ _cmsPipelineSetOptimizationParameters(Dest, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
+ }
+
+
+ // Don't fix white on absolute colorimetric
+ if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
+ *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
+
+ if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
+
+ FixWhiteMisalignment(Dest, ColorSpace, OutputColorSpace);
+ }
+
+ *Lut = Dest;
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(Intent);
+}
+
+
+// -----------------------------------------------------------------------------------------------------------------------------------------------
+// Fixes the gamma balancing of transform. This is described in my paper "Prelinearization Stages on
+// Color-Management Application-Specific Integrated Circuits (ASICs)" presented at NIP24. It only works
+// for RGB transforms. See the paper for more details
+// -----------------------------------------------------------------------------------------------------------------------------------------------
+
+
+// Normalize endpoints by slope limiting max and min. This assures endpoints as well.
+// Descending curves are handled as well.
+static
+void SlopeLimiting(cmsToneCurve* g)
+{
+ int BeginVal, EndVal;
+ int AtBegin = (int) floor((cmsFloat64Number) g ->nEntries * 0.02 + 0.5); // Cutoff at 2%
+ int AtEnd = g ->nEntries - AtBegin - 1; // And 98%
+ cmsFloat64Number Val, Slope, beta;
+ int i;
+
+ if (cmsIsToneCurveDescending(g)) {
+ BeginVal = 0xffff; EndVal = 0;
+ }
+ else {
+ BeginVal = 0; EndVal = 0xffff;
+ }
+
+ // Compute slope and offset for begin of curve
+ Val = g ->Table16[AtBegin];
+ Slope = (Val - BeginVal) / AtBegin;
+ beta = Val - Slope * AtBegin;
+
+ for (i=0; i < AtBegin; i++)
+ g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
+
+ // Compute slope and offset for the end
+ Val = g ->Table16[AtEnd];
+ Slope = (EndVal - Val) / AtBegin; // AtBegin holds the X interval, which is same in both cases
+ beta = Val - Slope * AtEnd;
+
+ for (i = AtEnd; i < (int) g ->nEntries; i++)
+ g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
+}
+
+
+// Precomputes tables for 8-bit on input devicelink.
+static
+Prelin8Data* PrelinOpt8alloc(cmsContext ContextID, const cmsInterpParams* p, cmsToneCurve* G[3])
+{
+ int i;
+ cmsUInt16Number Input[3];
+ cmsS15Fixed16Number v1, v2, v3;
+ Prelin8Data* p8;
+
+ p8 = (Prelin8Data*)_cmsMallocZero(ContextID, sizeof(Prelin8Data));
+ if (p8 == NULL) return NULL;
+
+ // Since this only works for 8 bit input, values comes always as x * 257,
+ // we can safely take msb byte (x << 8 + x)
+
+ for (i=0; i < 256; i++) {
+
+ if (G != NULL) {
+
+ // Get 16-bit representation
+ Input[0] = cmsEvalToneCurve16(G[0], FROM_8_TO_16(i));
+ Input[1] = cmsEvalToneCurve16(G[1], FROM_8_TO_16(i));
+ Input[2] = cmsEvalToneCurve16(G[2], FROM_8_TO_16(i));
+ }
+ else {
+ Input[0] = FROM_8_TO_16(i);
+ Input[1] = FROM_8_TO_16(i);
+ Input[2] = FROM_8_TO_16(i);
+ }
+
+
+ // Move to 0..1.0 in fixed domain
+ v1 = _cmsToFixedDomain(Input[0] * p -> Domain[0]);
+ v2 = _cmsToFixedDomain(Input[1] * p -> Domain[1]);
+ v3 = _cmsToFixedDomain(Input[2] * p -> Domain[2]);
+
+ // Store the precalculated table of nodes
+ p8 ->X0[i] = (p->opta[2] * FIXED_TO_INT(v1));
+ p8 ->Y0[i] = (p->opta[1] * FIXED_TO_INT(v2));
+ p8 ->Z0[i] = (p->opta[0] * FIXED_TO_INT(v3));
+
+ // Store the precalculated table of offsets
+ p8 ->rx[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v1);
+ p8 ->ry[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v2);
+ p8 ->rz[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v3);
+ }
+
+ p8 ->ContextID = ContextID;
+ p8 ->p = p;
+
+ return p8;
+}
+
+static
+void Prelin8free(cmsContext ContextID, void* ptr)
+{
+ _cmsFree(ContextID, ptr);
+}
+
+static
+void* Prelin8dup(cmsContext ContextID, const void* ptr)
+{
+ return _cmsDupMem(ContextID, ptr, sizeof(Prelin8Data));
+}
+
+
+
+// A optimized interpolation for 8-bit input.
+#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
+static
+void PrelinEval8(register const cmsUInt16Number Input[],
+ register cmsUInt16Number Output[],
+ register const void* D)
+{
+
+ cmsUInt8Number r, g, b;
+ cmsS15Fixed16Number rx, ry, rz;
+ cmsS15Fixed16Number c0, c1, c2, c3, Rest;
+ int OutChan;
+ register cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
+ Prelin8Data* p8 = (Prelin8Data*) D;
+ register const cmsInterpParams* p = p8 ->p;
+ int TotalOut = p -> nOutputs;
+ const cmsUInt16Number* LutTable = (const cmsUInt16Number*)p -> Table;
+
+ r = Input[0] >> 8;
+ g = Input[1] >> 8;
+ b = Input[2] >> 8;
+
+ X0 = X1 = p8->X0[r];
+ Y0 = Y1 = p8->Y0[g];
+ Z0 = Z1 = p8->Z0[b];
+
+ rx = p8 ->rx[r];
+ ry = p8 ->ry[g];
+ rz = p8 ->rz[b];
+
+ X1 = X0 + ((rx == 0) ? 0 : p ->opta[2]);
+ Y1 = Y0 + ((ry == 0) ? 0 : p ->opta[1]);
+ Z1 = Z0 + ((rz == 0) ? 0 : p ->opta[0]);
+
+
+ // These are the 6 Tetrahedral
+ for (OutChan=0; OutChan < TotalOut; OutChan++) {
+
+ c0 = DENS(X0, Y0, Z0);
+
+ if (rx >= ry && ry >= rz)
+ {
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+ }
+ else
+ if (rx >= rz && rz >= ry)
+ {
+ c1 = DENS(X1, Y0, Z0) - c0;
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
+ }
+ else
+ if (rz >= rx && rx >= ry)
+ {
+ c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
+ c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+ }
+ else
+ if (ry >= rx && rx >= rz)
+ {
+ c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
+ }
+ else
+ if (ry >= rz && rz >= rx)
+ {
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z0) - c0;
+ c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
+ }
+ else
+ if (rz >= ry && ry >= rx)
+ {
+ c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
+ c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
+ c3 = DENS(X0, Y0, Z1) - c0;
+ }
+ else {
+ c1 = c2 = c3 = 0;
+ }
+
+
+ Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
+ Output[OutChan] = (cmsUInt16Number)c0 + ((Rest + (Rest>>16))>>16);
+
+ }
+}
+
+#undef DENS
+
+
+// Curves that contain wide empty areas are not optimizeable
+static
+cmsBool IsDegenerated(const cmsToneCurve* g)
+{
+ int i, Zeros = 0, Poles = 0;
+ int nEntries = g ->nEntries;
+
+ for (i=0; i < nEntries; i++) {
+
+ if (g ->Table16[i] == 0x0000) Zeros++;
+ if (g ->Table16[i] == 0xffff) Poles++;
+ }
+
+ if (Zeros == 1 && Poles == 1) return FALSE; // For linear tables
+ if (Zeros > (nEntries / 4)) return TRUE; // Degenerated, mostly zeros
+ if (Poles > (nEntries / 4)) return TRUE; // Degenerated, mostly poles
+
+ return FALSE;
+}
+
+// --------------------------------------------------------------------------------------------------------------
+// We need xput over here
+
+static
+cmsBool OptimizeByComputingLinearization(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
+{
+ cmsPipeline* OriginalLut;
+ int nGridPoints;
+ cmsToneCurve *Trans[cmsMAXCHANNELS], *TransReverse[cmsMAXCHANNELS];
+ cmsUInt32Number t, i;
+ cmsFloat32Number v, In[cmsMAXCHANNELS], Out[cmsMAXCHANNELS];
+ cmsBool lIsSuitable, lIsLinear;
+ cmsPipeline* OptimizedLUT = NULL, *LutPlusCurves = NULL;
+ cmsStage* OptimizedCLUTmpe;
+ cmsColorSpaceSignature ColorSpace, OutputColorSpace;
+ cmsStage* OptimizedPrelinMpe;
+ cmsStage* mpe;
+ cmsToneCurve** OptimizedPrelinCurves;
+ _cmsStageCLutData* OptimizedPrelinCLUT;
+
+
+ // This is a loosy optimization! does not apply in floating-point cases
+ if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
+
+ // Only on RGB
+ if (T_COLORSPACE(*InputFormat) != PT_RGB) return FALSE;
+ if (T_COLORSPACE(*OutputFormat) != PT_RGB) return FALSE;
+
+
+ // On 16 bits, user has to specify the feature
+ if (!_cmsFormatterIs8bit(*InputFormat)) {
+ if (!(*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION)) return FALSE;
+ }
+
+ OriginalLut = *Lut;
+
+ // Named color pipelines cannot be optimized either
+ for (mpe = cmsPipelineGetPtrToFirstStage(OriginalLut);
+ mpe != NULL;
+ mpe = cmsStageNext(mpe)) {
+ if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
+ }
+
+ ColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*InputFormat));
+ OutputColorSpace = _cmsICCcolorSpace(T_COLORSPACE(*OutputFormat));
+ nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
+
+ // Empty gamma containers
+ memset(Trans, 0, sizeof(Trans));
+ memset(TransReverse, 0, sizeof(TransReverse));
+
+ for (t = 0; t < OriginalLut ->InputChannels; t++) {
+ Trans[t] = cmsBuildTabulatedToneCurve16(OriginalLut ->ContextID, PRELINEARIZATION_POINTS, NULL);
+ if (Trans[t] == NULL) goto Error;
+ }
+
+ // Populate the curves
+ for (i=0; i < PRELINEARIZATION_POINTS; i++) {
+
+ v = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
+
+ // Feed input with a gray ramp
+ for (t=0; t < OriginalLut ->InputChannels; t++)
+ In[t] = v;
+
+ // Evaluate the gray value
+ cmsPipelineEvalFloat(In, Out, OriginalLut);
+
+ // Store result in curve
+ for (t=0; t < OriginalLut ->InputChannels; t++)
+ Trans[t] ->Table16[i] = _cmsQuickSaturateWord(Out[t] * 65535.0);
+ }
+
+ // Slope-limit the obtained curves
+ for (t = 0; t < OriginalLut ->InputChannels; t++)
+ SlopeLimiting(Trans[t]);
+
+ // Check for validity
+ lIsSuitable = TRUE;
+ lIsLinear = TRUE;
+ for (t=0; (lIsSuitable && (t < OriginalLut ->InputChannels)); t++) {
+
+ // Exclude if already linear
+ if (!cmsIsToneCurveLinear(Trans[t]))
+ lIsLinear = FALSE;
+
+ // Exclude if non-monotonic
+ if (!cmsIsToneCurveMonotonic(Trans[t]))
+ lIsSuitable = FALSE;
+
+ if (IsDegenerated(Trans[t]))
+ lIsSuitable = FALSE;
+ }
+
+ // If it is not suitable, just quit
+ if (!lIsSuitable) goto Error;
+
+ // Invert curves if possible
+ for (t = 0; t < OriginalLut ->InputChannels; t++) {
+ TransReverse[t] = cmsReverseToneCurveEx(PRELINEARIZATION_POINTS, Trans[t]);
+ if (TransReverse[t] == NULL) goto Error;
+ }
+
+ // Now inset the reversed curves at the begin of transform
+ LutPlusCurves = cmsPipelineDup(OriginalLut);
+ if (LutPlusCurves == NULL) goto Error;
+
+ if (!cmsPipelineInsertStage(LutPlusCurves, cmsAT_BEGIN, cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, TransReverse)))
+ goto Error;
+
+ // Create the result LUT
+ OptimizedLUT = cmsPipelineAlloc(OriginalLut ->ContextID, OriginalLut ->InputChannels, OriginalLut ->OutputChannels);
+ if (OptimizedLUT == NULL) goto Error;
+
+ OptimizedPrelinMpe = cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, Trans);
+
+ // Create and insert the curves at the beginning
+ if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_BEGIN, OptimizedPrelinMpe))
+ goto Error;
+
+ // Allocate the CLUT for result
+ OptimizedCLUTmpe = cmsStageAllocCLut16bit(OriginalLut ->ContextID, nGridPoints, OriginalLut ->InputChannels, OriginalLut ->OutputChannels, NULL);
+
+ // Add the CLUT to the destination LUT
+ if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_END, OptimizedCLUTmpe))
+ goto Error;
+
+ // Resample the LUT
+ if (!cmsStageSampleCLut16bit(OptimizedCLUTmpe, XFormSampler16, (void*) LutPlusCurves, 0)) goto Error;
+
+ // Free resources
+ for (t = 0; t < OriginalLut ->InputChannels; t++) {
+
+ if (Trans[t]) cmsFreeToneCurve(Trans[t]);
+ if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
+ }
+
+ cmsPipelineFree(LutPlusCurves);
+
+
+ OptimizedPrelinCurves = _cmsStageGetPtrToCurveSet(OptimizedPrelinMpe);
+ OptimizedPrelinCLUT = (_cmsStageCLutData*) OptimizedCLUTmpe ->Data;
+
+ // Set the evaluator if 8-bit
+ if (_cmsFormatterIs8bit(*InputFormat)) {
+
+ Prelin8Data* p8 = PrelinOpt8alloc(OptimizedLUT ->ContextID,
+ OptimizedPrelinCLUT ->Params,
+ OptimizedPrelinCurves);
+ if (p8 == NULL) return FALSE;
+
+ _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval8, (void*) p8, Prelin8free, Prelin8dup);
+
+ }
+ else
+ {
+ Prelin16Data* p16 = PrelinOpt16alloc(OptimizedLUT ->ContextID,
+ OptimizedPrelinCLUT ->Params,
+ 3, OptimizedPrelinCurves, 3, NULL);
+ if (p16 == NULL) return FALSE;
+
+ _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
+
+ }
+
+ // Don't fix white on absolute colorimetric
+ if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
+ *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
+
+ if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
+
+ if (!FixWhiteMisalignment(OptimizedLUT, ColorSpace, OutputColorSpace)) {
+
+ return FALSE;
+ }
+ }
+
+ // And return the obtained LUT
+
+ cmsPipelineFree(OriginalLut);
+ *Lut = OptimizedLUT;
+ return TRUE;
+
+Error:
+
+ for (t = 0; t < OriginalLut ->InputChannels; t++) {
+
+ if (Trans[t]) cmsFreeToneCurve(Trans[t]);
+ if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
+ }
+
+ if (LutPlusCurves != NULL) cmsPipelineFree(LutPlusCurves);
+ if (OptimizedLUT != NULL) cmsPipelineFree(OptimizedLUT);
+
+ return FALSE;
+
+ cmsUNUSED_PARAMETER(Intent);
+}
+
+
+// Curves optimizer ------------------------------------------------------------------------------------------------------------------
+
+static
+void CurvesFree(cmsContext ContextID, void* ptr)
+{
+ Curves16Data* Data = (Curves16Data*) ptr;
+ int i;
+
+ for (i=0; i < Data -> nCurves; i++) {
+
+ _cmsFree(ContextID, Data ->Curves[i]);
+ }
+
+ _cmsFree(ContextID, Data ->Curves);
+ _cmsFree(ContextID, ptr);
+}
+
+static
+void* CurvesDup(cmsContext ContextID, const void* ptr)
+{
+ Curves16Data* Data = (Curves16Data*)_cmsDupMem(ContextID, ptr, sizeof(Curves16Data));
+ int i;
+
+ if (Data == NULL) return NULL;
+
+ Data ->Curves = (cmsUInt16Number**)_cmsDupMem(ContextID, Data ->Curves, Data ->nCurves * sizeof(cmsUInt16Number*));
+
+ for (i=0; i < Data -> nCurves; i++) {
+ Data ->Curves[i] = (cmsUInt16Number*)_cmsDupMem(ContextID, Data ->Curves[i], Data -> nElements * sizeof(cmsUInt16Number));
+ }
+
+ return (void*) Data;
+}
+
+// Precomputes tables for 8-bit on input devicelink.
+static
+Curves16Data* CurvesAlloc(cmsContext ContextID, int nCurves, int nElements, cmsToneCurve** G)
+{
+ int i, j;
+ Curves16Data* c16;
+
+ c16 = (Curves16Data*)_cmsMallocZero(ContextID, sizeof(Curves16Data));
+ if (c16 == NULL) return NULL;
+
+ c16 ->nCurves = nCurves;
+ c16 ->nElements = nElements;
+
+ c16 ->Curves = (cmsUInt16Number**)_cmsCalloc(ContextID, nCurves, sizeof(cmsUInt16Number*));
+ if (c16 ->Curves == NULL) return NULL;
+
+ for (i=0; i < nCurves; i++) {
+
+ c16->Curves[i] = (cmsUInt16Number*)_cmsCalloc(ContextID, nElements, sizeof(cmsUInt16Number));
+
+ if (c16->Curves[i] == NULL) {
+
+ for (j=0; j < i; j++) {
+ _cmsFree(ContextID, c16->Curves[j]);
+ }
+ _cmsFree(ContextID, c16->Curves);
+ _cmsFree(ContextID, c16);
+ return NULL;
+ }
+
+ if (nElements == 256) {
+
+ for (j=0; j < nElements; j++) {
+
+ c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], FROM_8_TO_16(j));
+ }
+ }
+ else {
+
+ for (j=0; j < nElements; j++) {
+ c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], (cmsUInt16Number) j);
+ }
+ }
+ }
+
+ return c16;
+}
+
+static
+void FastEvaluateCurves8(register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register const void* D)
+{
+ Curves16Data* Data = (Curves16Data*) D;
+ cmsUInt8Number x;
+ int i;
+
+ for (i=0; i < Data ->nCurves; i++) {
+
+ x = (In[i] >> 8);
+ Out[i] = Data -> Curves[i][x];
+ }
+}
+
+
+static
+void FastEvaluateCurves16(register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register const void* D)
+{
+ Curves16Data* Data = (Curves16Data*) D;
+ int i;
+
+ for (i=0; i < Data ->nCurves; i++) {
+ Out[i] = Data -> Curves[i][In[i]];
+ }
+}
+
+
+static
+void FastIdentity16(register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register const void* D)
+{
+ cmsPipeline* Lut = (cmsPipeline*) D;
+ cmsUInt32Number i;
+
+ for (i=0; i < Lut ->InputChannels; i++) {
+ Out[i] = In[i];
+ }
+}
+
+
+// If the target LUT holds only curves, the optimization procedure is to join all those
+// curves together. That only works on curves and does not work on matrices.
+static
+cmsBool OptimizeByJoiningCurves(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
+{
+ cmsToneCurve** GammaTables = NULL;
+ cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
+ cmsUInt32Number i, j;
+ cmsPipeline* Src = *Lut;
+ cmsPipeline* Dest = NULL;
+ cmsStage* mpe;
+ cmsStage* ObtainedCurves = NULL;
+
+
+ // This is a loosy optimization! does not apply in floating-point cases
+ if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
+
+ // Only curves in this LUT?
+ for (mpe = cmsPipelineGetPtrToFirstStage(Src);
+ mpe != NULL;
+ mpe = cmsStageNext(mpe)) {
+ if (cmsStageType(mpe) != cmsSigCurveSetElemType) return FALSE;
+ }
+
+ // Allocate an empty LUT
+ Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
+ if (Dest == NULL) return FALSE;
+
+ // Create target curves
+ GammaTables = (cmsToneCurve**) _cmsCalloc(Src ->ContextID, Src ->InputChannels, sizeof(cmsToneCurve*));
+ if (GammaTables == NULL) goto Error;
+
+ for (i=0; i < Src ->InputChannels; i++) {
+ GammaTables[i] = cmsBuildTabulatedToneCurve16(Src ->ContextID, PRELINEARIZATION_POINTS, NULL);
+ if (GammaTables[i] == NULL) goto Error;
+ }
+
+ // Compute 16 bit result by using floating point
+ for (i=0; i < PRELINEARIZATION_POINTS; i++) {
+
+ for (j=0; j < Src ->InputChannels; j++)
+ InFloat[j] = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
+
+ cmsPipelineEvalFloat(InFloat, OutFloat, Src);
+
+ for (j=0; j < Src ->InputChannels; j++)
+ GammaTables[j] -> Table16[i] = _cmsQuickSaturateWord(OutFloat[j] * 65535.0);
+ }
+
+ ObtainedCurves = cmsStageAllocToneCurves(Src ->ContextID, Src ->InputChannels, GammaTables);
+ if (ObtainedCurves == NULL) goto Error;
+
+ for (i=0; i < Src ->InputChannels; i++) {
+ cmsFreeToneCurve(GammaTables[i]);
+ GammaTables[i] = NULL;
+ }
+
+ if (GammaTables != NULL) _cmsFree(Src ->ContextID, GammaTables);
+
+ // Maybe the curves are linear at the end
+ if (!AllCurvesAreLinear(ObtainedCurves)) {
+
+ if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, ObtainedCurves))
+ goto Error;
+
+ // If the curves are to be applied in 8 bits, we can save memory
+ if (_cmsFormatterIs8bit(*InputFormat)) {
+
+ _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) ObtainedCurves ->Data;
+ Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 256, Data ->TheCurves);
+
+ if (c16 == NULL) goto Error;
+ *dwFlags |= cmsFLAGS_NOCACHE;
+ _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves8, c16, CurvesFree, CurvesDup);
+
+ }
+ else {
+
+ _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) cmsStageData(ObtainedCurves);
+ Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 65536, Data ->TheCurves);
+
+ if (c16 == NULL) goto Error;
+ *dwFlags |= cmsFLAGS_NOCACHE;
+ _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves16, c16, CurvesFree, CurvesDup);
+ }
+ }
+ else {
+
+ // LUT optimizes to nothing. Set the identity LUT
+ cmsStageFree(ObtainedCurves);
+
+ if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageAllocIdentity(Dest ->ContextID, Src ->InputChannels)))
+ goto Error;
+
+ *dwFlags |= cmsFLAGS_NOCACHE;
+ _cmsPipelineSetOptimizationParameters(Dest, FastIdentity16, (void*) Dest, NULL, NULL);
+ }
+
+ // We are done.
+ cmsPipelineFree(Src);
+ *Lut = Dest;
+ return TRUE;
+
+Error:
+
+ if (ObtainedCurves != NULL) cmsStageFree(ObtainedCurves);
+ if (GammaTables != NULL) {
+ for (i=0; i < Src ->InputChannels; i++) {
+ if (GammaTables[i] != NULL) cmsFreeToneCurve(GammaTables[i]);
+ }
+
+ _cmsFree(Src ->ContextID, GammaTables);
+ }
+
+ if (Dest != NULL) cmsPipelineFree(Dest);
+ return FALSE;
+
+ cmsUNUSED_PARAMETER(Intent);
+ cmsUNUSED_PARAMETER(InputFormat);
+ cmsUNUSED_PARAMETER(OutputFormat);
+ cmsUNUSED_PARAMETER(dwFlags);
+}
+
+// -------------------------------------------------------------------------------------------------------------------------------------
+// LUT is Shaper - Matrix - Matrix - Shaper, which is very frequent when combining two matrix-shaper profiles
+
+
+static
+void FreeMatShaper(cmsContext ContextID, void* Data)
+{
+ if (Data != NULL) _cmsFree(ContextID, Data);
+}
+
+static
+void* DupMatShaper(cmsContext ContextID, const void* Data)
+{
+ return _cmsDupMem(ContextID, Data, sizeof(MatShaper8Data));
+}
+
+
+// A fast matrix-shaper evaluator for 8 bits. This is a bit ticky since I'm using 1.14 signed fixed point
+// to accomplish some performance. Actually it takes 256x3 16 bits tables and 16385 x 3 tables of 8 bits,
+// in total about 50K, and the performance boost is huge!
+static
+void MatShaperEval16(register const cmsUInt16Number In[],
+ register cmsUInt16Number Out[],
+ register const void* D)
+{
+ MatShaper8Data* p = (MatShaper8Data*) D;
+ cmsS1Fixed14Number l1, l2, l3, r, g, b;
+ cmsUInt32Number ri, gi, bi;
+
+ // In this case (and only in this case!) we can use this simplification since
+ // In[] is assured to come from a 8 bit number. (a << 8 | a)
+ ri = In[0] & 0xFF;
+ gi = In[1] & 0xFF;
+ bi = In[2] & 0xFF;
+
+ // Across first shaper, which also converts to 1.14 fixed point
+ r = p->Shaper1R[ri];
+ g = p->Shaper1G[gi];
+ b = p->Shaper1B[bi];
+
+ // Evaluate the matrix in 1.14 fixed point
+ l1 = (p->Mat[0][0] * r + p->Mat[0][1] * g + p->Mat[0][2] * b + p->Off[0] + 0x2000) >> 14;
+ l2 = (p->Mat[1][0] * r + p->Mat[1][1] * g + p->Mat[1][2] * b + p->Off[1] + 0x2000) >> 14;
+ l3 = (p->Mat[2][0] * r + p->Mat[2][1] * g + p->Mat[2][2] * b + p->Off[2] + 0x2000) >> 14;
+
+ // Now we have to clip to 0..1.0 range
+ ri = (l1 < 0) ? 0 : ((l1 > 16384) ? 16384 : l1);
+ gi = (l2 < 0) ? 0 : ((l2 > 16384) ? 16384 : l2);
+ bi = (l3 < 0) ? 0 : ((l3 > 16384) ? 16384 : l3);
+
+ // And across second shaper,
+ Out[0] = p->Shaper2R[ri];
+ Out[1] = p->Shaper2G[gi];
+ Out[2] = p->Shaper2B[bi];
+
+}
+
+// This table converts from 8 bits to 1.14 after applying the curve
+static
+void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
+{
+ int i;
+ cmsFloat32Number R, y;
+
+ for (i=0; i < 256; i++) {
+
+ R = (cmsFloat32Number) (i / 255.0);
+ y = cmsEvalToneCurveFloat(Curve, R);
+
+ Table[i] = DOUBLE_TO_1FIXED14(y);
+ }
+}
+
+// This table converts form 1.14 (being 0x4000 the last entry) to 8 bits after applying the curve
+static
+void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
+{
+ int i;
+ cmsFloat32Number R, Val;
+
+ for (i=0; i < 16385; i++) {
+
+ R = (cmsFloat32Number) (i / 16384.0);
+ Val = cmsEvalToneCurveFloat(Curve, R); // Val comes 0..1.0
+
+ if (Is8BitsOutput) {
+
+ // If 8 bits output, we can optimize further by computing the / 257 part.
+ // first we compute the resulting byte and then we store the byte times
+ // 257. This quantization allows to round very quick by doing a >> 8, but
+ // since the low byte is always equal to msb, we can do a & 0xff and this works!
+ cmsUInt16Number w = _cmsQuickSaturateWord(Val * 65535.0);
+ cmsUInt8Number b = FROM_16_TO_8(w);
+
+ Table[i] = FROM_8_TO_16(b);
+ }
+ else Table[i] = _cmsQuickSaturateWord(Val * 65535.0);
+ }
+}
+
+// Compute the matrix-shaper structure
+static
+cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, cmsVEC3* Off, cmsToneCurve* Curve2[3], cmsUInt32Number* OutputFormat)
+{
+ MatShaper8Data* p;
+ int i, j;
+ cmsBool Is8Bits = _cmsFormatterIs8bit(*OutputFormat);
+
+ // Allocate a big chuck of memory to store precomputed tables
+ p = (MatShaper8Data*) _cmsMalloc(Dest ->ContextID, sizeof(MatShaper8Data));
+ if (p == NULL) return FALSE;
+
+ p -> ContextID = Dest -> ContextID;
+
+ // Precompute tables
+ FillFirstShaper(p ->Shaper1R, Curve1[0]);
+ FillFirstShaper(p ->Shaper1G, Curve1[1]);
+ FillFirstShaper(p ->Shaper1B, Curve1[2]);
+
+ FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits);
+ FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits);
+ FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits);
+
+ // Convert matrix to nFixed14. Note that those values may take more than 16 bits as
+ for (i=0; i < 3; i++) {
+ for (j=0; j < 3; j++) {
+ p ->Mat[i][j] = DOUBLE_TO_1FIXED14(Mat->v[i].n[j]);
+ }
+ }
+
+ for (i=0; i < 3; i++) {
+
+ if (Off == NULL) {
+ p ->Off[i] = 0;
+ }
+ else {
+ p ->Off[i] = DOUBLE_TO_1FIXED14(Off->n[i]);
+ }
+ }
+
+ // Mark as optimized for faster formatter
+ if (Is8Bits)
+ *OutputFormat |= OPTIMIZED_SH(1);
+
+ // Fill function pointers
+ _cmsPipelineSetOptimizationParameters(Dest, MatShaperEval16, (void*) p, FreeMatShaper, DupMatShaper);
+ return TRUE;
+}
+
+// 8 bits on input allows matrix-shaper boot up to 25 Mpixels per second on RGB. That's fast!
+// TODO: Allow a third matrix for abs. colorimetric
+static
+cmsBool OptimizeMatrixShaper(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
+{
+ cmsStage* Curve1, *Curve2;
+ cmsStage* Matrix1, *Matrix2;
+ _cmsStageMatrixData* Data1;
+ _cmsStageMatrixData* Data2;
+ cmsMAT3 res;
+ cmsBool IdentityMat;
+ cmsPipeline* Dest, *Src;
+
+ // Only works on RGB to RGB
+ if (T_CHANNELS(*InputFormat) != 3 || T_CHANNELS(*OutputFormat) != 3) return FALSE;
+
+ // Only works on 8 bit input
+ if (!_cmsFormatterIs8bit(*InputFormat)) return FALSE;
+
+ // Seems suitable, proceed
+ Src = *Lut;
+
+ // Check for shaper-matrix-matrix-shaper structure, that is what this optimizer stands for
+ if (!cmsPipelineCheckAndRetreiveStages(Src, 4,
+ cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
+ &Curve1, &Matrix1, &Matrix2, &Curve2)) return FALSE;
+
+ // Get both matrices
+ Data1 = (_cmsStageMatrixData*) cmsStageData(Matrix1);
+ Data2 = (_cmsStageMatrixData*) cmsStageData(Matrix2);
+
+ // Input offset should be zero
+ if (Data1 ->Offset != NULL) return FALSE;
+
+ // Multiply both matrices to get the result
+ _cmsMAT3per(&res, (cmsMAT3*) Data2 ->Double, (cmsMAT3*) Data1 ->Double);
+
+ // Now the result is in res + Data2 -> Offset. Maybe is a plain identity?
+ IdentityMat = FALSE;
+ if (_cmsMAT3isIdentity(&res) && Data2 ->Offset == NULL) {
+
+ // We can get rid of full matrix
+ IdentityMat = TRUE;
+ }
+
+ // Allocate an empty LUT
+ Dest = cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
+ if (!Dest) return FALSE;
+
+ // Assamble the new LUT
+ if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageDup(Curve1)))
+ goto Error;
+
+ if (!IdentityMat)
+ if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageAllocMatrix(Dest ->ContextID, 3, 3, (const cmsFloat64Number*) &res, Data2 ->Offset)))
+ goto Error;
+ if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageDup(Curve2)))
+ goto Error;
+
+ // If identity on matrix, we can further optimize the curves, so call the join curves routine
+ if (IdentityMat) {
+
+ OptimizeByJoiningCurves(&Dest, Intent, InputFormat, OutputFormat, dwFlags);
+ }
+ else {
+ _cmsStageToneCurvesData* mpeC1 = (_cmsStageToneCurvesData*) cmsStageData(Curve1);
+ _cmsStageToneCurvesData* mpeC2 = (_cmsStageToneCurvesData*) cmsStageData(Curve2);
+
+ // In this particular optimization, cach?does not help as it takes more time to deal with
+ // the cach?that with the pixel handling
+ *dwFlags |= cmsFLAGS_NOCACHE;
+
+ // Setup the optimizarion routines
+ SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Data2 ->Offset, mpeC2->TheCurves, OutputFormat);
+ }
+
+ cmsPipelineFree(Src);
+ *Lut = Dest;
+ return TRUE;
+Error:
+ // Leave Src unchanged
+ cmsPipelineFree(Dest);
+ return FALSE;
+}
+
+
+// -------------------------------------------------------------------------------------------------------------------------------------
+// Optimization plug-ins
+
+// List of optimizations
+typedef struct _cmsOptimizationCollection_st {
+
+ _cmsOPToptimizeFn OptimizePtr;
+
+ struct _cmsOptimizationCollection_st *Next;
+
+} _cmsOptimizationCollection;
+
+
+// The built-in list. We currently implement 4 types of optimizations. Joining of curves, matrix-shaper, linearization and resampling
+static _cmsOptimizationCollection DefaultOptimization[] = {
+
+ { OptimizeByJoiningCurves, &DefaultOptimization[1] },
+ { OptimizeMatrixShaper, &DefaultOptimization[2] },
+ { OptimizeByComputingLinearization, &DefaultOptimization[3] },
+ { OptimizeByResampling, NULL }
+};
+
+// The linked list head
+_cmsOptimizationPluginChunkType _cmsOptimizationPluginChunk = { NULL };
+
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupPluginOptimizationList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsOptimizationPluginChunkType newHead = { NULL };
+ _cmsOptimizationCollection* entry;
+ _cmsOptimizationCollection* Anterior = NULL;
+ _cmsOptimizationPluginChunkType* head = (_cmsOptimizationPluginChunkType*) src->chunks[OptimizationPlugin];
+
+ _cmsAssert(ctx != NULL);
+ _cmsAssert(head != NULL);
+
+ // Walk the list copying all nodes
+ for (entry = head->OptimizationCollection;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ _cmsOptimizationCollection *newEntry = ( _cmsOptimizationCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsOptimizationCollection));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.OptimizationCollection == NULL)
+ newHead.OptimizationCollection = newEntry;
+ }
+
+ ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsOptimizationPluginChunkType));
+}
+
+void _cmsAllocOptimizationPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ // Copy all linked list
+ DupPluginOptimizationList(ctx, src);
+ }
+ else {
+ static _cmsOptimizationPluginChunkType OptimizationPluginChunkType = { NULL };
+ ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx ->MemPool, &OptimizationPluginChunkType, sizeof(_cmsOptimizationPluginChunkType));
+ }
+}
+
+
+// Register new ways to optimize
+cmsBool _cmsRegisterOptimizationPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ cmsPluginOptimization* Plugin = (cmsPluginOptimization*) Data;
+ _cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
+ _cmsOptimizationCollection* fl;
+
+ if (Data == NULL) {
+
+ ctx->OptimizationCollection = NULL;
+ return TRUE;
+ }
+
+ // Optimizer callback is required
+ if (Plugin ->OptimizePtr == NULL) return FALSE;
+
+ fl = (_cmsOptimizationCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsOptimizationCollection));
+ if (fl == NULL) return FALSE;
+
+ // Copy the parameters
+ fl ->OptimizePtr = Plugin ->OptimizePtr;
+
+ // Keep linked list
+ fl ->Next = ctx->OptimizationCollection;
+
+ // Set the head
+ ctx ->OptimizationCollection = fl;
+
+ // All is ok
+ return TRUE;
+}
+
+// The entry point for LUT optimization
+cmsBool _cmsOptimizePipeline(cmsContext ContextID,
+ cmsPipeline** PtrLut,
+ int Intent,
+ cmsUInt32Number* InputFormat,
+ cmsUInt32Number* OutputFormat,
+ cmsUInt32Number* dwFlags)
+{
+ _cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
+ _cmsOptimizationCollection* Opts;
+ cmsBool AnySuccess = FALSE;
+
+ // A CLUT is being asked, so force this specific optimization
+ if (*dwFlags & cmsFLAGS_FORCE_CLUT) {
+
+ PreOptimize(*PtrLut);
+ return OptimizeByResampling(PtrLut, Intent, InputFormat, OutputFormat, dwFlags);
+ }
+
+ // Anything to optimize?
+ if ((*PtrLut) ->Elements == NULL) {
+ _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
+ return TRUE;
+ }
+
+ // Try to get rid of identities and trivial conversions.
+ AnySuccess = PreOptimize(*PtrLut);
+
+ // After removal do we end with an identity?
+ if ((*PtrLut) ->Elements == NULL) {
+ _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
+ return TRUE;
+ }
+
+ // Do not optimize, keep all precision
+ if (*dwFlags & cmsFLAGS_NOOPTIMIZE)
+ return FALSE;
+
+ // Try plug-in optimizations
+ for (Opts = ctx->OptimizationCollection;
+ Opts != NULL;
+ Opts = Opts ->Next) {
+
+ // If one schema succeeded, we are done
+ if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
+
+ return TRUE; // Optimized!
+ }
+ }
+
+ // Try built-in optimizations
+ for (Opts = DefaultOptimization;
+ Opts != NULL;
+ Opts = Opts ->Next) {
+
+ if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
+
+ return TRUE;
+ }
+ }
+
+ // Only simple optimizations succeeded
+ return AnySuccess;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspack.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspack.c
new file mode 100644
index 0000000000..9323b53ec5
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspack.c
@@ -0,0 +1,3369 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2010 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"
+
+// This module handles all formats supported by lcms. There are two flavors, 16 bits and
+// floating point. Floating point is supported only in a subset, those formats holding
+// cmsFloat32Number (4 bytes per component) and double (marked as 0 bytes per component
+// as special case)
+
+// ---------------------------------------------------------------------------
+
+
+// This macro return words stored as big endian
+#define CHANGE_ENDIAN(w) (cmsUInt16Number) ((cmsUInt16Number) ((w)<<8)|((w)>>8))
+
+// These macros handles reversing (negative)
+#define REVERSE_FLAVOR_8(x) ((cmsUInt8Number) (0xff-(x)))
+#define REVERSE_FLAVOR_16(x) ((cmsUInt16Number)(0xffff-(x)))
+
+// * 0xffff / 0xff00 = (255 * 257) / (255 * 256) = 257 / 256
+cmsINLINE cmsUInt16Number FomLabV2ToLabV4(cmsUInt16Number x)
+{
+ int a = (x << 8 | x) >> 8; // * 257 / 256
+ if ( a > 0xffff) return 0xffff;
+ return (cmsUInt16Number) a;
+}
+
+// * 0xf00 / 0xffff = * 256 / 257
+cmsINLINE cmsUInt16Number FomLabV4ToLabV2(cmsUInt16Number x)
+{
+ return (cmsUInt16Number) (((x << 8) + 0x80) / 257);
+}
+
+
+typedef struct {
+ cmsUInt32Number Type;
+ cmsUInt32Number Mask;
+ cmsFormatter16 Frm;
+
+} cmsFormatters16;
+
+typedef struct {
+ cmsUInt32Number Type;
+ cmsUInt32Number Mask;
+ cmsFormatterFloat Frm;
+
+} cmsFormattersFloat;
+
+
+#define ANYSPACE COLORSPACE_SH(31)
+#define ANYCHANNELS CHANNELS_SH(15)
+#define ANYEXTRA EXTRA_SH(7)
+#define ANYPLANAR PLANAR_SH(1)
+#define ANYENDIAN ENDIAN16_SH(1)
+#define ANYSWAP DOSWAP_SH(1)
+#define ANYSWAPFIRST SWAPFIRST_SH(1)
+#define ANYFLAVOR FLAVOR_SH(1)
+
+
+// Supress waning about info never being used
+
+#ifdef _MSC_VER
+#pragma warning(disable : 4100)
+#endif
+
+// Unpacking routines (16 bits) ----------------------------------------------------------------------------------------
+
+
+// Does almost everything but is slow
+static
+cmsUInt8Number* UnrollChunkyBytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsUInt16Number v;
+ int i;
+
+ if (ExtraFirst) {
+ accum += Extra;
+ }
+
+ for (i=0; i < nChan; i++) {
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = FROM_8_TO_16(*accum);
+ v = Reverse ? REVERSE_FLAVOR_16(v) : v;
+ wIn[index] = v;
+ accum++;
+ }
+
+ if (!ExtraFirst) {
+ accum += Extra;
+ }
+
+ if (Extra == 0 && SwapFirst) {
+ cmsUInt16Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsUInt16Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+
+}
+
+// Extra channels are just ignored because come in the next planes
+static
+cmsUInt8Number* UnrollPlanarBytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int i;
+ cmsUInt8Number* Init = accum;
+
+ if (DoSwap ^ SwapFirst) {
+ accum += T_EXTRA(info -> InputFormat) * Stride;
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+ cmsUInt16Number v = FROM_8_TO_16(*accum);
+
+ wIn[index] = Reverse ? REVERSE_FLAVOR_16(v) : v;
+ accum += Stride;
+ }
+
+ return (Init + 1);
+}
+
+// Special cases, provided for performance
+static
+cmsUInt8Number* Unroll4Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // C
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // M
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // Y
+ wIn[3] = FROM_8_TO_16(*accum); accum++; // K
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4BytesReverse(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FROM_8_TO_16(REVERSE_FLAVOR_8(*accum)); accum++; // C
+ wIn[1] = FROM_8_TO_16(REVERSE_FLAVOR_8(*accum)); accum++; // M
+ wIn[2] = FROM_8_TO_16(REVERSE_FLAVOR_8(*accum)); accum++; // Y
+ wIn[3] = FROM_8_TO_16(REVERSE_FLAVOR_8(*accum)); accum++; // K
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4BytesSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[3] = FROM_8_TO_16(*accum); accum++; // K
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // C
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // M
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // Y
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// KYMC
+static
+cmsUInt8Number* Unroll4BytesSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[3] = FROM_8_TO_16(*accum); accum++; // K
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // Y
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // M
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // C
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4BytesSwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // K
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // Y
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // M
+ wIn[3] = FROM_8_TO_16(*accum); accum++; // C
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // R
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // G
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3BytesSkip1Swap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ accum++; // A
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // B
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // G
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // R
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3BytesSkip1SwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // B
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // G
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // R
+ accum++; // A
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3BytesSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ accum++; // A
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // R
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // G
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+// BRG
+static
+cmsUInt8Number* Unroll3BytesSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[2] = FROM_8_TO_16(*accum); accum++; // B
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // G
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // R
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* UnrollLabV2_8(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // L
+ wIn[1] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // a
+ wIn[2] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // b
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* UnrollALabV2_8(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ accum++; // A
+ wIn[0] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // L
+ wIn[1] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // a
+ wIn[2] = FomLabV2ToLabV4(FROM_8_TO_16(*accum)); accum++; // b
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* UnrollLabV2_16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FomLabV2ToLabV4(*(cmsUInt16Number*) accum); accum += 2; // L
+ wIn[1] = FomLabV2ToLabV4(*(cmsUInt16Number*) accum); accum += 2; // a
+ wIn[2] = FomLabV2ToLabV4(*(cmsUInt16Number*) accum); accum += 2; // b
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// for duplex
+static
+cmsUInt8Number* Unroll2Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = FROM_8_TO_16(*accum); accum++; // ch1
+ wIn[1] = FROM_8_TO_16(*accum); accum++; // ch2
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+
+
+// Monochrome duplicates L into RGB for null-transforms
+static
+cmsUInt8Number* Unroll1Byte(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = FROM_8_TO_16(*accum); accum++; // L
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Unroll1ByteSkip1(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = FROM_8_TO_16(*accum); accum++; // L
+ accum += 1;
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll1ByteSkip2(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = FROM_8_TO_16(*accum); accum++; // L
+ accum += 2;
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll1ByteReversed(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = REVERSE_FLAVOR_16(FROM_8_TO_16(*accum)); accum++; // L
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* UnrollAnyWords(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int SwapEndian = T_ENDIAN16(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int i;
+
+ if (ExtraFirst) {
+ accum += Extra * sizeof(cmsUInt16Number);
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+ cmsUInt16Number v = *(cmsUInt16Number*) accum;
+
+ if (SwapEndian)
+ v = CHANGE_ENDIAN(v);
+
+ wIn[index] = Reverse ? REVERSE_FLAVOR_16(v) : v;
+
+ accum += sizeof(cmsUInt16Number);
+ }
+
+ if (!ExtraFirst) {
+ accum += Extra * sizeof(cmsUInt16Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ cmsUInt16Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsUInt16Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* UnrollPlanarWords(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap= T_DOSWAP(info ->InputFormat);
+ int Reverse= T_FLAVOR(info ->InputFormat);
+ int SwapEndian = T_ENDIAN16(info -> InputFormat);
+ int i;
+ cmsUInt8Number* Init = accum;
+
+ if (DoSwap) {
+ accum += T_EXTRA(info -> InputFormat) * Stride * sizeof(cmsUInt16Number);
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+ cmsUInt16Number v = *(cmsUInt16Number*) accum;
+
+ if (SwapEndian)
+ v = CHANGE_ENDIAN(v);
+
+ wIn[index] = Reverse ? REVERSE_FLAVOR_16(v) : v;
+
+ accum += Stride * sizeof(cmsUInt16Number);
+ }
+
+ return (Init + sizeof(cmsUInt16Number));
+}
+
+
+static
+cmsUInt8Number* Unroll4Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // C
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // M
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // Y
+ wIn[3] = *(cmsUInt16Number*) accum; accum+= 2; // K
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4WordsReverse(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = REVERSE_FLAVOR_16(*(cmsUInt16Number*) accum); accum+= 2; // C
+ wIn[1] = REVERSE_FLAVOR_16(*(cmsUInt16Number*) accum); accum+= 2; // M
+ wIn[2] = REVERSE_FLAVOR_16(*(cmsUInt16Number*) accum); accum+= 2; // Y
+ wIn[3] = REVERSE_FLAVOR_16(*(cmsUInt16Number*) accum); accum+= 2; // K
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4WordsSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[3] = *(cmsUInt16Number*) accum; accum+= 2; // K
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // C
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // M
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // Y
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// KYMC
+static
+cmsUInt8Number* Unroll4WordsSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[3] = *(cmsUInt16Number*) accum; accum+= 2; // K
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // Y
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // M
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // C
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll4WordsSwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // K
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // Y
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // M
+ wIn[3] = *(cmsUInt16Number*) accum; accum+= 2; // C
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // C R
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // M G
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // Y B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3WordsSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // C R
+ wIn[1] = *(cmsUInt16Number*) accum; accum+= 2; // M G
+ wIn[0] = *(cmsUInt16Number*) accum; accum+= 2; // Y B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3WordsSkip1Swap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ accum += 2; // A
+ wIn[2] = *(cmsUInt16Number*) accum; accum += 2; // R
+ wIn[1] = *(cmsUInt16Number*) accum; accum += 2; // G
+ wIn[0] = *(cmsUInt16Number*) accum; accum += 2; // B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll3WordsSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ accum += 2; // A
+ wIn[0] = *(cmsUInt16Number*) accum; accum += 2; // R
+ wIn[1] = *(cmsUInt16Number*) accum; accum += 2; // G
+ wIn[2] = *(cmsUInt16Number*) accum; accum += 2; // B
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll1Word(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = *(cmsUInt16Number*) accum; accum+= 2; // L
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll1WordReversed(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = REVERSE_FLAVOR_16(*(cmsUInt16Number*) accum); accum+= 2;
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll1WordSkip3(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = wIn[1] = wIn[2] = *(cmsUInt16Number*) accum;
+
+ accum += 8;
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Unroll2Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ wIn[0] = *(cmsUInt16Number*) accum; accum += 2; // ch1
+ wIn[1] = *(cmsUInt16Number*) accum; accum += 2; // ch2
+
+ return accum;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+// This is a conversion of Lab double to 16 bits
+static
+cmsUInt8Number* UnrollLabDoubleTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ if (T_PLANAR(info -> InputFormat)) {
+
+ cmsFloat64Number* Pt = (cmsFloat64Number*) accum;
+
+ cmsCIELab Lab;
+
+ Lab.L = Pt[0];
+ Lab.a = Pt[Stride];
+ Lab.b = Pt[Stride*2];
+
+ cmsFloat2LabEncoded(wIn, &Lab);
+ return accum + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ cmsFloat2LabEncoded(wIn, (cmsCIELab*) accum);
+ accum += sizeof(cmsCIELab) + T_EXTRA(info ->InputFormat) * sizeof(cmsFloat64Number);
+ return accum;
+ }
+}
+
+
+// This is a conversion of Lab float to 16 bits
+static
+cmsUInt8Number* UnrollLabFloatTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ cmsCIELab Lab;
+
+ if (T_PLANAR(info -> InputFormat)) {
+
+ cmsFloat32Number* Pt = (cmsFloat32Number*) accum;
+
+
+ Lab.L = Pt[0];
+ Lab.a = Pt[Stride];
+ Lab.b = Pt[Stride*2];
+
+ cmsFloat2LabEncoded(wIn, &Lab);
+ return accum + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ Lab.L = ((cmsFloat32Number*) accum)[0];
+ Lab.a = ((cmsFloat32Number*) accum)[1];
+ Lab.b = ((cmsFloat32Number*) accum)[2];
+
+ cmsFloat2LabEncoded(wIn, &Lab);
+ accum += (3 + T_EXTRA(info ->InputFormat)) * sizeof(cmsFloat32Number);
+ return accum;
+ }
+}
+
+// This is a conversion of XYZ double to 16 bits
+static
+cmsUInt8Number* UnrollXYZDoubleTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ if (T_PLANAR(info -> InputFormat)) {
+
+ cmsFloat64Number* Pt = (cmsFloat64Number*) accum;
+ cmsCIEXYZ XYZ;
+
+ XYZ.X = Pt[0];
+ XYZ.Y = Pt[Stride];
+ XYZ.Z = Pt[Stride*2];
+ cmsFloat2XYZEncoded(wIn, &XYZ);
+
+ return accum + sizeof(cmsFloat64Number);
+
+ }
+
+ else {
+ cmsFloat2XYZEncoded(wIn, (cmsCIEXYZ*) accum);
+ accum += sizeof(cmsCIEXYZ) + T_EXTRA(info ->InputFormat) * sizeof(cmsFloat64Number);
+
+ return accum;
+ }
+}
+
+// This is a conversion of XYZ float to 16 bits
+static
+cmsUInt8Number* UnrollXYZFloatTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ if (T_PLANAR(info -> InputFormat)) {
+
+ cmsFloat32Number* Pt = (cmsFloat32Number*) accum;
+ cmsCIEXYZ XYZ;
+
+ XYZ.X = Pt[0];
+ XYZ.Y = Pt[Stride];
+ XYZ.Z = Pt[Stride*2];
+ cmsFloat2XYZEncoded(wIn, &XYZ);
+
+ return accum + sizeof(cmsFloat32Number);
+
+ }
+
+ else {
+ cmsFloat32Number* Pt = (cmsFloat32Number*) accum;
+ cmsCIEXYZ XYZ;
+
+ XYZ.X = Pt[0];
+ XYZ.Y = Pt[1];
+ XYZ.Z = Pt[2];
+ cmsFloat2XYZEncoded(wIn, &XYZ);
+
+ accum += 3 * sizeof(cmsFloat32Number) + T_EXTRA(info ->InputFormat) * sizeof(cmsFloat32Number);
+
+ return accum;
+ }
+}
+
+// Check if space is marked as ink
+cmsINLINE cmsBool IsInkSpace(cmsUInt32Number Type)
+{
+ switch (T_COLORSPACE(Type)) {
+
+ case PT_CMY:
+ case PT_CMYK:
+ case PT_MCH5:
+ case PT_MCH6:
+ case PT_MCH7:
+ case PT_MCH8:
+ case PT_MCH9:
+ case PT_MCH10:
+ case PT_MCH11:
+ case PT_MCH12:
+ case PT_MCH13:
+ case PT_MCH14:
+ case PT_MCH15: return TRUE;
+
+ default: return FALSE;
+ }
+}
+
+// Inks does come in percentage, remaining cases are between 0..1.0, again to 16 bits
+static
+cmsUInt8Number* UnrollDoubleTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat64Number v;
+ cmsUInt16Number vi;
+ int i, start = 0;
+ cmsFloat64Number maximum = IsInkSpace(info ->InputFormat) ? 655.35 : 65535.0;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = (cmsFloat32Number) ((cmsFloat64Number*) accum)[(i + start) * Stride];
+ else
+ v = (cmsFloat32Number) ((cmsFloat64Number*) accum)[i + start];
+
+ vi = _cmsQuickSaturateWord(v * maximum);
+
+ if (Reverse)
+ vi = REVERSE_FLAVOR_16(vi);
+
+ wIn[index] = vi;
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsUInt16Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsUInt16Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsFloat64Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsFloat64Number);
+}
+
+
+
+static
+cmsUInt8Number* UnrollFloatTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat32Number v;
+ cmsUInt16Number vi;
+ int i, start = 0;
+ cmsFloat64Number maximum = IsInkSpace(info ->InputFormat) ? 655.35 : 65535.0;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = (cmsFloat32Number) ((cmsFloat32Number*) accum)[(i + start) * Stride];
+ else
+ v = (cmsFloat32Number) ((cmsFloat32Number*) accum)[i + start];
+
+ vi = _cmsQuickSaturateWord(v * maximum);
+
+ if (Reverse)
+ vi = REVERSE_FLAVOR_16(vi);
+
+ wIn[index] = vi;
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsUInt16Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsUInt16Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsFloat32Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsFloat32Number);
+}
+
+
+
+
+// For 1 channel, we need to duplicate data (it comes in 0..1.0 range)
+static
+cmsUInt8Number* UnrollDouble1Chan(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+ cmsFloat64Number* Inks = (cmsFloat64Number*) accum;
+
+ wIn[0] = wIn[1] = wIn[2] = _cmsQuickSaturateWord(Inks[0] * 65535.0);
+
+ return accum + sizeof(cmsFloat64Number);
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+//-------------------------------------------------------------------------------------------------------------------
+
+// For anything going from cmsFloat32Number
+static
+cmsUInt8Number* UnrollFloatsToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat32Number v;
+ int i, start = 0;
+ cmsFloat32Number maximum = IsInkSpace(info ->InputFormat) ? 100.0F : 1.0F;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = (cmsFloat32Number) ((cmsFloat32Number*) accum)[(i + start) * Stride];
+ else
+ v = (cmsFloat32Number) ((cmsFloat32Number*) accum)[i + start];
+
+ v /= maximum;
+
+ wIn[index] = Reverse ? 1 - v : v;
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsFloat32Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsFloat32Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsFloat32Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsFloat32Number);
+}
+
+// For anything going from double
+
+static
+cmsUInt8Number* UnrollDoublesToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat64Number v;
+ int i, start = 0;
+ cmsFloat64Number maximum = IsInkSpace(info ->InputFormat) ? 100.0 : 1.0;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = (cmsFloat64Number) ((cmsFloat64Number*) accum)[(i + start) * Stride];
+ else
+ v = (cmsFloat64Number) ((cmsFloat64Number*) accum)[i + start];
+
+ v /= maximum;
+
+ wIn[index] = (cmsFloat32Number) (Reverse ? 1.0 - v : v);
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsFloat32Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsFloat32Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsFloat64Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsFloat64Number);
+}
+
+
+
+// From Lab double to cmsFloat32Number
+static
+cmsUInt8Number* UnrollLabDoubleToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+ cmsFloat64Number* Pt = (cmsFloat64Number*) accum;
+
+ if (T_PLANAR(info -> InputFormat)) {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / 100.0); // from 0..100 to 0..1
+ wIn[1] = (cmsFloat32Number) ((Pt[Stride] + 128) / 255.0); // form -128..+127 to 0..1
+ wIn[2] = (cmsFloat32Number) ((Pt[Stride*2] + 128) / 255.0);
+
+ return accum + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / 100.0); // from 0..100 to 0..1
+ wIn[1] = (cmsFloat32Number) ((Pt[1] + 128) / 255.0); // form -128..+127 to 0..1
+ wIn[2] = (cmsFloat32Number) ((Pt[2] + 128) / 255.0);
+
+ accum += sizeof(cmsFloat64Number)*(3 + T_EXTRA(info ->InputFormat));
+ return accum;
+ }
+}
+
+// From Lab double to cmsFloat32Number
+static
+cmsUInt8Number* UnrollLabFloatToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+ cmsFloat32Number* Pt = (cmsFloat32Number*) accum;
+
+ if (T_PLANAR(info -> InputFormat)) {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / 100.0); // from 0..100 to 0..1
+ wIn[1] = (cmsFloat32Number) ((Pt[Stride] + 128) / 255.0); // form -128..+127 to 0..1
+ wIn[2] = (cmsFloat32Number) ((Pt[Stride*2] + 128) / 255.0);
+
+ return accum + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / 100.0); // from 0..100 to 0..1
+ wIn[1] = (cmsFloat32Number) ((Pt[1] + 128) / 255.0); // form -128..+127 to 0..1
+ wIn[2] = (cmsFloat32Number) ((Pt[2] + 128) / 255.0);
+
+ accum += sizeof(cmsFloat32Number)*(3 + T_EXTRA(info ->InputFormat));
+ return accum;
+ }
+}
+
+
+
+// 1.15 fixed point, that means maximum value is MAX_ENCODEABLE_XYZ (0xFFFF)
+static
+cmsUInt8Number* UnrollXYZDoubleToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+ cmsFloat64Number* Pt = (cmsFloat64Number*) accum;
+
+ if (T_PLANAR(info -> InputFormat)) {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / MAX_ENCODEABLE_XYZ);
+ wIn[1] = (cmsFloat32Number) (Pt[Stride] / MAX_ENCODEABLE_XYZ);
+ wIn[2] = (cmsFloat32Number) (Pt[Stride*2] / MAX_ENCODEABLE_XYZ);
+
+ return accum + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / MAX_ENCODEABLE_XYZ);
+ wIn[1] = (cmsFloat32Number) (Pt[1] / MAX_ENCODEABLE_XYZ);
+ wIn[2] = (cmsFloat32Number) (Pt[2] / MAX_ENCODEABLE_XYZ);
+
+ accum += sizeof(cmsFloat64Number)*(3 + T_EXTRA(info ->InputFormat));
+ return accum;
+ }
+}
+
+static
+cmsUInt8Number* UnrollXYZFloatToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+ cmsFloat32Number* Pt = (cmsFloat32Number*) accum;
+
+ if (T_PLANAR(info -> InputFormat)) {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / MAX_ENCODEABLE_XYZ);
+ wIn[1] = (cmsFloat32Number) (Pt[Stride] / MAX_ENCODEABLE_XYZ);
+ wIn[2] = (cmsFloat32Number) (Pt[Stride*2] / MAX_ENCODEABLE_XYZ);
+
+ return accum + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ wIn[0] = (cmsFloat32Number) (Pt[0] / MAX_ENCODEABLE_XYZ);
+ wIn[1] = (cmsFloat32Number) (Pt[1] / MAX_ENCODEABLE_XYZ);
+ wIn[2] = (cmsFloat32Number) (Pt[2] / MAX_ENCODEABLE_XYZ);
+
+ accum += sizeof(cmsFloat32Number)*(3 + T_EXTRA(info ->InputFormat));
+ return accum;
+ }
+}
+
+
+
+// Packing routines -----------------------------------------------------------------------------------------------------------
+
+
+// Generic chunky for byte
+
+static
+cmsUInt8Number* PackAnyBytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsUInt8Number* swap1;
+ cmsUInt8Number v = 0;
+ int i;
+
+ swap1 = output;
+
+ if (ExtraFirst) {
+ output += Extra;
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = FROM_16_TO_8(wOut[index]);
+
+ if (Reverse)
+ v = REVERSE_FLAVOR_8(v);
+
+ *output++ = v;
+ }
+
+ if (!ExtraFirst) {
+ output += Extra;
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, nChan-1);
+ *swap1 = v;
+ }
+
+
+ return output;
+
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+
+static
+cmsUInt8Number* PackAnyWords(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int SwapEndian = T_ENDIAN16(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsUInt16Number* swap1;
+ cmsUInt16Number v = 0;
+ int i;
+
+ swap1 = (cmsUInt16Number*) output;
+
+ if (ExtraFirst) {
+ output += Extra * sizeof(cmsUInt16Number);
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = wOut[index];
+
+ if (SwapEndian)
+ v = CHANGE_ENDIAN(v);
+
+ if (Reverse)
+ v = REVERSE_FLAVOR_16(v);
+
+ *(cmsUInt16Number*) output = v;
+
+ output += sizeof(cmsUInt16Number);
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsUInt16Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsUInt16Number));
+ *swap1 = v;
+ }
+
+
+ return output;
+
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* PackPlanarBytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int i;
+ cmsUInt8Number* Init = output;
+
+
+ if (DoSwap ^ SwapFirst) {
+ output += T_EXTRA(info -> OutputFormat) * Stride;
+ }
+
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+ cmsUInt8Number v = FROM_16_TO_8(wOut[index]);
+
+ *(cmsUInt8Number*) output = (cmsUInt8Number) (Reverse ? REVERSE_FLAVOR_8(v) : v);
+ output += Stride;
+ }
+
+ return (Init + 1);
+
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* PackPlanarWords(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse= T_FLAVOR(info ->OutputFormat);
+ int SwapEndian = T_ENDIAN16(info -> OutputFormat);
+ int i;
+ cmsUInt8Number* Init = output;
+ cmsUInt16Number v;
+
+ if (DoSwap) {
+ output += T_EXTRA(info -> OutputFormat) * Stride * sizeof(cmsUInt16Number);
+ }
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = wOut[index];
+
+ if (SwapEndian)
+ v = CHANGE_ENDIAN(v);
+
+ if (Reverse)
+ v = REVERSE_FLAVOR_16(v);
+
+ *(cmsUInt16Number*) output = v;
+ output += (Stride * sizeof(cmsUInt16Number));
+ }
+
+ return (Init + sizeof(cmsUInt16Number));
+}
+
+// CMYKcm (unrolled for speed)
+
+static
+cmsUInt8Number* Pack6Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[3]);
+ *output++ = FROM_16_TO_8(wOut[4]);
+ *output++ = FROM_16_TO_8(wOut[5]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// KCMYcm
+
+static
+cmsUInt8Number* Pack6BytesSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[5]);
+ *output++ = FROM_16_TO_8(wOut[4]);
+ *output++ = FROM_16_TO_8(wOut[3]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// CMYKcm
+static
+cmsUInt8Number* Pack6Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[3];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[4];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[5];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// KCMYcm
+static
+cmsUInt8Number* Pack6WordsSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[5];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[4];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[3];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack4Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[3]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack4BytesReverse(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = REVERSE_FLAVOR_8(FROM_16_TO_8(wOut[0]));
+ *output++ = REVERSE_FLAVOR_8(FROM_16_TO_8(wOut[1]));
+ *output++ = REVERSE_FLAVOR_8(FROM_16_TO_8(wOut[2]));
+ *output++ = REVERSE_FLAVOR_8(FROM_16_TO_8(wOut[3]));
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack4BytesSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[3]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// ABGR
+static
+cmsUInt8Number* Pack4BytesSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[3]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack4BytesSwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[3]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack4Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[3];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack4WordsReverse(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = REVERSE_FLAVOR_16(wOut[0]);
+ output+= 2;
+ *(cmsUInt16Number*) output = REVERSE_FLAVOR_16(wOut[1]);
+ output+= 2;
+ *(cmsUInt16Number*) output = REVERSE_FLAVOR_16(wOut[2]);
+ output+= 2;
+ *(cmsUInt16Number*) output = REVERSE_FLAVOR_16(wOut[3]);
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// ABGR
+static
+cmsUInt8Number* Pack4WordsSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[3];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+// CMYK
+static
+cmsUInt8Number* Pack4WordsBigEndian(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[0]);
+ output+= 2;
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[1]);
+ output+= 2;
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[2]);
+ output+= 2;
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[3]);
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* PackLabV2_8(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[0]));
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[1]));
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[2]));
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* PackALabV2_8(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[0]));
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[1]));
+ *output++ = FROM_16_TO_8(FomLabV4ToLabV2(wOut[2]));
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* PackLabV2_16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = FomLabV4ToLabV2(wOut[0]);
+ output += 2;
+ *(cmsUInt16Number*) output = FomLabV4ToLabV2(wOut[1]);
+ output += 2;
+ *(cmsUInt16Number*) output = FomLabV4ToLabV2(wOut[2]);
+ output += 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3Bytes(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesOptimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = (wOut[0] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[2] & 0xFF);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesSwapOptimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = (wOut[2] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[0] & 0xFF);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack3Words(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3WordsSwap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3WordsBigEndian(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[0]);
+ output+= 2;
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[1]);
+ output+= 2;
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[2]);
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+ output++;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1Optimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = (wOut[0] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[2] & 0xFF);
+ output++;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = FROM_16_TO_8(wOut[0]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[2]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1SwapFirstOptimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = (wOut[0] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[2] & 0xFF);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1Swap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1SwapOptimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = (wOut[2] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[0] & 0xFF);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1SwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[2]);
+ *output++ = FROM_16_TO_8(wOut[1]);
+ *output++ = FROM_16_TO_8(wOut[0]);
+ output++;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3BytesAndSkip1SwapSwapFirstOptimized(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = (wOut[2] & 0xFF);
+ *output++ = (wOut[1] & 0xFF);
+ *output++ = (wOut[0] & 0xFF);
+ output++;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3WordsAndSkip1(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack3WordsAndSkip1Swap(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack3WordsAndSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack3WordsAndSkip1SwapSwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[2];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[1];
+ output+= 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+
+static
+cmsUInt8Number* Pack1Byte(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack1ByteReversed(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(REVERSE_FLAVOR_16(wOut[0]));
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack1ByteSkip1(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *output++ = FROM_16_TO_8(wOut[0]);
+ output++;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack1ByteSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output++;
+ *output++ = FROM_16_TO_8(wOut[0]);
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack1Word(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack1WordReversed(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = REVERSE_FLAVOR_16(wOut[0]);
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack1WordBigEndian(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = CHANGE_ENDIAN(wOut[0]);
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+static
+cmsUInt8Number* Pack1WordSkip1(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 4;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+static
+cmsUInt8Number* Pack1WordSkip1SwapFirst(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ output += 2;
+ *(cmsUInt16Number*) output = wOut[0];
+ output+= 2;
+
+ return output;
+
+ cmsUNUSED_PARAMETER(info);
+ cmsUNUSED_PARAMETER(Stride);
+}
+
+
+// Unencoded Float values -- don't try optimize speed
+static
+cmsUInt8Number* PackLabDoubleFrom16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+
+ if (T_PLANAR(info -> OutputFormat)) {
+
+ cmsCIELab Lab;
+ cmsFloat64Number* Out = (cmsFloat64Number*) output;
+ cmsLabEncoded2Float(&Lab, wOut);
+
+ Out[0] = Lab.L;
+ Out[Stride] = Lab.a;
+ Out[Stride*2] = Lab.b;
+
+ return output + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ cmsLabEncoded2Float((cmsCIELab*) output, wOut);
+ return output + (sizeof(cmsCIELab) + T_EXTRA(info ->OutputFormat) * sizeof(cmsFloat64Number));
+ }
+}
+
+
+static
+cmsUInt8Number* PackLabFloatFrom16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ cmsCIELab Lab;
+ cmsLabEncoded2Float(&Lab, wOut);
+
+ if (T_PLANAR(info -> OutputFormat)) {
+
+ cmsFloat32Number* Out = (cmsFloat32Number*) output;
+
+ Out[0] = (cmsFloat32Number)Lab.L;
+ Out[Stride] = (cmsFloat32Number)Lab.a;
+ Out[Stride*2] = (cmsFloat32Number)Lab.b;
+
+ return output + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ ((cmsFloat32Number*) output)[0] = (cmsFloat32Number) Lab.L;
+ ((cmsFloat32Number*) output)[1] = (cmsFloat32Number) Lab.a;
+ ((cmsFloat32Number*) output)[2] = (cmsFloat32Number) Lab.b;
+
+ return output + (3 + T_EXTRA(info ->OutputFormat)) * sizeof(cmsFloat32Number);
+ }
+}
+
+static
+cmsUInt8Number* PackXYZDoubleFrom16(register _cmsTRANSFORM* Info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ cmsCIEXYZ XYZ;
+ cmsFloat64Number* Out = (cmsFloat64Number*) output;
+ cmsXYZEncoded2Float(&XYZ, wOut);
+
+ Out[0] = XYZ.X;
+ Out[Stride] = XYZ.Y;
+ Out[Stride*2] = XYZ.Z;
+
+ return output + sizeof(cmsFloat64Number);
+
+ }
+ else {
+
+ cmsXYZEncoded2Float((cmsCIEXYZ*) output, wOut);
+
+ return output + (sizeof(cmsCIEXYZ) + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat64Number));
+ }
+}
+
+static
+cmsUInt8Number* PackXYZFloatFrom16(register _cmsTRANSFORM* Info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ cmsCIEXYZ XYZ;
+ cmsFloat32Number* Out = (cmsFloat32Number*) output;
+ cmsXYZEncoded2Float(&XYZ, wOut);
+
+ Out[0] = (cmsFloat32Number) XYZ.X;
+ Out[Stride] = (cmsFloat32Number) XYZ.Y;
+ Out[Stride*2] = (cmsFloat32Number) XYZ.Z;
+
+ return output + sizeof(cmsFloat32Number);
+
+ }
+ else {
+
+ cmsCIEXYZ XYZ;
+ cmsFloat32Number* Out = (cmsFloat32Number*) output;
+ cmsXYZEncoded2Float(&XYZ, wOut);
+
+ Out[0] = (cmsFloat32Number) XYZ.X;
+ Out[1] = (cmsFloat32Number) XYZ.Y;
+ Out[2] = (cmsFloat32Number) XYZ.Z;
+
+ return output + (3 * sizeof(cmsFloat32Number) + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat32Number));
+ }
+}
+
+static
+cmsUInt8Number* PackDoubleFrom16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat64Number maximum = IsInkSpace(info ->OutputFormat) ? 655.35 : 65535.0;
+ cmsFloat64Number v = 0;
+ cmsFloat64Number* swap1 = (cmsFloat64Number*) output;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = (cmsFloat64Number) wOut[index] / maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsFloat64Number*) output)[(i + start) * Stride]= v;
+ else
+ ((cmsFloat64Number*) output)[i + start] = v;
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsFloat64Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsFloat64Number));
+ *swap1 = v;
+ }
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsFloat64Number);
+ else
+ return output + nChan * sizeof(cmsFloat64Number);
+
+}
+
+
+static
+cmsUInt8Number* PackFloatFrom16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat64Number maximum = IsInkSpace(info ->OutputFormat) ? 655.35 : 65535.0;
+ cmsFloat64Number v = 0;
+ cmsFloat32Number* swap1 = (cmsFloat32Number*) output;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = (cmsFloat64Number) wOut[index] / maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsFloat32Number*) output)[(i + start ) * Stride]= (cmsFloat32Number) v;
+ else
+ ((cmsFloat32Number*) output)[i + start] = (cmsFloat32Number) v;
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsFloat32Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsFloat32Number));
+ *swap1 = (cmsFloat32Number) v;
+ }
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsFloat32Number);
+ else
+ return output + nChan * sizeof(cmsFloat32Number);
+}
+
+
+
+// --------------------------------------------------------------------------------------------------------
+
+static
+cmsUInt8Number* PackFloatsFromFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat64Number maximum = IsInkSpace(info ->OutputFormat) ? 100.0 : 1.0;
+ cmsFloat32Number* swap1 = (cmsFloat32Number*) output;
+ cmsFloat64Number v = 0;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = wOut[index] * maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsFloat32Number*) output)[(i + start)* Stride]= (cmsFloat32Number) v;
+ else
+ ((cmsFloat32Number*) output)[i + start] = (cmsFloat32Number) v;
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsFloat32Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsFloat32Number));
+ *swap1 = (cmsFloat32Number) v;
+ }
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsFloat32Number);
+ else
+ return output + nChan * sizeof(cmsFloat32Number);
+}
+
+static
+cmsUInt8Number* PackDoublesFromFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat64Number maximum = IsInkSpace(info ->OutputFormat) ? 100.0 : 1.0;
+ cmsFloat64Number v = 0;
+ cmsFloat64Number* swap1 = (cmsFloat64Number*) output;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = wOut[index] * maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsFloat64Number*) output)[(i + start) * Stride] = v;
+ else
+ ((cmsFloat64Number*) output)[i + start] = v;
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsFloat64Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsFloat64Number));
+ *swap1 = v;
+ }
+
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsFloat64Number);
+ else
+ return output + nChan * sizeof(cmsFloat64Number);
+
+}
+
+
+
+
+
+static
+cmsUInt8Number* PackLabFloatFromFloat(_cmsTRANSFORM* Info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ cmsFloat32Number* Out = (cmsFloat32Number*) output;
+
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ Out[0] = (cmsFloat32Number) (wOut[0] * 100.0);
+ Out[Stride] = (cmsFloat32Number) (wOut[1] * 255.0 - 128.0);
+ Out[Stride*2] = (cmsFloat32Number) (wOut[2] * 255.0 - 128.0);
+
+ return output + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ Out[0] = (cmsFloat32Number) (wOut[0] * 100.0);
+ Out[1] = (cmsFloat32Number) (wOut[1] * 255.0 - 128.0);
+ Out[2] = (cmsFloat32Number) (wOut[2] * 255.0 - 128.0);
+
+ return output + (sizeof(cmsFloat32Number)*3 + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat32Number));
+ }
+
+}
+
+
+static
+cmsUInt8Number* PackLabDoubleFromFloat(_cmsTRANSFORM* Info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ cmsFloat64Number* Out = (cmsFloat64Number*) output;
+
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ Out[0] = (cmsFloat64Number) (wOut[0] * 100.0);
+ Out[Stride] = (cmsFloat64Number) (wOut[1] * 255.0 - 128.0);
+ Out[Stride*2] = (cmsFloat64Number) (wOut[2] * 255.0 - 128.0);
+
+ return output + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ Out[0] = (cmsFloat64Number) (wOut[0] * 100.0);
+ Out[1] = (cmsFloat64Number) (wOut[1] * 255.0 - 128.0);
+ Out[2] = (cmsFloat64Number) (wOut[2] * 255.0 - 128.0);
+
+ return output + (sizeof(cmsFloat64Number)*3 + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat64Number));
+ }
+
+}
+
+
+// From 0..1 range to 0..MAX_ENCODEABLE_XYZ
+static
+cmsUInt8Number* PackXYZFloatFromFloat(_cmsTRANSFORM* Info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ cmsFloat32Number* Out = (cmsFloat32Number*) output;
+
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ Out[0] = (cmsFloat32Number) (wOut[0] * MAX_ENCODEABLE_XYZ);
+ Out[Stride] = (cmsFloat32Number) (wOut[1] * MAX_ENCODEABLE_XYZ);
+ Out[Stride*2] = (cmsFloat32Number) (wOut[2] * MAX_ENCODEABLE_XYZ);
+
+ return output + sizeof(cmsFloat32Number);
+ }
+ else {
+
+ Out[0] = (cmsFloat32Number) (wOut[0] * MAX_ENCODEABLE_XYZ);
+ Out[1] = (cmsFloat32Number) (wOut[1] * MAX_ENCODEABLE_XYZ);
+ Out[2] = (cmsFloat32Number) (wOut[2] * MAX_ENCODEABLE_XYZ);
+
+ return output + (sizeof(cmsFloat32Number)*3 + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat32Number));
+ }
+
+}
+
+// Same, but convert to double
+static
+cmsUInt8Number* PackXYZDoubleFromFloat(_cmsTRANSFORM* Info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ cmsFloat64Number* Out = (cmsFloat64Number*) output;
+
+ if (T_PLANAR(Info -> OutputFormat)) {
+
+ Out[0] = (cmsFloat64Number) (wOut[0] * MAX_ENCODEABLE_XYZ);
+ Out[Stride] = (cmsFloat64Number) (wOut[1] * MAX_ENCODEABLE_XYZ);
+ Out[Stride*2] = (cmsFloat64Number) (wOut[2] * MAX_ENCODEABLE_XYZ);
+
+ return output + sizeof(cmsFloat64Number);
+ }
+ else {
+
+ Out[0] = (cmsFloat64Number) (wOut[0] * MAX_ENCODEABLE_XYZ);
+ Out[1] = (cmsFloat64Number) (wOut[1] * MAX_ENCODEABLE_XYZ);
+ Out[2] = (cmsFloat64Number) (wOut[2] * MAX_ENCODEABLE_XYZ);
+
+ return output + (sizeof(cmsFloat64Number)*3 + T_EXTRA(Info ->OutputFormat) * sizeof(cmsFloat64Number));
+ }
+
+}
+
+
+// ----------------------------------------------------------------------------------------------------------------
+
+#ifndef CMS_NO_HALF_SUPPORT
+
+// Decodes an stream of half floats to wIn[] described by input format
+
+static
+cmsUInt8Number* UnrollHalfTo16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wIn[],
+ register cmsUInt8Number* accum,
+ register cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat32Number v;
+ int i, start = 0;
+ cmsFloat32Number maximum = IsInkSpace(info ->InputFormat) ? 655.35F : 65535.0F;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = _cmsHalf2Float ( ((cmsUInt16Number*) accum)[(i + start) * Stride] );
+ else
+ v = _cmsHalf2Float ( ((cmsUInt16Number*) accum)[i + start] ) ;
+
+ if (Reverse) v = maximum - v;
+
+ wIn[index] = _cmsQuickSaturateWord(v * maximum);
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsUInt16Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsUInt16Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsUInt16Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsUInt16Number);
+}
+
+// Decodes an stream of half floats to wIn[] described by input format
+
+static
+cmsUInt8Number* UnrollHalfToFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wIn[],
+ cmsUInt8Number* accum,
+ cmsUInt32Number Stride)
+{
+
+ int nChan = T_CHANNELS(info -> InputFormat);
+ int DoSwap = T_DOSWAP(info ->InputFormat);
+ int Reverse = T_FLAVOR(info ->InputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> InputFormat);
+ int Extra = T_EXTRA(info -> InputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ int Planar = T_PLANAR(info -> InputFormat);
+ cmsFloat32Number v;
+ int i, start = 0;
+ cmsFloat32Number maximum = IsInkSpace(info ->InputFormat) ? 100.0F : 1.0F;
+
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ if (Planar)
+ v = _cmsHalf2Float ( ((cmsUInt16Number*) accum)[(i + start) * Stride] );
+ else
+ v = _cmsHalf2Float ( ((cmsUInt16Number*) accum)[i + start] ) ;
+
+ v /= maximum;
+
+ wIn[index] = Reverse ? 1 - v : v;
+ }
+
+
+ if (Extra == 0 && SwapFirst) {
+ cmsFloat32Number tmp = wIn[0];
+
+ memmove(&wIn[0], &wIn[1], (nChan-1) * sizeof(cmsFloat32Number));
+ wIn[nChan-1] = tmp;
+ }
+
+ if (T_PLANAR(info -> InputFormat))
+ return accum + sizeof(cmsUInt16Number);
+ else
+ return accum + (nChan + Extra) * sizeof(cmsUInt16Number);
+}
+
+
+static
+cmsUInt8Number* PackHalfFrom16(register _cmsTRANSFORM* info,
+ register cmsUInt16Number wOut[],
+ register cmsUInt8Number* output,
+ register cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat32Number maximum = IsInkSpace(info ->OutputFormat) ? 655.35F : 65535.0F;
+ cmsFloat32Number v = 0;
+ cmsUInt16Number* swap1 = (cmsUInt16Number*) output;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = (cmsFloat32Number) wOut[index] / maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsUInt16Number*) output)[(i + start ) * Stride]= _cmsFloat2Half(v);
+ else
+ ((cmsUInt16Number*) output)[i + start] = _cmsFloat2Half(v);
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsUInt16Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsUInt16Number));
+ *swap1 = _cmsFloat2Half(v);
+ }
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsUInt16Number);
+ else
+ return output + nChan * sizeof(cmsUInt16Number);
+}
+
+
+
+static
+cmsUInt8Number* PackHalfFromFloat(_cmsTRANSFORM* info,
+ cmsFloat32Number wOut[],
+ cmsUInt8Number* output,
+ cmsUInt32Number Stride)
+{
+ int nChan = T_CHANNELS(info -> OutputFormat);
+ int DoSwap = T_DOSWAP(info ->OutputFormat);
+ int Reverse = T_FLAVOR(info ->OutputFormat);
+ int Extra = T_EXTRA(info -> OutputFormat);
+ int SwapFirst = T_SWAPFIRST(info -> OutputFormat);
+ int Planar = T_PLANAR(info -> OutputFormat);
+ int ExtraFirst = DoSwap ^ SwapFirst;
+ cmsFloat32Number maximum = IsInkSpace(info ->OutputFormat) ? 100.0F : 1.0F;
+ cmsUInt16Number* swap1 = (cmsUInt16Number*) output;
+ cmsFloat32Number v = 0;
+ int i, start = 0;
+
+ if (ExtraFirst)
+ start = Extra;
+
+ for (i=0; i < nChan; i++) {
+
+ int index = DoSwap ? (nChan - i - 1) : i;
+
+ v = wOut[index] * maximum;
+
+ if (Reverse)
+ v = maximum - v;
+
+ if (Planar)
+ ((cmsUInt16Number*) output)[(i + start)* Stride]= _cmsFloat2Half( v );
+ else
+ ((cmsUInt16Number*) output)[i + start] = _cmsFloat2Half( v );
+ }
+
+ if (!ExtraFirst) {
+ output += Extra * sizeof(cmsUInt16Number);
+ }
+
+ if (Extra == 0 && SwapFirst) {
+
+ memmove(swap1 + 1, swap1, (nChan-1)* sizeof(cmsUInt16Number));
+ *swap1 = (cmsUInt16Number) _cmsFloat2Half( v );
+ }
+
+ if (T_PLANAR(info -> OutputFormat))
+ return output + sizeof(cmsUInt16Number);
+ else
+ return output + nChan * sizeof(cmsUInt16Number);
+}
+
+#endif
+
+// ----------------------------------------------------------------------------------------------------------------
+
+
+static cmsFormatters16 InputFormatters16[] = {
+
+ // Type Mask Function
+ // ---------------------------- ------------------------------------ ----------------------------
+ { TYPE_Lab_DBL, ANYPLANAR|ANYEXTRA, UnrollLabDoubleTo16},
+ { TYPE_XYZ_DBL, ANYPLANAR|ANYEXTRA, UnrollXYZDoubleTo16},
+ { TYPE_Lab_FLT, ANYPLANAR|ANYEXTRA, UnrollLabFloatTo16},
+ { TYPE_XYZ_FLT, ANYPLANAR|ANYEXTRA, UnrollXYZFloatTo16},
+ { TYPE_GRAY_DBL, 0, UnrollDouble1Chan},
+ { FLOAT_SH(1)|BYTES_SH(0), ANYCHANNELS|ANYPLANAR|ANYSWAPFIRST|ANYFLAVOR|
+ ANYSWAP|ANYEXTRA|ANYSPACE, UnrollDoubleTo16},
+ { FLOAT_SH(1)|BYTES_SH(4), ANYCHANNELS|ANYPLANAR|ANYSWAPFIRST|ANYFLAVOR|
+ ANYSWAP|ANYEXTRA|ANYSPACE, UnrollFloatTo16},
+#ifndef CMS_NO_HALF_SUPPORT
+ { FLOAT_SH(1)|BYTES_SH(2), ANYCHANNELS|ANYPLANAR|ANYSWAPFIRST|ANYFLAVOR|
+ ANYEXTRA|ANYSWAP|ANYSPACE, UnrollHalfTo16},
+#endif
+
+ { CHANNELS_SH(1)|BYTES_SH(1), ANYSPACE, Unroll1Byte},
+ { CHANNELS_SH(1)|BYTES_SH(1)|EXTRA_SH(1), ANYSPACE, Unroll1ByteSkip1},
+ { CHANNELS_SH(1)|BYTES_SH(1)|EXTRA_SH(2), ANYSPACE, Unroll1ByteSkip2},
+ { CHANNELS_SH(1)|BYTES_SH(1)|FLAVOR_SH(1), ANYSPACE, Unroll1ByteReversed},
+ { COLORSPACE_SH(PT_MCH2)|CHANNELS_SH(2)|BYTES_SH(1), 0, Unroll2Bytes},
+
+ { TYPE_LabV2_8, 0, UnrollLabV2_8 },
+ { TYPE_ALabV2_8, 0, UnrollALabV2_8 },
+ { TYPE_LabV2_16, 0, UnrollLabV2_16 },
+
+ { CHANNELS_SH(3)|BYTES_SH(1), ANYSPACE, Unroll3Bytes},
+ { CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Unroll3BytesSwap},
+ { CHANNELS_SH(3)|EXTRA_SH(1)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Unroll3BytesSkip1Swap},
+ { CHANNELS_SH(3)|EXTRA_SH(1)|BYTES_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Unroll3BytesSkip1SwapFirst},
+
+ { CHANNELS_SH(3)|EXTRA_SH(1)|BYTES_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1),
+ ANYSPACE, Unroll3BytesSkip1SwapSwapFirst},
+
+ { CHANNELS_SH(4)|BYTES_SH(1), ANYSPACE, Unroll4Bytes},
+ { CHANNELS_SH(4)|BYTES_SH(1)|FLAVOR_SH(1), ANYSPACE, Unroll4BytesReverse},
+ { CHANNELS_SH(4)|BYTES_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Unroll4BytesSwapFirst},
+ { CHANNELS_SH(4)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Unroll4BytesSwap},
+ { CHANNELS_SH(4)|BYTES_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Unroll4BytesSwapSwapFirst},
+
+ { BYTES_SH(1)|PLANAR_SH(1), ANYFLAVOR|ANYSWAPFIRST|
+ ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, UnrollPlanarBytes},
+
+ { BYTES_SH(1), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|
+ ANYEXTRA|ANYCHANNELS|ANYSPACE, UnrollChunkyBytes},
+
+ { CHANNELS_SH(1)|BYTES_SH(2), ANYSPACE, Unroll1Word},
+ { CHANNELS_SH(1)|BYTES_SH(2)|FLAVOR_SH(1), ANYSPACE, Unroll1WordReversed},
+ { CHANNELS_SH(1)|BYTES_SH(2)|EXTRA_SH(3), ANYSPACE, Unroll1WordSkip3},
+
+ { CHANNELS_SH(2)|BYTES_SH(2), ANYSPACE, Unroll2Words},
+ { CHANNELS_SH(3)|BYTES_SH(2), ANYSPACE, Unroll3Words},
+ { CHANNELS_SH(4)|BYTES_SH(2), ANYSPACE, Unroll4Words},
+
+ { CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1), ANYSPACE, Unroll3WordsSwap},
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Unroll3WordsSkip1SwapFirst},
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1)|DOSWAP_SH(1), ANYSPACE, Unroll3WordsSkip1Swap},
+ { CHANNELS_SH(4)|BYTES_SH(2)|FLAVOR_SH(1), ANYSPACE, Unroll4WordsReverse},
+ { CHANNELS_SH(4)|BYTES_SH(2)|SWAPFIRST_SH(1), ANYSPACE, Unroll4WordsSwapFirst},
+ { CHANNELS_SH(4)|BYTES_SH(2)|DOSWAP_SH(1), ANYSPACE, Unroll4WordsSwap},
+ { CHANNELS_SH(4)|BYTES_SH(2)|DOSWAP_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Unroll4WordsSwapSwapFirst},
+
+
+ { BYTES_SH(2)|PLANAR_SH(1), ANYFLAVOR|ANYSWAP|ANYENDIAN|ANYEXTRA|ANYCHANNELS|ANYSPACE, UnrollPlanarWords},
+ { BYTES_SH(2), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYENDIAN|ANYEXTRA|ANYCHANNELS|ANYSPACE, UnrollAnyWords},
+};
+
+
+
+static cmsFormattersFloat InputFormattersFloat[] = {
+
+ // Type Mask Function
+ // ---------------------------- ------------------------------------ ----------------------------
+ { TYPE_Lab_DBL, ANYPLANAR|ANYEXTRA, UnrollLabDoubleToFloat},
+ { TYPE_Lab_FLT, ANYPLANAR|ANYEXTRA, UnrollLabFloatToFloat},
+
+ { TYPE_XYZ_DBL, ANYPLANAR|ANYEXTRA, UnrollXYZDoubleToFloat},
+ { TYPE_XYZ_FLT, ANYPLANAR|ANYEXTRA, UnrollXYZFloatToFloat},
+
+ { FLOAT_SH(1)|BYTES_SH(4), ANYPLANAR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|
+ ANYCHANNELS|ANYSPACE, UnrollFloatsToFloat},
+
+ { FLOAT_SH(1)|BYTES_SH(0), ANYPLANAR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|
+ ANYCHANNELS|ANYSPACE, UnrollDoublesToFloat},
+#ifndef CMS_NO_HALF_SUPPORT
+ { FLOAT_SH(1)|BYTES_SH(2), ANYPLANAR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|
+ ANYCHANNELS|ANYSPACE, UnrollHalfToFloat},
+#endif
+};
+
+
+// Bit fields set to one in the mask are not compared
+static
+cmsFormatter _cmsGetStockInputFormatter(cmsUInt32Number dwInput, cmsUInt32Number dwFlags)
+{
+ cmsUInt32Number i;
+ cmsFormatter fr;
+
+ switch (dwFlags) {
+
+ case CMS_PACK_FLAGS_16BITS: {
+ for (i=0; i < sizeof(InputFormatters16) / sizeof(cmsFormatters16); i++) {
+ cmsFormatters16* f = InputFormatters16 + i;
+
+ if ((dwInput & ~f ->Mask) == f ->Type) {
+ fr.Fmt16 = f ->Frm;
+ return fr;
+ }
+ }
+ }
+ break;
+
+ case CMS_PACK_FLAGS_FLOAT: {
+ for (i=0; i < sizeof(InputFormattersFloat) / sizeof(cmsFormattersFloat); i++) {
+ cmsFormattersFloat* f = InputFormattersFloat + i;
+
+ if ((dwInput & ~f ->Mask) == f ->Type) {
+ fr.FmtFloat = f ->Frm;
+ return fr;
+ }
+ }
+ }
+ break;
+
+ default:;
+
+ }
+
+ fr.Fmt16 = NULL;
+ return fr;
+}
+
+static cmsFormatters16 OutputFormatters16[] = {
+ // Type Mask Function
+ // ---------------------------- ------------------------------------ ----------------------------
+
+ { TYPE_Lab_DBL, ANYPLANAR|ANYEXTRA, PackLabDoubleFrom16},
+ { TYPE_XYZ_DBL, ANYPLANAR|ANYEXTRA, PackXYZDoubleFrom16},
+
+ { TYPE_Lab_FLT, ANYPLANAR|ANYEXTRA, PackLabFloatFrom16},
+ { TYPE_XYZ_FLT, ANYPLANAR|ANYEXTRA, PackXYZFloatFrom16},
+
+ { FLOAT_SH(1)|BYTES_SH(0), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|
+ ANYCHANNELS|ANYPLANAR|ANYEXTRA|ANYSPACE, PackDoubleFrom16},
+ { FLOAT_SH(1)|BYTES_SH(4), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|
+ ANYCHANNELS|ANYPLANAR|ANYEXTRA|ANYSPACE, PackFloatFrom16},
+#ifndef CMS_NO_HALF_SUPPORT
+ { FLOAT_SH(1)|BYTES_SH(2), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|
+ ANYCHANNELS|ANYPLANAR|ANYEXTRA|ANYSPACE, PackHalfFrom16},
+#endif
+
+ { CHANNELS_SH(1)|BYTES_SH(1), ANYSPACE, Pack1Byte},
+ { CHANNELS_SH(1)|BYTES_SH(1)|EXTRA_SH(1), ANYSPACE, Pack1ByteSkip1},
+ { CHANNELS_SH(1)|BYTES_SH(1)|EXTRA_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack1ByteSkip1SwapFirst},
+
+ { CHANNELS_SH(1)|BYTES_SH(1)|FLAVOR_SH(1), ANYSPACE, Pack1ByteReversed},
+
+ { TYPE_LabV2_8, 0, PackLabV2_8 },
+ { TYPE_ALabV2_8, 0, PackALabV2_8 },
+ { TYPE_LabV2_16, 0, PackLabV2_16 },
+
+ { CHANNELS_SH(3)|BYTES_SH(1)|OPTIMIZED_SH(1), ANYSPACE, Pack3BytesOptimized},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|OPTIMIZED_SH(1), ANYSPACE, Pack3BytesAndSkip1Optimized},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|SWAPFIRST_SH(1)|OPTIMIZED_SH(1),
+ ANYSPACE, Pack3BytesAndSkip1SwapFirstOptimized},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1)|OPTIMIZED_SH(1),
+ ANYSPACE, Pack3BytesAndSkip1SwapSwapFirstOptimized},
+ { CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|EXTRA_SH(1)|OPTIMIZED_SH(1),
+ ANYSPACE, Pack3BytesAndSkip1SwapOptimized},
+ { CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|OPTIMIZED_SH(1), ANYSPACE, Pack3BytesSwapOptimized},
+
+
+
+ { CHANNELS_SH(3)|BYTES_SH(1), ANYSPACE, Pack3Bytes},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1), ANYSPACE, Pack3BytesAndSkip1},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack3BytesAndSkip1SwapFirst},
+ { CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1),
+ ANYSPACE, Pack3BytesAndSkip1SwapSwapFirst},
+ { CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1)|EXTRA_SH(1), ANYSPACE, Pack3BytesAndSkip1Swap},
+ { CHANNELS_SH(3)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Pack3BytesSwap},
+ { CHANNELS_SH(6)|BYTES_SH(1), ANYSPACE, Pack6Bytes},
+ { CHANNELS_SH(6)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Pack6BytesSwap},
+ { CHANNELS_SH(4)|BYTES_SH(1), ANYSPACE, Pack4Bytes},
+ { CHANNELS_SH(4)|BYTES_SH(1)|FLAVOR_SH(1), ANYSPACE, Pack4BytesReverse},
+ { CHANNELS_SH(4)|BYTES_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack4BytesSwapFirst},
+ { CHANNELS_SH(4)|BYTES_SH(1)|DOSWAP_SH(1), ANYSPACE, Pack4BytesSwap},
+ { CHANNELS_SH(4)|BYTES_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack4BytesSwapSwapFirst},
+
+ { BYTES_SH(1), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackAnyBytes},
+ { BYTES_SH(1)|PLANAR_SH(1), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackPlanarBytes},
+
+ { CHANNELS_SH(1)|BYTES_SH(2), ANYSPACE, Pack1Word},
+ { CHANNELS_SH(1)|BYTES_SH(2)|EXTRA_SH(1), ANYSPACE, Pack1WordSkip1},
+ { CHANNELS_SH(1)|BYTES_SH(2)|EXTRA_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack1WordSkip1SwapFirst},
+ { CHANNELS_SH(1)|BYTES_SH(2)|FLAVOR_SH(1), ANYSPACE, Pack1WordReversed},
+ { CHANNELS_SH(1)|BYTES_SH(2)|ENDIAN16_SH(1), ANYSPACE, Pack1WordBigEndian},
+ { CHANNELS_SH(3)|BYTES_SH(2), ANYSPACE, Pack3Words},
+ { CHANNELS_SH(3)|BYTES_SH(2)|DOSWAP_SH(1), ANYSPACE, Pack3WordsSwap},
+ { CHANNELS_SH(3)|BYTES_SH(2)|ENDIAN16_SH(1), ANYSPACE, Pack3WordsBigEndian},
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1), ANYSPACE, Pack3WordsAndSkip1},
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1)|DOSWAP_SH(1), ANYSPACE, Pack3WordsAndSkip1Swap},
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1)|SWAPFIRST_SH(1), ANYSPACE, Pack3WordsAndSkip1SwapFirst},
+
+ { CHANNELS_SH(3)|BYTES_SH(2)|EXTRA_SH(1)|DOSWAP_SH(1)|SWAPFIRST_SH(1),
+ ANYSPACE, Pack3WordsAndSkip1SwapSwapFirst},
+
+ { CHANNELS_SH(4)|BYTES_SH(2), ANYSPACE, Pack4Words},
+ { CHANNELS_SH(4)|BYTES_SH(2)|FLAVOR_SH(1), ANYSPACE, Pack4WordsReverse},
+ { CHANNELS_SH(4)|BYTES_SH(2)|DOSWAP_SH(1), ANYSPACE, Pack4WordsSwap},
+ { CHANNELS_SH(4)|BYTES_SH(2)|ENDIAN16_SH(1), ANYSPACE, Pack4WordsBigEndian},
+
+ { CHANNELS_SH(6)|BYTES_SH(2), ANYSPACE, Pack6Words},
+ { CHANNELS_SH(6)|BYTES_SH(2)|DOSWAP_SH(1), ANYSPACE, Pack6WordsSwap},
+
+ { BYTES_SH(2)|PLANAR_SH(1), ANYFLAVOR|ANYENDIAN|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackPlanarWords},
+ { BYTES_SH(2), ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYENDIAN|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackAnyWords}
+
+};
+
+
+static cmsFormattersFloat OutputFormattersFloat[] = {
+ // Type Mask Function
+ // ---------------------------- --------------------------------------------------- ----------------------------
+ { TYPE_Lab_FLT, ANYPLANAR|ANYEXTRA, PackLabFloatFromFloat},
+ { TYPE_XYZ_FLT, ANYPLANAR|ANYEXTRA, PackXYZFloatFromFloat},
+
+ { TYPE_Lab_DBL, ANYPLANAR|ANYEXTRA, PackLabDoubleFromFloat},
+ { TYPE_XYZ_DBL, ANYPLANAR|ANYEXTRA, PackXYZDoubleFromFloat},
+
+ { FLOAT_SH(1)|BYTES_SH(4), ANYPLANAR|
+ ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackFloatsFromFloat },
+ { FLOAT_SH(1)|BYTES_SH(0), ANYPLANAR|
+ ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackDoublesFromFloat },
+#ifndef CMS_NO_HALF_SUPPORT
+ { FLOAT_SH(1)|BYTES_SH(2),
+ ANYFLAVOR|ANYSWAPFIRST|ANYSWAP|ANYEXTRA|ANYCHANNELS|ANYSPACE, PackHalfFromFloat },
+#endif
+
+
+
+};
+
+
+// Bit fields set to one in the mask are not compared
+static
+cmsFormatter _cmsGetStockOutputFormatter(cmsUInt32Number dwInput, cmsUInt32Number dwFlags)
+{
+ cmsUInt32Number i;
+ cmsFormatter fr;
+
+
+ switch (dwFlags)
+ {
+
+ case CMS_PACK_FLAGS_16BITS: {
+
+ for (i=0; i < sizeof(OutputFormatters16) / sizeof(cmsFormatters16); i++) {
+ cmsFormatters16* f = OutputFormatters16 + i;
+
+ if ((dwInput & ~f ->Mask) == f ->Type) {
+ fr.Fmt16 = f ->Frm;
+ return fr;
+ }
+ }
+ }
+ break;
+
+ case CMS_PACK_FLAGS_FLOAT: {
+
+ for (i=0; i < sizeof(OutputFormattersFloat) / sizeof(cmsFormattersFloat); i++) {
+ cmsFormattersFloat* f = OutputFormattersFloat + i;
+
+ if ((dwInput & ~f ->Mask) == f ->Type) {
+ fr.FmtFloat = f ->Frm;
+ return fr;
+ }
+ }
+ }
+ break;
+
+ default:;
+
+ }
+
+ fr.Fmt16 = NULL;
+ return fr;
+}
+
+
+typedef struct _cms_formatters_factory_list {
+
+ cmsFormatterFactory Factory;
+ struct _cms_formatters_factory_list *Next;
+
+} cmsFormattersFactoryList;
+
+_cmsFormattersPluginChunkType _cmsFormattersPluginChunk = { NULL };
+
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupFormatterFactoryList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsFormattersPluginChunkType newHead = { NULL };
+ cmsFormattersFactoryList* entry;
+ cmsFormattersFactoryList* Anterior = NULL;
+ _cmsFormattersPluginChunkType* head = (_cmsFormattersPluginChunkType*) src->chunks[FormattersPlugin];
+
+ _cmsAssert(head != NULL);
+
+ // Walk the list copying all nodes
+ for (entry = head->FactoryList;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ cmsFormattersFactoryList *newEntry = ( cmsFormattersFactoryList *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(cmsFormattersFactoryList));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.FactoryList == NULL)
+ newHead.FactoryList = newEntry;
+ }
+
+ ctx ->chunks[FormattersPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsFormattersPluginChunkType));
+}
+
+// The interpolation plug-in memory chunk allocator/dup
+void _cmsAllocFormattersPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsAssert(ctx != NULL);
+
+ if (src != NULL) {
+
+ // Duplicate the LIST
+ DupFormatterFactoryList(ctx, src);
+ }
+ else {
+ static _cmsFormattersPluginChunkType FormattersPluginChunk = { NULL };
+ ctx ->chunks[FormattersPlugin] = _cmsSubAllocDup(ctx ->MemPool, &FormattersPluginChunk, sizeof(_cmsFormattersPluginChunkType));
+ }
+}
+
+
+
+// Formatters management
+cmsBool _cmsRegisterFormattersPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ _cmsFormattersPluginChunkType* ctx = ( _cmsFormattersPluginChunkType*) _cmsContextGetClientChunk(ContextID, FormattersPlugin);
+ cmsPluginFormatters* Plugin = (cmsPluginFormatters*) Data;
+ cmsFormattersFactoryList* fl ;
+
+ // Reset to built-in defaults
+ if (Data == NULL) {
+
+ ctx ->FactoryList = NULL;
+ return TRUE;
+ }
+
+ fl = (cmsFormattersFactoryList*) _cmsPluginMalloc(ContextID, sizeof(cmsFormattersFactoryList));
+ if (fl == NULL) return FALSE;
+
+ fl ->Factory = Plugin ->FormattersFactory;
+
+ fl ->Next = ctx -> FactoryList;
+ ctx ->FactoryList = fl;
+
+ return TRUE;
+}
+
+cmsFormatter _cmsGetFormatter(cmsContext ContextID,
+ cmsUInt32Number Type, // Specific type, i.e. TYPE_RGB_8
+ cmsFormatterDirection Dir,
+ cmsUInt32Number dwFlags)
+{
+ _cmsFormattersPluginChunkType* ctx = ( _cmsFormattersPluginChunkType*) _cmsContextGetClientChunk(ContextID, FormattersPlugin);
+ cmsFormattersFactoryList* f;
+
+ for (f =ctx->FactoryList; f != NULL; f = f ->Next) {
+
+ cmsFormatter fn = f ->Factory(Type, Dir, dwFlags);
+ if (fn.Fmt16 != NULL) return fn;
+ }
+
+ // Revert to default
+ if (Dir == cmsFormatterInput)
+ return _cmsGetStockInputFormatter(Type, dwFlags);
+ else
+ return _cmsGetStockOutputFormatter(Type, dwFlags);
+}
+
+
+// Return whatever given formatter refers to float values
+cmsBool _cmsFormatterIsFloat(cmsUInt32Number Type)
+{
+ return T_FLOAT(Type) ? TRUE : FALSE;
+}
+
+// Return whatever given formatter refers to 8 bits
+cmsBool _cmsFormatterIs8bit(cmsUInt32Number Type)
+{
+ int Bytes = T_BYTES(Type);
+
+ return (Bytes == 1);
+}
+
+// Build a suitable formatter for the colorspace of this profile
+cmsUInt32Number CMSEXPORT cmsFormatterForColorspaceOfProfile(cmsHPROFILE hProfile, cmsUInt32Number nBytes, cmsBool lIsFloat)
+{
+
+ cmsColorSpaceSignature ColorSpace = cmsGetColorSpace(hProfile);
+ cmsUInt32Number ColorSpaceBits = _cmsLCMScolorSpace(ColorSpace);
+ cmsUInt32Number nOutputChans = cmsChannelsOf(ColorSpace);
+ cmsUInt32Number Float = lIsFloat ? 1 : 0;
+
+ // Create a fake formatter for result
+ return FLOAT_SH(Float) | COLORSPACE_SH(ColorSpaceBits) | BYTES_SH(nBytes) | CHANNELS_SH(nOutputChans);
+}
+
+// Build a suitable formatter for the colorspace of this profile
+cmsUInt32Number CMSEXPORT cmsFormatterForPCSOfProfile(cmsHPROFILE hProfile, cmsUInt32Number nBytes, cmsBool lIsFloat)
+{
+
+ cmsColorSpaceSignature ColorSpace = cmsGetPCS(hProfile);
+ int ColorSpaceBits = _cmsLCMScolorSpace(ColorSpace);
+ cmsUInt32Number nOutputChans = cmsChannelsOf(ColorSpace);
+ cmsUInt32Number Float = lIsFloat ? 1 : 0;
+
+ // Create a fake formatter for result
+ return FLOAT_SH(Float) | COLORSPACE_SH(ColorSpaceBits) | BYTES_SH(nBytes) | CHANNELS_SH(nOutputChans);
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspcs.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspcs.c
new file mode 100644
index 0000000000..102cd7d21e
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmspcs.c
@@ -0,0 +1,931 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2010 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"
+
+// inter PCS conversions XYZ <-> CIE L* a* b*
+/*
+
+
+ CIE 15:2004 CIELab is defined as:
+
+ L* = 116*f(Y/Yn) - 16 0 <= L* <= 100
+ a* = 500*[f(X/Xn) - f(Y/Yn)]
+ b* = 200*[f(Y/Yn) - f(Z/Zn)]
+
+ and
+
+ f(t) = t^(1/3) 1 >= t > (24/116)^3
+ (841/108)*t + (16/116) 0 <= t <= (24/116)^3
+
+
+ Reverse transform is:
+
+ X = Xn*[a* / 500 + (L* + 16) / 116] ^ 3 if (X/Xn) > (24/116)
+ = Xn*(a* / 500 + L* / 116) / 7.787 if (X/Xn) <= (24/116)
+
+
+
+ PCS in Lab2 is encoded as:
+
+ 8 bit Lab PCS:
+
+ L* 0..100 into a 0..ff byte.
+ a* t + 128 range is -128.0 +127.0
+ b*
+
+ 16 bit Lab PCS:
+
+ L* 0..100 into a 0..ff00 word.
+ a* t + 128 range is -128.0 +127.9961
+ b*
+
+
+
+Interchange Space Component Actual Range Encoded Range
+CIE XYZ X 0 -> 1.99997 0x0000 -> 0xffff
+CIE XYZ Y 0 -> 1.99997 0x0000 -> 0xffff
+CIE XYZ Z 0 -> 1.99997 0x0000 -> 0xffff
+
+Version 2,3
+-----------
+
+CIELAB (16 bit) L* 0 -> 100.0 0x0000 -> 0xff00
+CIELAB (16 bit) a* -128.0 -> +127.996 0x0000 -> 0x8000 -> 0xffff
+CIELAB (16 bit) b* -128.0 -> +127.996 0x0000 -> 0x8000 -> 0xffff
+
+
+Version 4
+---------
+
+CIELAB (16 bit) L* 0 -> 100.0 0x0000 -> 0xffff
+CIELAB (16 bit) a* -128.0 -> +127 0x0000 -> 0x8080 -> 0xffff
+CIELAB (16 bit) b* -128.0 -> +127 0x0000 -> 0x8080 -> 0xffff
+
+*/
+
+// Conversions
+void CMSEXPORT cmsXYZ2xyY(cmsCIExyY* Dest, const cmsCIEXYZ* Source)
+{
+ cmsFloat64Number ISum;
+
+ ISum = 1./(Source -> X + Source -> Y + Source -> Z);
+
+ Dest -> x = (Source -> X) * ISum;
+ Dest -> y = (Source -> Y) * ISum;
+ Dest -> Y = Source -> Y;
+}
+
+void CMSEXPORT cmsxyY2XYZ(cmsCIEXYZ* Dest, const cmsCIExyY* Source)
+{
+ Dest -> X = (Source -> x / Source -> y) * Source -> Y;
+ Dest -> Y = Source -> Y;
+ Dest -> Z = ((1 - Source -> x - Source -> y) / Source -> y) * Source -> Y;
+}
+
+static
+cmsFloat64Number f(cmsFloat64Number t)
+{
+ const cmsFloat64Number Limit = (24.0/116.0) * (24.0/116.0) * (24.0/116.0);
+
+ if (t <= Limit)
+ return (841.0/108.0) * t + (16.0/116.0);
+ else
+ return pow(t, 1.0/3.0);
+}
+
+static
+cmsFloat64Number f_1(cmsFloat64Number t)
+{
+ const cmsFloat64Number Limit = (24.0/116.0);
+
+ if (t <= Limit) {
+ return (108.0/841.0) * (t - (16.0/116.0));
+ }
+
+ return t * t * t;
+}
+
+
+// Standard XYZ to Lab. it can handle negative XZY numbers in some cases
+void CMSEXPORT cmsXYZ2Lab(const cmsCIEXYZ* WhitePoint, cmsCIELab* Lab, const cmsCIEXYZ* xyz)
+{
+ cmsFloat64Number fx, fy, fz;
+
+ if (WhitePoint == NULL)
+ WhitePoint = cmsD50_XYZ();
+
+ fx = f(xyz->X / WhitePoint->X);
+ fy = f(xyz->Y / WhitePoint->Y);
+ fz = f(xyz->Z / WhitePoint->Z);
+
+ Lab->L = 116.0*fy - 16.0;
+ Lab->a = 500.0*(fx - fy);
+ Lab->b = 200.0*(fy - fz);
+}
+
+
+// Standard XYZ to Lab. It can return negative XYZ in some cases
+void CMSEXPORT cmsLab2XYZ(const cmsCIEXYZ* WhitePoint, cmsCIEXYZ* xyz, const cmsCIELab* Lab)
+{
+ cmsFloat64Number x, y, z;
+
+ if (WhitePoint == NULL)
+ WhitePoint = cmsD50_XYZ();
+
+ y = (Lab-> L + 16.0) / 116.0;
+ x = y + 0.002 * Lab -> a;
+ z = y - 0.005 * Lab -> b;
+
+ xyz -> X = f_1(x) * WhitePoint -> X;
+ xyz -> Y = f_1(y) * WhitePoint -> Y;
+ xyz -> Z = f_1(z) * WhitePoint -> Z;
+
+}
+
+static
+cmsFloat64Number L2float2(cmsUInt16Number v)
+{
+ return (cmsFloat64Number) v / 652.800;
+}
+
+// the a/b part
+static
+cmsFloat64Number ab2float2(cmsUInt16Number v)
+{
+ return ((cmsFloat64Number) v / 256.0) - 128.0;
+}
+
+static
+cmsUInt16Number L2Fix2(cmsFloat64Number L)
+{
+ return _cmsQuickSaturateWord(L * 652.8);
+}
+
+static
+cmsUInt16Number ab2Fix2(cmsFloat64Number ab)
+{
+ return _cmsQuickSaturateWord((ab + 128.0) * 256.0);
+}
+
+
+static
+cmsFloat64Number L2float4(cmsUInt16Number v)
+{
+ return (cmsFloat64Number) v / 655.35;
+}
+
+// the a/b part
+static
+cmsFloat64Number ab2float4(cmsUInt16Number v)
+{
+ return ((cmsFloat64Number) v / 257.0) - 128.0;
+}
+
+
+void CMSEXPORT cmsLabEncoded2FloatV2(cmsCIELab* Lab, const cmsUInt16Number wLab[3])
+{
+ Lab->L = L2float2(wLab[0]);
+ Lab->a = ab2float2(wLab[1]);
+ Lab->b = ab2float2(wLab[2]);
+}
+
+
+void CMSEXPORT cmsLabEncoded2Float(cmsCIELab* Lab, const cmsUInt16Number wLab[3])
+{
+ Lab->L = L2float4(wLab[0]);
+ Lab->a = ab2float4(wLab[1]);
+ Lab->b = ab2float4(wLab[2]);
+}
+
+static
+cmsFloat64Number Clamp_L_doubleV2(cmsFloat64Number L)
+{
+ const cmsFloat64Number L_max = (cmsFloat64Number) (0xFFFF * 100.0) / 0xFF00;
+
+ if (L < 0) L = 0;
+ if (L > L_max) L = L_max;
+
+ return L;
+}
+
+
+static
+cmsFloat64Number Clamp_ab_doubleV2(cmsFloat64Number ab)
+{
+ if (ab < MIN_ENCODEABLE_ab2) ab = MIN_ENCODEABLE_ab2;
+ if (ab > MAX_ENCODEABLE_ab2) ab = MAX_ENCODEABLE_ab2;
+
+ return ab;
+}
+
+void CMSEXPORT cmsFloat2LabEncodedV2(cmsUInt16Number wLab[3], const cmsCIELab* fLab)
+{
+ cmsCIELab Lab;
+
+ Lab.L = Clamp_L_doubleV2(fLab ->L);
+ Lab.a = Clamp_ab_doubleV2(fLab ->a);
+ Lab.b = Clamp_ab_doubleV2(fLab ->b);
+
+ wLab[0] = L2Fix2(Lab.L);
+ wLab[1] = ab2Fix2(Lab.a);
+ wLab[2] = ab2Fix2(Lab.b);
+}
+
+
+static
+cmsFloat64Number Clamp_L_doubleV4(cmsFloat64Number L)
+{
+ if (L < 0) L = 0;
+ if (L > 100.0) L = 100.0;
+
+ return L;
+}
+
+static
+cmsFloat64Number Clamp_ab_doubleV4(cmsFloat64Number ab)
+{
+ if (ab < MIN_ENCODEABLE_ab4) ab = MIN_ENCODEABLE_ab4;
+ if (ab > MAX_ENCODEABLE_ab4) ab = MAX_ENCODEABLE_ab4;
+
+ return ab;
+}
+
+static
+cmsUInt16Number L2Fix4(cmsFloat64Number L)
+{
+ return _cmsQuickSaturateWord(L * 655.35);
+}
+
+static
+cmsUInt16Number ab2Fix4(cmsFloat64Number ab)
+{
+ return _cmsQuickSaturateWord((ab + 128.0) * 257.0);
+}
+
+void CMSEXPORT cmsFloat2LabEncoded(cmsUInt16Number wLab[3], const cmsCIELab* fLab)
+{
+ cmsCIELab Lab;
+
+ Lab.L = Clamp_L_doubleV4(fLab ->L);
+ Lab.a = Clamp_ab_doubleV4(fLab ->a);
+ Lab.b = Clamp_ab_doubleV4(fLab ->b);
+
+ wLab[0] = L2Fix4(Lab.L);
+ wLab[1] = ab2Fix4(Lab.a);
+ wLab[2] = ab2Fix4(Lab.b);
+}
+
+// Auxiliar: convert to Radians
+static
+cmsFloat64Number RADIANS(cmsFloat64Number deg)
+{
+ return (deg * M_PI) / 180.;
+}
+
+
+// Auxiliar: atan2 but operating in degrees and returning 0 if a==b==0
+static
+cmsFloat64Number atan2deg(cmsFloat64Number a, cmsFloat64Number b)
+{
+ cmsFloat64Number h;
+
+ if (a == 0 && b == 0)
+ h = 0;
+ else
+ h = atan2(a, b);
+
+ h *= (180. / M_PI);
+
+ while (h > 360.)
+ h -= 360.;
+
+ while ( h < 0)
+ h += 360.;
+
+ return h;
+}
+
+
+// Auxiliar: Square
+static
+cmsFloat64Number Sqr(cmsFloat64Number v)
+{
+ return v * v;
+}
+// From cylindrical coordinates. No check is performed, then negative values are allowed
+void CMSEXPORT cmsLab2LCh(cmsCIELCh* LCh, const cmsCIELab* Lab)
+{
+ LCh -> L = Lab -> L;
+ LCh -> C = pow(Sqr(Lab ->a) + Sqr(Lab ->b), 0.5);
+ LCh -> h = atan2deg(Lab ->b, Lab ->a);
+}
+
+
+// To cylindrical coordinates. No check is performed, then negative values are allowed
+void CMSEXPORT cmsLCh2Lab(cmsCIELab* Lab, const cmsCIELCh* LCh)
+{
+ cmsFloat64Number h = (LCh -> h * M_PI) / 180.0;
+
+ Lab -> L = LCh -> L;
+ Lab -> a = LCh -> C * cos(h);
+ Lab -> b = LCh -> C * sin(h);
+}
+
+// In XYZ All 3 components are encoded using 1.15 fixed point
+static
+cmsUInt16Number XYZ2Fix(cmsFloat64Number d)
+{
+ return _cmsQuickSaturateWord(d * 32768.0);
+}
+
+void CMSEXPORT cmsFloat2XYZEncoded(cmsUInt16Number XYZ[3], const cmsCIEXYZ* fXYZ)
+{
+ cmsCIEXYZ xyz;
+
+ xyz.X = fXYZ -> X;
+ xyz.Y = fXYZ -> Y;
+ xyz.Z = fXYZ -> Z;
+
+ // Clamp to encodeable values.
+ if (xyz.Y <= 0) {
+
+ xyz.X = 0;
+ xyz.Y = 0;
+ xyz.Z = 0;
+ }
+
+ if (xyz.X > MAX_ENCODEABLE_XYZ)
+ xyz.X = MAX_ENCODEABLE_XYZ;
+
+ if (xyz.X < 0)
+ xyz.X = 0;
+
+ if (xyz.Y > MAX_ENCODEABLE_XYZ)
+ xyz.Y = MAX_ENCODEABLE_XYZ;
+
+ if (xyz.Y < 0)
+ xyz.Y = 0;
+
+ if (xyz.Z > MAX_ENCODEABLE_XYZ)
+ xyz.Z = MAX_ENCODEABLE_XYZ;
+
+ if (xyz.Z < 0)
+ xyz.Z = 0;
+
+
+ XYZ[0] = XYZ2Fix(xyz.X);
+ XYZ[1] = XYZ2Fix(xyz.Y);
+ XYZ[2] = XYZ2Fix(xyz.Z);
+}
+
+
+// To convert from Fixed 1.15 point to cmsFloat64Number
+static
+cmsFloat64Number XYZ2float(cmsUInt16Number v)
+{
+ cmsS15Fixed16Number fix32;
+
+ // From 1.15 to 15.16
+ fix32 = v << 1;
+
+ // From fixed 15.16 to cmsFloat64Number
+ return _cms15Fixed16toDouble(fix32);
+}
+
+
+void CMSEXPORT cmsXYZEncoded2Float(cmsCIEXYZ* fXYZ, const cmsUInt16Number XYZ[3])
+{
+ fXYZ -> X = XYZ2float(XYZ[0]);
+ fXYZ -> Y = XYZ2float(XYZ[1]);
+ fXYZ -> Z = XYZ2float(XYZ[2]);
+}
+
+
+// Returns dE on two Lab values
+cmsFloat64Number CMSEXPORT cmsDeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2)
+{
+ cmsFloat64Number dL, da, db;
+
+ dL = fabs(Lab1 -> L - Lab2 -> L);
+ da = fabs(Lab1 -> a - Lab2 -> a);
+ db = fabs(Lab1 -> b - Lab2 -> b);
+
+ return pow(Sqr(dL) + Sqr(da) + Sqr(db), 0.5);
+}
+
+
+// Return the CIE94 Delta E
+cmsFloat64Number CMSEXPORT cmsCIE94DeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2)
+{
+ cmsCIELCh LCh1, LCh2;
+ cmsFloat64Number dE, dL, dC, dh, dhsq;
+ cmsFloat64Number c12, sc, sh;
+
+ dL = fabs(Lab1 ->L - Lab2 ->L);
+
+ cmsLab2LCh(&LCh1, Lab1);
+ cmsLab2LCh(&LCh2, Lab2);
+
+ dC = fabs(LCh1.C - LCh2.C);
+ dE = cmsDeltaE(Lab1, Lab2);
+
+ dhsq = Sqr(dE) - Sqr(dL) - Sqr(dC);
+ if (dhsq < 0)
+ dh = 0;
+ else
+ dh = pow(dhsq, 0.5);
+
+ c12 = sqrt(LCh1.C * LCh2.C);
+
+ sc = 1.0 + (0.048 * c12);
+ sh = 1.0 + (0.014 * c12);
+
+ return sqrt(Sqr(dL) + Sqr(dC) / Sqr(sc) + Sqr(dh) / Sqr(sh));
+}
+
+
+// Auxiliary
+static
+cmsFloat64Number ComputeLBFD(const cmsCIELab* Lab)
+{
+ cmsFloat64Number yt;
+
+ if (Lab->L > 7.996969)
+ yt = (Sqr((Lab->L+16)/116)*((Lab->L+16)/116))*100;
+ else
+ yt = 100 * (Lab->L / 903.3);
+
+ return (54.6 * (M_LOG10E * (log(yt + 1.5))) - 9.6);
+}
+
+
+
+// bfd - gets BFD(1:1) difference between Lab1, Lab2
+cmsFloat64Number CMSEXPORT cmsBFDdeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2)
+{
+ cmsFloat64Number lbfd1,lbfd2,AveC,Aveh,dE,deltaL,
+ deltaC,deltah,dc,t,g,dh,rh,rc,rt,bfd;
+ cmsCIELCh LCh1, LCh2;
+
+
+ lbfd1 = ComputeLBFD(Lab1);
+ lbfd2 = ComputeLBFD(Lab2);
+ deltaL = lbfd2 - lbfd1;
+
+ cmsLab2LCh(&LCh1, Lab1);
+ cmsLab2LCh(&LCh2, Lab2);
+
+ deltaC = LCh2.C - LCh1.C;
+ AveC = (LCh1.C+LCh2.C)/2;
+ Aveh = (LCh1.h+LCh2.h)/2;
+
+ dE = cmsDeltaE(Lab1, Lab2);
+
+ if (Sqr(dE)>(Sqr(Lab2->L-Lab1->L)+Sqr(deltaC)))
+ deltah = sqrt(Sqr(dE)-Sqr(Lab2->L-Lab1->L)-Sqr(deltaC));
+ else
+ deltah =0;
+
+
+ dc = 0.035 * AveC / (1 + 0.00365 * AveC)+0.521;
+ g = sqrt(Sqr(Sqr(AveC))/(Sqr(Sqr(AveC))+14000));
+ t = 0.627+(0.055*cos((Aveh-254)/(180/M_PI))-
+ 0.040*cos((2*Aveh-136)/(180/M_PI))+
+ 0.070*cos((3*Aveh-31)/(180/M_PI))+
+ 0.049*cos((4*Aveh+114)/(180/M_PI))-
+ 0.015*cos((5*Aveh-103)/(180/M_PI)));
+
+ dh = dc*(g*t+1-g);
+ rh = -0.260*cos((Aveh-308)/(180/M_PI))-
+ 0.379*cos((2*Aveh-160)/(180/M_PI))-
+ 0.636*cos((3*Aveh+254)/(180/M_PI))+
+ 0.226*cos((4*Aveh+140)/(180/M_PI))-
+ 0.194*cos((5*Aveh+280)/(180/M_PI));
+
+ rc = sqrt((AveC*AveC*AveC*AveC*AveC*AveC)/((AveC*AveC*AveC*AveC*AveC*AveC)+70000000));
+ rt = rh*rc;
+
+ bfd = sqrt(Sqr(deltaL)+Sqr(deltaC/dc)+Sqr(deltah/dh)+(rt*(deltaC/dc)*(deltah/dh)));
+
+ return bfd;
+}
+
+
+// cmc - CMC(l:c) difference between Lab1, Lab2
+cmsFloat64Number CMSEXPORT cmsCMCdeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2, cmsFloat64Number l, cmsFloat64Number c)
+{
+ cmsFloat64Number dE,dL,dC,dh,sl,sc,sh,t,f,cmc;
+ cmsCIELCh LCh1, LCh2;
+
+ if (Lab1 ->L == 0 && Lab2 ->L == 0) return 0;
+
+ cmsLab2LCh(&LCh1, Lab1);
+ cmsLab2LCh(&LCh2, Lab2);
+
+
+ dL = Lab2->L-Lab1->L;
+ dC = LCh2.C-LCh1.C;
+
+ dE = cmsDeltaE(Lab1, Lab2);
+
+ if (Sqr(dE)>(Sqr(dL)+Sqr(dC)))
+ dh = sqrt(Sqr(dE)-Sqr(dL)-Sqr(dC));
+ else
+ dh =0;
+
+ if ((LCh1.h > 164) && (LCh1.h < 345))
+ t = 0.56 + fabs(0.2 * cos(((LCh1.h + 168)/(180/M_PI))));
+ else
+ t = 0.36 + fabs(0.4 * cos(((LCh1.h + 35 )/(180/M_PI))));
+
+ sc = 0.0638 * LCh1.C / (1 + 0.0131 * LCh1.C) + 0.638;
+ sl = 0.040975 * Lab1->L /(1 + 0.01765 * Lab1->L);
+
+ if (Lab1->L<16)
+ sl = 0.511;
+
+ f = sqrt((LCh1.C * LCh1.C * LCh1.C * LCh1.C)/((LCh1.C * LCh1.C * LCh1.C * LCh1.C)+1900));
+ sh = sc*(t*f+1-f);
+ cmc = sqrt(Sqr(dL/(l*sl))+Sqr(dC/(c*sc))+Sqr(dh/sh));
+
+ return cmc;
+}
+
+// dE2000 The weightings KL, KC and KH can be modified to reflect the relative
+// importance of lightness, chroma and hue in different industrial applications
+cmsFloat64Number CMSEXPORT cmsCIE2000DeltaE(const cmsCIELab* Lab1, const cmsCIELab* Lab2,
+ cmsFloat64Number Kl, cmsFloat64Number Kc, cmsFloat64Number Kh)
+{
+ cmsFloat64Number L1 = Lab1->L;
+ cmsFloat64Number a1 = Lab1->a;
+ cmsFloat64Number b1 = Lab1->b;
+ cmsFloat64Number C = sqrt( Sqr(a1) + Sqr(b1) );
+
+ cmsFloat64Number Ls = Lab2 ->L;
+ cmsFloat64Number as = Lab2 ->a;
+ cmsFloat64Number bs = Lab2 ->b;
+ cmsFloat64Number Cs = sqrt( Sqr(as) + Sqr(bs) );
+
+ cmsFloat64Number G = 0.5 * ( 1 - sqrt(pow((C + Cs) / 2 , 7.0) / (pow((C + Cs) / 2, 7.0) + pow(25.0, 7.0) ) ));
+
+ cmsFloat64Number a_p = (1 + G ) * a1;
+ cmsFloat64Number b_p = b1;
+ cmsFloat64Number C_p = sqrt( Sqr(a_p) + Sqr(b_p));
+ cmsFloat64Number h_p = atan2deg(b_p, a_p);
+
+
+ cmsFloat64Number a_ps = (1 + G) * as;
+ cmsFloat64Number b_ps = bs;
+ cmsFloat64Number C_ps = sqrt(Sqr(a_ps) + Sqr(b_ps));
+ cmsFloat64Number h_ps = atan2deg(b_ps, a_ps);
+
+ cmsFloat64Number meanC_p =(C_p + C_ps) / 2;
+
+ cmsFloat64Number hps_plus_hp = h_ps + h_p;
+ cmsFloat64Number hps_minus_hp = h_ps - h_p;
+
+ cmsFloat64Number meanh_p = fabs(hps_minus_hp) <= 180.000001 ? (hps_plus_hp)/2 :
+ (hps_plus_hp) < 360 ? (hps_plus_hp + 360)/2 :
+ (hps_plus_hp - 360)/2;
+
+ cmsFloat64Number delta_h = (hps_minus_hp) <= -180.000001 ? (hps_minus_hp + 360) :
+ (hps_minus_hp) > 180 ? (hps_minus_hp - 360) :
+ (hps_minus_hp);
+ cmsFloat64Number delta_L = (Ls - L1);
+ cmsFloat64Number delta_C = (C_ps - C_p );
+
+
+ cmsFloat64Number delta_H =2 * sqrt(C_ps*C_p) * sin(RADIANS(delta_h) / 2);
+
+ cmsFloat64Number T = 1 - 0.17 * cos(RADIANS(meanh_p-30))
+ + 0.24 * cos(RADIANS(2*meanh_p))
+ + 0.32 * cos(RADIANS(3*meanh_p + 6))
+ - 0.2 * cos(RADIANS(4*meanh_p - 63));
+
+ cmsFloat64Number Sl = 1 + (0.015 * Sqr((Ls + L1) /2- 50) )/ sqrt(20 + Sqr( (Ls+L1)/2 - 50) );
+
+ cmsFloat64Number Sc = 1 + 0.045 * (C_p + C_ps)/2;
+ cmsFloat64Number Sh = 1 + 0.015 * ((C_ps + C_p)/2) * T;
+
+ cmsFloat64Number delta_ro = 30 * exp( -Sqr(((meanh_p - 275 ) / 25)));
+
+ cmsFloat64Number Rc = 2 * sqrt(( pow(meanC_p, 7.0) )/( pow(meanC_p, 7.0) + pow(25.0, 7.0)));
+
+ cmsFloat64Number Rt = -sin(2 * RADIANS(delta_ro)) * Rc;
+
+ cmsFloat64Number deltaE00 = sqrt( Sqr(delta_L /(Sl * Kl)) +
+ Sqr(delta_C/(Sc * Kc)) +
+ Sqr(delta_H/(Sh * Kh)) +
+ Rt*(delta_C/(Sc * Kc)) * (delta_H / (Sh * Kh)));
+
+ return deltaE00;
+}
+
+// This function returns a number of gridpoints to be used as LUT table. It assumes same number
+// of gripdpoints in all dimensions. Flags may override the choice.
+int _cmsReasonableGridpointsByColorspace(cmsColorSpaceSignature Colorspace, cmsUInt32Number dwFlags)
+{
+ int nChannels;
+
+ // Already specified?
+ if (dwFlags & 0x00FF0000) {
+ // Yes, grab'em
+ return (dwFlags >> 16) & 0xFF;
+ }
+
+ nChannels = cmsChannelsOf(Colorspace);
+
+ // HighResPrecalc is maximum resolution
+ if (dwFlags & cmsFLAGS_HIGHRESPRECALC) {
+
+ if (nChannels > 4)
+ return 7; // 7 for Hifi
+
+ if (nChannels == 4) // 23 for CMYK
+ return 23;
+
+ return 49; // 49 for RGB and others
+ }
+
+
+ // LowResPrecal is lower resolution
+ if (dwFlags & cmsFLAGS_LOWRESPRECALC) {
+
+ if (nChannels > 4)
+ return 6; // 6 for more than 4 channels
+
+ if (nChannels == 1)
+ return 33; // For monochrome
+
+ return 17; // 17 for remaining
+ }
+
+ // Default values
+ if (nChannels > 4)
+ return 7; // 7 for Hifi
+
+ if (nChannels == 4)
+ return 17; // 17 for CMYK
+
+ return 33; // 33 for RGB
+}
+
+
+cmsBool _cmsEndPointsBySpace(cmsColorSpaceSignature Space,
+ cmsUInt16Number **White,
+ cmsUInt16Number **Black,
+ cmsUInt32Number *nOutputs)
+{
+ // Only most common spaces
+
+ static cmsUInt16Number RGBblack[4] = { 0, 0, 0 };
+ static cmsUInt16Number RGBwhite[4] = { 0xffff, 0xffff, 0xffff };
+ static cmsUInt16Number CMYKblack[4] = { 0xffff, 0xffff, 0xffff, 0xffff }; // 400% of ink
+ static cmsUInt16Number CMYKwhite[4] = { 0, 0, 0, 0 };
+ static cmsUInt16Number LABblack[4] = { 0, 0x8080, 0x8080 }; // V4 Lab encoding
+ static cmsUInt16Number LABwhite[4] = { 0xFFFF, 0x8080, 0x8080 };
+ static cmsUInt16Number CMYblack[4] = { 0xffff, 0xffff, 0xffff };
+ static cmsUInt16Number CMYwhite[4] = { 0, 0, 0 };
+ static cmsUInt16Number Grayblack[4] = { 0 };
+ static cmsUInt16Number GrayWhite[4] = { 0xffff };
+
+ switch (Space) {
+
+ case cmsSigGrayData: if (White) *White = GrayWhite;
+ if (Black) *Black = Grayblack;
+ if (nOutputs) *nOutputs = 1;
+ return TRUE;
+
+ case cmsSigRgbData: if (White) *White = RGBwhite;
+ if (Black) *Black = RGBblack;
+ if (nOutputs) *nOutputs = 3;
+ return TRUE;
+
+ case cmsSigLabData: if (White) *White = LABwhite;
+ if (Black) *Black = LABblack;
+ if (nOutputs) *nOutputs = 3;
+ return TRUE;
+
+ case cmsSigCmykData: if (White) *White = CMYKwhite;
+ if (Black) *Black = CMYKblack;
+ if (nOutputs) *nOutputs = 4;
+ return TRUE;
+
+ case cmsSigCmyData: if (White) *White = CMYwhite;
+ if (Black) *Black = CMYblack;
+ if (nOutputs) *nOutputs = 3;
+ return TRUE;
+
+ default:;
+ }
+
+ return FALSE;
+}
+
+
+
+// Several utilities -------------------------------------------------------
+
+// Translate from our colorspace to ICC representation
+
+cmsColorSpaceSignature CMSEXPORT _cmsICCcolorSpace(int OurNotation)
+{
+ switch (OurNotation) {
+
+ case 1:
+ case PT_GRAY: return cmsSigGrayData;
+
+ case 2:
+ case PT_RGB: return cmsSigRgbData;
+
+ case PT_CMY: return cmsSigCmyData;
+ case PT_CMYK: return cmsSigCmykData;
+ case PT_YCbCr:return cmsSigYCbCrData;
+ case PT_YUV: return cmsSigLuvData;
+ case PT_XYZ: return cmsSigXYZData;
+
+ case PT_LabV2:
+ case PT_Lab: return cmsSigLabData;
+
+ case PT_YUVK: return cmsSigLuvKData;
+ case PT_HSV: return cmsSigHsvData;
+ case PT_HLS: return cmsSigHlsData;
+ case PT_Yxy: return cmsSigYxyData;
+
+ case PT_MCH1: return cmsSigMCH1Data;
+ case PT_MCH2: return cmsSigMCH2Data;
+ case PT_MCH3: return cmsSigMCH3Data;
+ case PT_MCH4: return cmsSigMCH4Data;
+ case PT_MCH5: return cmsSigMCH5Data;
+ case PT_MCH6: return cmsSigMCH6Data;
+ case PT_MCH7: return cmsSigMCH7Data;
+ case PT_MCH8: return cmsSigMCH8Data;
+
+ case PT_MCH9: return cmsSigMCH9Data;
+ case PT_MCH10: return cmsSigMCHAData;
+ case PT_MCH11: return cmsSigMCHBData;
+ case PT_MCH12: return cmsSigMCHCData;
+ case PT_MCH13: return cmsSigMCHDData;
+ case PT_MCH14: return cmsSigMCHEData;
+ case PT_MCH15: return cmsSigMCHFData;
+
+ default: return (cmsColorSpaceSignature) (-1);
+ }
+}
+
+
+int CMSEXPORT _cmsLCMScolorSpace(cmsColorSpaceSignature ProfileSpace)
+{
+ switch (ProfileSpace) {
+
+ case cmsSigGrayData: return PT_GRAY;
+ case cmsSigRgbData: return PT_RGB;
+ case cmsSigCmyData: return PT_CMY;
+ case cmsSigCmykData: return PT_CMYK;
+ case cmsSigYCbCrData:return PT_YCbCr;
+ case cmsSigLuvData: return PT_YUV;
+ case cmsSigXYZData: return PT_XYZ;
+ case cmsSigLabData: return PT_Lab;
+ case cmsSigLuvKData: return PT_YUVK;
+ case cmsSigHsvData: return PT_HSV;
+ case cmsSigHlsData: return PT_HLS;
+ case cmsSigYxyData: return PT_Yxy;
+
+ case cmsSig1colorData:
+ case cmsSigMCH1Data: return PT_MCH1;
+
+ case cmsSig2colorData:
+ case cmsSigMCH2Data: return PT_MCH2;
+
+ case cmsSig3colorData:
+ case cmsSigMCH3Data: return PT_MCH3;
+
+ case cmsSig4colorData:
+ case cmsSigMCH4Data: return PT_MCH4;
+
+ case cmsSig5colorData:
+ case cmsSigMCH5Data: return PT_MCH5;
+
+ case cmsSig6colorData:
+ case cmsSigMCH6Data: return PT_MCH6;
+
+ case cmsSigMCH7Data:
+ case cmsSig7colorData:return PT_MCH7;
+
+ case cmsSigMCH8Data:
+ case cmsSig8colorData:return PT_MCH8;
+
+ case cmsSigMCH9Data:
+ case cmsSig9colorData:return PT_MCH9;
+
+ case cmsSigMCHAData:
+ case cmsSig10colorData:return PT_MCH10;
+
+ case cmsSigMCHBData:
+ case cmsSig11colorData:return PT_MCH11;
+
+ case cmsSigMCHCData:
+ case cmsSig12colorData:return PT_MCH12;
+
+ case cmsSigMCHDData:
+ case cmsSig13colorData:return PT_MCH13;
+
+ case cmsSigMCHEData:
+ case cmsSig14colorData:return PT_MCH14;
+
+ case cmsSigMCHFData:
+ case cmsSig15colorData:return PT_MCH15;
+
+ default: return (cmsColorSpaceSignature) (-1);
+ }
+}
+
+
+cmsUInt32Number CMSEXPORT cmsChannelsOf(cmsColorSpaceSignature ColorSpace)
+{
+ switch (ColorSpace) {
+
+ case cmsSigMCH1Data:
+ case cmsSig1colorData:
+ case cmsSigGrayData: return 1;
+
+ case cmsSigMCH2Data:
+ case cmsSig2colorData: return 2;
+
+ case cmsSigXYZData:
+ case cmsSigLabData:
+ case cmsSigLuvData:
+ case cmsSigYCbCrData:
+ case cmsSigYxyData:
+ case cmsSigRgbData:
+ case cmsSigHsvData:
+ case cmsSigHlsData:
+ case cmsSigCmyData:
+ case cmsSigMCH3Data:
+ case cmsSig3colorData: return 3;
+
+ case cmsSigLuvKData:
+ case cmsSigCmykData:
+ case cmsSigMCH4Data:
+ case cmsSig4colorData: return 4;
+
+ case cmsSigMCH5Data:
+ case cmsSig5colorData: return 5;
+
+ case cmsSigMCH6Data:
+ case cmsSig6colorData: return 6;
+
+ case cmsSigMCH7Data:
+ case cmsSig7colorData: return 7;
+
+ case cmsSigMCH8Data:
+ case cmsSig8colorData: return 8;
+
+ case cmsSigMCH9Data:
+ case cmsSig9colorData: return 9;
+
+ case cmsSigMCHAData:
+ case cmsSig10colorData: return 10;
+
+ case cmsSigMCHBData:
+ case cmsSig11colorData: return 11;
+
+ case cmsSigMCHCData:
+ case cmsSig12colorData: return 12;
+
+ case cmsSigMCHDData:
+ case cmsSig13colorData: return 13;
+
+ case cmsSigMCHEData:
+ case cmsSig14colorData: return 14;
+
+ case cmsSigMCHFData:
+ case cmsSig15colorData: return 15;
+
+ default: return 3;
+ }
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsplugin.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsplugin.c
new file mode 100644
index 0000000000..55928b7018
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsplugin.c
@@ -0,0 +1,955 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2010 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"
+
+
+// ----------------------------------------------------------------------------------
+// Encoding & Decoding support functions
+// ----------------------------------------------------------------------------------
+
+// Little-Endian to Big-Endian
+
+// Adjust a word value after being readed/ before being written from/to an ICC profile
+cmsUInt16Number CMSEXPORT _cmsAdjustEndianess16(cmsUInt16Number Word)
+{
+#ifndef CMS_USE_BIG_ENDIAN
+
+ cmsUInt8Number* pByte = (cmsUInt8Number*) &Word;
+ cmsUInt8Number tmp;
+
+ tmp = pByte[0];
+ pByte[0] = pByte[1];
+ pByte[1] = tmp;
+#endif
+
+ return Word;
+}
+
+
+// Transports to properly encoded values - note that icc profiles does use big endian notation.
+
+// 1 2 3 4
+// 4 3 2 1
+
+cmsUInt32Number CMSEXPORT _cmsAdjustEndianess32(cmsUInt32Number DWord)
+{
+#ifndef CMS_USE_BIG_ENDIAN
+
+ cmsUInt8Number* pByte = (cmsUInt8Number*) &DWord;
+ cmsUInt8Number temp1;
+ cmsUInt8Number temp2;
+
+ temp1 = *pByte++;
+ temp2 = *pByte++;
+ *(pByte-1) = *pByte;
+ *pByte++ = temp2;
+ *(pByte-3) = *pByte;
+ *pByte = temp1;
+#endif
+ return DWord;
+}
+
+// 1 2 3 4 5 6 7 8
+// 8 7 6 5 4 3 2 1
+
+void CMSEXPORT _cmsAdjustEndianess64(cmsUInt64Number* Result, cmsUInt64Number* QWord)
+{
+
+#ifndef CMS_USE_BIG_ENDIAN
+
+ cmsUInt8Number* pIn = (cmsUInt8Number*) QWord;
+ cmsUInt8Number* pOut = (cmsUInt8Number*) Result;
+
+ _cmsAssert(Result != NULL);
+
+ pOut[7] = pIn[0];
+ pOut[6] = pIn[1];
+ pOut[5] = pIn[2];
+ pOut[4] = pIn[3];
+ pOut[3] = pIn[4];
+ pOut[2] = pIn[5];
+ pOut[1] = pIn[6];
+ pOut[0] = pIn[7];
+
+#else
+ _cmsAssert(Result != NULL);
+
+# ifdef CMS_DONT_USE_INT64
+ (*Result)[0] = QWord[0];
+ (*Result)[1] = QWord[1];
+# else
+ *Result = *QWord;
+# endif
+#endif
+}
+
+// Auxiliar -- read 8, 16 and 32-bit numbers
+cmsBool CMSEXPORT _cmsReadUInt8Number(cmsIOHANDLER* io, cmsUInt8Number* n)
+{
+ cmsUInt8Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsUInt8Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) *n = tmp;
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsReadUInt16Number(cmsIOHANDLER* io, cmsUInt16Number* n)
+{
+ cmsUInt16Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsUInt16Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) *n = _cmsAdjustEndianess16(tmp);
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsReadUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, cmsUInt16Number* Array)
+{
+ cmsUInt32Number i;
+
+ _cmsAssert(io != NULL);
+
+ for (i=0; i < n; i++) {
+
+ if (Array != NULL) {
+ if (!_cmsReadUInt16Number(io, Array + i)) return FALSE;
+ }
+ else {
+ if (!_cmsReadUInt16Number(io, NULL)) return FALSE;
+ }
+
+ }
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsReadUInt32Number(cmsIOHANDLER* io, cmsUInt32Number* n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsUInt32Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) *n = _cmsAdjustEndianess32(tmp);
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsReadFloat32Number(cmsIOHANDLER* io, cmsFloat32Number* n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsFloat32Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) {
+
+ tmp = _cmsAdjustEndianess32(tmp);
+ *n = *(cmsFloat32Number*) &tmp;
+ }
+ return TRUE;
+}
+
+
+cmsBool CMSEXPORT _cmsReadUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n)
+{
+ cmsUInt64Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsUInt64Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) _cmsAdjustEndianess64(n, &tmp);
+ return TRUE;
+}
+
+
+cmsBool CMSEXPORT _cmsRead15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number* n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &tmp, sizeof(cmsUInt32Number), 1) != 1)
+ return FALSE;
+
+ if (n != NULL) {
+ *n = _cms15Fixed16toDouble(_cmsAdjustEndianess32(tmp));
+ }
+
+ return TRUE;
+}
+
+
+// Jun-21-2000: Some profiles (those that comes with W2K) comes
+// with the media white (media black?) x 100. Add a sanity check
+
+static
+void NormalizeXYZ(cmsCIEXYZ* Dest)
+{
+ while (Dest -> X > 2. &&
+ Dest -> Y > 2. &&
+ Dest -> Z > 2.) {
+
+ Dest -> X /= 10.;
+ Dest -> Y /= 10.;
+ Dest -> Z /= 10.;
+ }
+}
+
+cmsBool CMSEXPORT _cmsReadXYZNumber(cmsIOHANDLER* io, cmsCIEXYZ* XYZ)
+{
+ cmsEncodedXYZNumber xyz;
+
+ _cmsAssert(io != NULL);
+
+ if (io ->Read(io, &xyz, sizeof(cmsEncodedXYZNumber), 1) != 1) return FALSE;
+
+ if (XYZ != NULL) {
+
+ XYZ->X = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.X));
+ XYZ->Y = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.Y));
+ XYZ->Z = _cms15Fixed16toDouble(_cmsAdjustEndianess32(xyz.Z));
+
+ NormalizeXYZ(XYZ);
+ }
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteUInt8Number(cmsIOHANDLER* io, cmsUInt8Number n)
+{
+ _cmsAssert(io != NULL);
+
+ if (io -> Write(io, sizeof(cmsUInt8Number), &n) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteUInt16Number(cmsIOHANDLER* io, cmsUInt16Number n)
+{
+ cmsUInt16Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ tmp = _cmsAdjustEndianess16(n);
+ if (io -> Write(io, sizeof(cmsUInt16Number), &tmp) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteUInt16Array(cmsIOHANDLER* io, cmsUInt32Number n, const cmsUInt16Number* Array)
+{
+ cmsUInt32Number i;
+
+ _cmsAssert(io != NULL);
+ _cmsAssert(Array != NULL);
+
+ for (i=0; i < n; i++) {
+ if (!_cmsWriteUInt16Number(io, Array[i])) return FALSE;
+ }
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteUInt32Number(cmsIOHANDLER* io, cmsUInt32Number n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ tmp = _cmsAdjustEndianess32(n);
+ if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+
+cmsBool CMSEXPORT _cmsWriteFloat32Number(cmsIOHANDLER* io, cmsFloat32Number n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ tmp = *(cmsUInt32Number*) &n;
+ tmp = _cmsAdjustEndianess32(tmp);
+ if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteUInt64Number(cmsIOHANDLER* io, cmsUInt64Number* n)
+{
+ cmsUInt64Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ _cmsAdjustEndianess64(&tmp, n);
+ if (io -> Write(io, sizeof(cmsUInt64Number), &tmp) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWrite15Fixed16Number(cmsIOHANDLER* io, cmsFloat64Number n)
+{
+ cmsUInt32Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ tmp = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(n));
+ if (io -> Write(io, sizeof(cmsUInt32Number), &tmp) != 1)
+ return FALSE;
+
+ return TRUE;
+}
+
+cmsBool CMSEXPORT _cmsWriteXYZNumber(cmsIOHANDLER* io, const cmsCIEXYZ* XYZ)
+{
+ cmsEncodedXYZNumber xyz;
+
+ _cmsAssert(io != NULL);
+ _cmsAssert(XYZ != NULL);
+
+ xyz.X = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->X));
+ xyz.Y = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->Y));
+ xyz.Z = _cmsAdjustEndianess32(_cmsDoubleTo15Fixed16(XYZ->Z));
+
+ return io -> Write(io, sizeof(cmsEncodedXYZNumber), &xyz);
+}
+
+// from Fixed point 8.8 to double
+cmsFloat64Number CMSEXPORT _cms8Fixed8toDouble(cmsUInt16Number fixed8)
+{
+ cmsUInt8Number msb, lsb;
+
+ lsb = (cmsUInt8Number) (fixed8 & 0xff);
+ msb = (cmsUInt8Number) (((cmsUInt16Number) fixed8 >> 8) & 0xff);
+
+ return (cmsFloat64Number) ((cmsFloat64Number) msb + ((cmsFloat64Number) lsb / 256.0));
+}
+
+cmsUInt16Number CMSEXPORT _cmsDoubleTo8Fixed8(cmsFloat64Number val)
+{
+ cmsS15Fixed16Number GammaFixed32 = _cmsDoubleTo15Fixed16(val);
+ return (cmsUInt16Number) ((GammaFixed32 >> 8) & 0xFFFF);
+}
+
+// from Fixed point 15.16 to double
+cmsFloat64Number CMSEXPORT _cms15Fixed16toDouble(cmsS15Fixed16Number fix32)
+{
+ cmsFloat64Number floater, sign, mid;
+ int Whole, FracPart;
+
+ sign = (fix32 < 0 ? -1 : 1);
+ fix32 = abs(fix32);
+
+ Whole = (cmsUInt16Number)(fix32 >> 16) & 0xffff;
+ FracPart = (cmsUInt16Number)(fix32 & 0xffff);
+
+ mid = (cmsFloat64Number) FracPart / 65536.0;
+ floater = (cmsFloat64Number) Whole + mid;
+
+ return sign * floater;
+}
+
+// from double to Fixed point 15.16
+cmsS15Fixed16Number CMSEXPORT _cmsDoubleTo15Fixed16(cmsFloat64Number v)
+{
+ return ((cmsS15Fixed16Number) floor((v)*65536.0 + 0.5));
+}
+
+// Date/Time functions
+
+void CMSEXPORT _cmsDecodeDateTimeNumber(const cmsDateTimeNumber *Source, struct tm *Dest)
+{
+
+ _cmsAssert(Dest != NULL);
+ _cmsAssert(Source != NULL);
+
+ Dest->tm_sec = _cmsAdjustEndianess16(Source->seconds);
+ Dest->tm_min = _cmsAdjustEndianess16(Source->minutes);
+ Dest->tm_hour = _cmsAdjustEndianess16(Source->hours);
+ Dest->tm_mday = _cmsAdjustEndianess16(Source->day);
+ Dest->tm_mon = _cmsAdjustEndianess16(Source->month) - 1;
+ Dest->tm_year = _cmsAdjustEndianess16(Source->year) - 1900;
+ Dest->tm_wday = -1;
+ Dest->tm_yday = -1;
+ Dest->tm_isdst = 0;
+}
+
+void CMSEXPORT _cmsEncodeDateTimeNumber(cmsDateTimeNumber *Dest, const struct tm *Source)
+{
+ _cmsAssert(Dest != NULL);
+ _cmsAssert(Source != NULL);
+
+ Dest->seconds = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_sec);
+ Dest->minutes = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_min);
+ Dest->hours = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_hour);
+ Dest->day = _cmsAdjustEndianess16((cmsUInt16Number) Source->tm_mday);
+ Dest->month = _cmsAdjustEndianess16((cmsUInt16Number) (Source->tm_mon + 1));
+ Dest->year = _cmsAdjustEndianess16((cmsUInt16Number) (Source->tm_year + 1900));
+}
+
+// Read base and return type base
+cmsTagTypeSignature CMSEXPORT _cmsReadTypeBase(cmsIOHANDLER* io)
+{
+ _cmsTagBase Base;
+
+ _cmsAssert(io != NULL);
+
+ if (io -> Read(io, &Base, sizeof(_cmsTagBase), 1) != 1)
+ return (cmsTagTypeSignature) 0;
+
+ return (cmsTagTypeSignature) _cmsAdjustEndianess32(Base.sig);
+}
+
+// Setup base marker
+cmsBool CMSEXPORT _cmsWriteTypeBase(cmsIOHANDLER* io, cmsTagTypeSignature sig)
+{
+ _cmsTagBase Base;
+
+ _cmsAssert(io != NULL);
+
+ Base.sig = (cmsTagTypeSignature) _cmsAdjustEndianess32(sig);
+ memset(&Base.reserved, 0, sizeof(Base.reserved));
+ return io -> Write(io, sizeof(_cmsTagBase), &Base);
+}
+
+cmsBool CMSEXPORT _cmsReadAlignment(cmsIOHANDLER* io)
+{
+ cmsUInt8Number Buffer[4];
+ cmsUInt32Number NextAligned, At;
+ cmsUInt32Number BytesToNextAlignedPos;
+
+ _cmsAssert(io != NULL);
+
+ At = io -> Tell(io);
+ NextAligned = _cmsALIGNLONG(At);
+ BytesToNextAlignedPos = NextAligned - At;
+ if (BytesToNextAlignedPos == 0) return TRUE;
+ if (BytesToNextAlignedPos > 4) return FALSE;
+
+ return (io ->Read(io, Buffer, BytesToNextAlignedPos, 1) == 1);
+}
+
+cmsBool CMSEXPORT _cmsWriteAlignment(cmsIOHANDLER* io)
+{
+ cmsUInt8Number Buffer[4];
+ cmsUInt32Number NextAligned, At;
+ cmsUInt32Number BytesToNextAlignedPos;
+
+ _cmsAssert(io != NULL);
+
+ At = io -> Tell(io);
+ NextAligned = _cmsALIGNLONG(At);
+ BytesToNextAlignedPos = NextAligned - At;
+ if (BytesToNextAlignedPos == 0) return TRUE;
+ if (BytesToNextAlignedPos > 4) return FALSE;
+
+ memset(Buffer, 0, BytesToNextAlignedPos);
+ return io -> Write(io, BytesToNextAlignedPos, Buffer);
+}
+
+
+// To deal with text streams. 2K at most
+cmsBool CMSEXPORT _cmsIOPrintf(cmsIOHANDLER* io, const char* frm, ...)
+{
+ va_list args;
+ int len;
+ cmsUInt8Number Buffer[2048];
+ cmsBool rc;
+
+ _cmsAssert(io != NULL);
+ _cmsAssert(frm != NULL);
+
+ va_start(args, frm);
+
+ len = vsnprintf((char*) Buffer, 2047, frm, args);
+ if (len < 0) return FALSE; // Truncated, which is a fatal error for us
+
+ rc = io ->Write(io, len, Buffer);
+
+ va_end(args);
+
+ return rc;
+}
+
+
+// Plugin memory management -------------------------------------------------------------------------------------------------
+
+// Specialized malloc for plug-ins, that is freed upon exit.
+void* _cmsPluginMalloc(cmsContext ContextID, cmsUInt32Number size)
+{
+ struct _cmsContext_struct* ctx = _cmsGetContext(ContextID);
+
+ if (ctx ->MemPool == NULL) {
+
+ if (ContextID == NULL) {
+
+ ctx->MemPool = _cmsCreateSubAlloc(0, 2*1024);
+ }
+ else {
+ cmsSignalError(ContextID, cmsERROR_CORRUPTION_DETECTED, "NULL memory pool on context");
+ return NULL;
+ }
+ }
+
+ return _cmsSubAlloc(ctx->MemPool, size);
+}
+
+
+// Main plug-in dispatcher
+cmsBool CMSEXPORT cmsPlugin(void* Plug_in)
+{
+ return cmsPluginTHR(NULL, Plug_in);
+}
+
+cmsBool CMSEXPORT cmsPluginTHR(cmsContext id, void* Plug_in)
+{
+ cmsPluginBase* Plugin;
+
+ for (Plugin = (cmsPluginBase*) Plug_in;
+ Plugin != NULL;
+ Plugin = Plugin -> Next) {
+
+ if (Plugin -> Magic != cmsPluginMagicNumber) {
+ cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "Unrecognized plugin");
+ return FALSE;
+ }
+
+ if (Plugin ->ExpectedVersion > LCMS_VERSION) {
+ cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "plugin needs Little CMS %d, current version is %d",
+ Plugin ->ExpectedVersion, LCMS_VERSION);
+ return FALSE;
+ }
+
+ switch (Plugin -> Type) {
+
+ case cmsPluginMemHandlerSig:
+ if (!_cmsRegisterMemHandlerPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginInterpolationSig:
+ if (!_cmsRegisterInterpPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginTagTypeSig:
+ if (!_cmsRegisterTagTypePlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginTagSig:
+ if (!_cmsRegisterTagPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginFormattersSig:
+ if (!_cmsRegisterFormattersPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginRenderingIntentSig:
+ if (!_cmsRegisterRenderingIntentPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginParametricCurveSig:
+ if (!_cmsRegisterParametricCurvesPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginMultiProcessElementSig:
+ if (!_cmsRegisterMultiProcessElementPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginOptimizationSig:
+ if (!_cmsRegisterOptimizationPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginTransformSig:
+ if (!_cmsRegisterTransformPlugin(id, Plugin)) return FALSE;
+ break;
+
+ case cmsPluginMutexSig:
+ if (!_cmsRegisterMutexPlugin(id, Plugin)) return FALSE;
+ break;
+
+ default:
+ cmsSignalError(id, cmsERROR_UNKNOWN_EXTENSION, "Unrecognized plugin type '%X'", Plugin -> Type);
+ return FALSE;
+ }
+ }
+
+ // Keep a reference to the plug-in
+ return TRUE;
+}
+
+
+// Revert all plug-ins to default
+void CMSEXPORT cmsUnregisterPlugins(void)
+{
+ cmsUnregisterPluginsTHR(NULL);
+}
+
+
+// The Global storage for system context. This is the one and only global variable
+// pointers structure. All global vars are referenced here.
+static struct _cmsContext_struct globalContext = {
+
+ NULL, // Not in the linked list
+ NULL, // No suballocator
+ {
+ NULL, // UserPtr,
+ &_cmsLogErrorChunk, // Logger,
+ &_cmsAlarmCodesChunk, // AlarmCodes,
+ &_cmsAdaptationStateChunk, // AdaptationState,
+ &_cmsMemPluginChunk, // MemPlugin,
+ &_cmsInterpPluginChunk, // InterpPlugin,
+ &_cmsCurvesPluginChunk, // CurvesPlugin,
+ &_cmsFormattersPluginChunk, // FormattersPlugin,
+ &_cmsTagTypePluginChunk, // TagTypePlugin,
+ &_cmsTagPluginChunk, // TagPlugin,
+ &_cmsIntentsPluginChunk, // IntentPlugin,
+ &_cmsMPETypePluginChunk, // MPEPlugin,
+ &_cmsOptimizationPluginChunk, // OptimizationPlugin,
+ &_cmsTransformPluginChunk, // TransformPlugin,
+ &_cmsMutexPluginChunk // MutexPlugin
+ },
+
+ { NULL, NULL, NULL, NULL, NULL, NULL } // The default memory allocator is not used for context 0
+};
+
+
+// The context pool (linked list head)
+static _cmsMutex _cmsContextPoolHeadMutex = CMS_MUTEX_INITIALIZER;
+static struct _cmsContext_struct* _cmsContextPoolHead = NULL;
+
+// Internal, get associated pointer, with guessing. Never returns NULL.
+struct _cmsContext_struct* _cmsGetContext(cmsContext ContextID)
+{
+ struct _cmsContext_struct* id = (struct _cmsContext_struct*) ContextID;
+ struct _cmsContext_struct* ctx;
+
+
+ // On 0, use global settings
+ if (id == NULL)
+ return &globalContext;
+
+ // Search
+ for (ctx = _cmsContextPoolHead;
+ ctx != NULL;
+ ctx = ctx ->Next) {
+
+ // Found it?
+ if (id == ctx)
+ return ctx; // New-style context,
+ }
+
+ return &globalContext;
+}
+
+
+// Internal: get the memory area associanted with each context client
+// Returns the block assigned to the specific zone.
+void* _cmsContextGetClientChunk(cmsContext ContextID, _cmsMemoryClient mc)
+{
+ struct _cmsContext_struct* ctx;
+ void *ptr;
+
+ if (mc < 0 || mc >= MemoryClientMax) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Bad context client");
+ return NULL;
+ }
+
+ ctx = _cmsGetContext(ContextID);
+ ptr = ctx ->chunks[mc];
+
+ if (ptr != NULL)
+ return ptr;
+
+ // A null ptr means no special settings for that context, and this
+ // reverts to Context0 globals
+ return globalContext.chunks[mc];
+}
+
+
+// This function returns the given context its default pristine state,
+// as no plug-ins were declared. There is no way to unregister a single
+// plug-in, as a single call to cmsPluginTHR() function may register
+// many different plug-ins simultaneously, then there is no way to
+// identify which plug-in to unregister.
+void CMSEXPORT cmsUnregisterPluginsTHR(cmsContext ContextID)
+{
+ _cmsRegisterMemHandlerPlugin(ContextID, NULL);
+ _cmsRegisterInterpPlugin(ContextID, NULL);
+ _cmsRegisterTagTypePlugin(ContextID, NULL);
+ _cmsRegisterTagPlugin(ContextID, NULL);
+ _cmsRegisterFormattersPlugin(ContextID, NULL);
+ _cmsRegisterRenderingIntentPlugin(ContextID, NULL);
+ _cmsRegisterParametricCurvesPlugin(ContextID, NULL);
+ _cmsRegisterMultiProcessElementPlugin(ContextID, NULL);
+ _cmsRegisterOptimizationPlugin(ContextID, NULL);
+ _cmsRegisterTransformPlugin(ContextID, NULL);
+ _cmsRegisterMutexPlugin(ContextID, NULL);
+}
+
+
+// Returns the memory manager plug-in, if any, from the Plug-in bundle
+static
+cmsPluginMemHandler* _cmsFindMemoryPlugin(void* PluginBundle)
+{
+ cmsPluginBase* Plugin;
+
+ for (Plugin = (cmsPluginBase*) PluginBundle;
+ Plugin != NULL;
+ Plugin = Plugin -> Next) {
+
+ if (Plugin -> Magic == cmsPluginMagicNumber &&
+ Plugin -> ExpectedVersion <= LCMS_VERSION &&
+ Plugin -> Type == cmsPluginMemHandlerSig) {
+
+ // Found!
+ return (cmsPluginMemHandler*) Plugin;
+ }
+ }
+
+ // Nope, revert to defaults
+ return NULL;
+}
+
+
+// Creates a new context with optional associated plug-ins. Caller may also specify an optional pointer to user-defined
+// data that will be forwarded to plug-ins and logger.
+cmsContext CMSEXPORT cmsCreateContext(void* Plugin, void* UserData)
+{
+ struct _cmsContext_struct* ctx;
+ struct _cmsContext_struct fakeContext;
+
+ _cmsInstallAllocFunctions(_cmsFindMemoryPlugin(Plugin), &fakeContext.DefaultMemoryManager);
+
+ fakeContext.chunks[UserPtr] = UserData;
+ fakeContext.chunks[MemPlugin] = &fakeContext.DefaultMemoryManager;
+
+ // Create the context structure.
+ ctx = (struct _cmsContext_struct*) _cmsMalloc(&fakeContext, sizeof(struct _cmsContext_struct));
+ if (ctx == NULL)
+ return NULL; // Something very wrong happened!
+
+ // Init the structure and the memory manager
+ memset(ctx, 0, sizeof(struct _cmsContext_struct));
+
+ // Keep memory manager
+ memcpy(&ctx->DefaultMemoryManager, &fakeContext.DefaultMemoryManager, sizeof(_cmsMemPluginChunk));
+
+ // Maintain the linked list (with proper locking)
+ _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+ ctx ->Next = _cmsContextPoolHead;
+ _cmsContextPoolHead = ctx;
+ _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+
+ ctx ->chunks[UserPtr] = UserData;
+ ctx ->chunks[MemPlugin] = &ctx->DefaultMemoryManager;
+
+ // Now we can allocate the pool by using default memory manager
+ ctx ->MemPool = _cmsCreateSubAlloc(ctx, 22 * sizeof(void*)); // default size about 32 pointers
+ if (ctx ->MemPool == NULL) {
+
+ cmsDeleteContext(ctx);
+ return NULL;
+ }
+
+ _cmsAllocLogErrorChunk(ctx, NULL);
+ _cmsAllocAlarmCodesChunk(ctx, NULL);
+ _cmsAllocAdaptationStateChunk(ctx, NULL);
+ _cmsAllocMemPluginChunk(ctx, NULL);
+ _cmsAllocInterpPluginChunk(ctx, NULL);
+ _cmsAllocCurvesPluginChunk(ctx, NULL);
+ _cmsAllocFormattersPluginChunk(ctx, NULL);
+ _cmsAllocTagTypePluginChunk(ctx, NULL);
+ _cmsAllocMPETypePluginChunk(ctx, NULL);
+ _cmsAllocTagPluginChunk(ctx, NULL);
+ _cmsAllocIntentsPluginChunk(ctx, NULL);
+ _cmsAllocOptimizationPluginChunk(ctx, NULL);
+ _cmsAllocTransformPluginChunk(ctx, NULL);
+ _cmsAllocMutexPluginChunk(ctx, NULL);
+
+ // Setup the plug-ins
+ if (!cmsPluginTHR(ctx, Plugin)) {
+
+ cmsDeleteContext(ctx);
+ return NULL;
+ }
+
+ return (cmsContext) ctx;
+}
+
+// Duplicates a context with all associated plug-ins.
+// Caller may specify an optional pointer to user-defined
+// data that will be forwarded to plug-ins and logger.
+cmsContext CMSEXPORT cmsDupContext(cmsContext ContextID, void* NewUserData)
+{
+ int i;
+ struct _cmsContext_struct* ctx;
+ const struct _cmsContext_struct* src = _cmsGetContext(ContextID);
+
+ void* userData = (NewUserData != NULL) ? NewUserData : src -> chunks[UserPtr];
+
+
+ ctx = (struct _cmsContext_struct*) _cmsMalloc(ContextID, sizeof(struct _cmsContext_struct));
+ if (ctx == NULL)
+ return NULL; // Something very wrong happened
+
+ // Setup default memory allocators
+ memcpy(&ctx->DefaultMemoryManager, &src->DefaultMemoryManager, sizeof(ctx->DefaultMemoryManager));
+
+ // Maintain the linked list
+ _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+ ctx ->Next = _cmsContextPoolHead;
+ _cmsContextPoolHead = ctx;
+ _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+
+ ctx ->chunks[UserPtr] = userData;
+ ctx ->chunks[MemPlugin] = &ctx->DefaultMemoryManager;
+
+ ctx ->MemPool = _cmsCreateSubAlloc(ctx, 22 * sizeof(void*));
+ if (ctx ->MemPool == NULL) {
+
+ cmsDeleteContext(ctx);
+ return NULL;
+ }
+
+ // Allocate all required chunks.
+ _cmsAllocLogErrorChunk(ctx, src);
+ _cmsAllocAlarmCodesChunk(ctx, src);
+ _cmsAllocAdaptationStateChunk(ctx, src);
+ _cmsAllocMemPluginChunk(ctx, src);
+ _cmsAllocInterpPluginChunk(ctx, src);
+ _cmsAllocCurvesPluginChunk(ctx, src);
+ _cmsAllocFormattersPluginChunk(ctx, src);
+ _cmsAllocTagTypePluginChunk(ctx, src);
+ _cmsAllocMPETypePluginChunk(ctx, src);
+ _cmsAllocTagPluginChunk(ctx, src);
+ _cmsAllocIntentsPluginChunk(ctx, src);
+ _cmsAllocOptimizationPluginChunk(ctx, src);
+ _cmsAllocTransformPluginChunk(ctx, src);
+ _cmsAllocMutexPluginChunk(ctx, src);
+
+ // Make sure no one failed
+ for (i=Logger; i < MemoryClientMax; i++) {
+
+ if (src ->chunks[i] == NULL) {
+ cmsDeleteContext((cmsContext) ctx);
+ return NULL;
+ }
+ }
+
+ return (cmsContext) ctx;
+}
+
+
+
+static
+struct _cmsContext_struct* FindPrev(struct _cmsContext_struct* id)
+{
+ struct _cmsContext_struct* prev;
+
+ // Search for previous
+ for (prev = _cmsContextPoolHead;
+ prev != NULL;
+ prev = prev ->Next)
+ {
+ if (prev ->Next == id)
+ return prev;
+ }
+
+ return NULL; // List is empty or only one element!
+}
+
+// Frees any resources associated with the given context,
+// and destroys the context placeholder.
+// The ContextID can no longer be used in any THR operation.
+void CMSEXPORT cmsDeleteContext(cmsContext ContextID)
+{
+ if (ContextID != NULL) {
+
+ struct _cmsContext_struct* ctx = (struct _cmsContext_struct*) ContextID;
+ struct _cmsContext_struct fakeContext;
+ struct _cmsContext_struct* prev;
+
+ memcpy(&fakeContext.DefaultMemoryManager, &ctx->DefaultMemoryManager, sizeof(ctx->DefaultMemoryManager));
+
+ fakeContext.chunks[UserPtr] = ctx ->chunks[UserPtr];
+ fakeContext.chunks[MemPlugin] = &fakeContext.DefaultMemoryManager;
+
+ // Get rid of plugins
+ cmsUnregisterPluginsTHR(ContextID);
+
+ // Since all memory is allocated in the private pool, all what we need to do is destroy the pool
+ if (ctx -> MemPool != NULL)
+ _cmsSubAllocDestroy(ctx ->MemPool);
+ ctx -> MemPool = NULL;
+
+ // Maintain list
+ _cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+ if (_cmsContextPoolHead == ctx) {
+
+ _cmsContextPoolHead = ctx->Next;
+ }
+ else {
+
+ // Search for previous
+ for (prev = _cmsContextPoolHead;
+ prev != NULL;
+ prev = prev ->Next)
+ {
+ if (prev -> Next == ctx) {
+ prev -> Next = ctx ->Next;
+ break;
+ }
+ }
+ }
+ _cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
+
+ // free the memory block itself
+ _cmsFree(&fakeContext, ctx);
+ }
+}
+
+// Returns the user data associated to the given ContextID, or NULL if no user data was attached on context creation
+void* CMSEXPORT cmsGetContextUserData(cmsContext ContextID)
+{
+ return _cmsContextGetClientChunk(ContextID, UserPtr);
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsps2.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsps2.c
new file mode 100644
index 0000000000..224b44b542
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsps2.c
@@ -0,0 +1,1597 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2011 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"
+
+// PostScript ColorRenderingDictionary and ColorSpaceArray
+
+
+#define MAXPSCOLS 60 // Columns on tables
+
+/*
+ Implementation
+ --------------
+
+ PostScript does use XYZ as its internal PCS. But since PostScript
+ interpolation tables are limited to 8 bits, I use Lab as a way to
+ improve the accuracy, favoring perceptual results. So, for the creation
+ of each CRD, CSA the profiles are converted to Lab via a device
+ link between profile -> Lab or Lab -> profile. The PS code necessary to
+ convert Lab <-> XYZ is also included.
+
+
+
+ Color Space Arrays (CSA)
+ ==================================================================================
+
+ In order to obtain precision, code chooses between three ways to implement
+ the device -> XYZ transform. These cases identifies monochrome profiles (often
+ implemented as a set of curves), matrix-shaper and Pipeline-based.
+
+ Monochrome
+ -----------
+
+ This is implemented as /CIEBasedA CSA. The prelinearization curve is
+ placed into /DecodeA section, and matrix equals to D50. Since here is
+ no interpolation tables, I do the conversion directly to XYZ
+
+ NOTE: CLUT-based monochrome profiles are NOT supported. So, cmsFLAGS_MATRIXINPUT
+ flag is forced on such profiles.
+
+ [ /CIEBasedA
+ <<
+ /DecodeA { transfer function } bind
+ /MatrixA [D50]
+ /RangeLMN [ 0.0 cmsD50X 0.0 cmsD50Y 0.0 cmsD50Z ]
+ /WhitePoint [D50]
+ /BlackPoint [BP]
+ /RenderingIntent (intent)
+ >>
+ ]
+
+ On simpler profiles, the PCS is already XYZ, so no conversion is required.
+
+
+ Matrix-shaper based
+ -------------------
+
+ This is implemented both with /CIEBasedABC or /CIEBasedDEF on dependig
+ of profile implementation. Since here there are no interpolation tables, I do
+ the conversion directly to XYZ
+
+
+
+ [ /CIEBasedABC
+ <<
+ /DecodeABC [ {transfer1} {transfer2} {transfer3} ]
+ /MatrixABC [Matrix]
+ /RangeLMN [ 0.0 cmsD50X 0.0 cmsD50Y 0.0 cmsD50Z ]
+ /DecodeLMN [ { / 2} dup dup ]
+ /WhitePoint [D50]
+ /BlackPoint [BP]
+ /RenderingIntent (intent)
+ >>
+ ]
+
+
+ CLUT based
+ ----------
+
+ Lab is used in such cases.
+
+ [ /CIEBasedDEF
+ <<
+ /DecodeDEF [ <prelinearization> ]
+ /Table [ p p p [<...>]]
+ /RangeABC [ 0 1 0 1 0 1]
+ /DecodeABC[ <postlinearization> ]
+ /RangeLMN [ -0.236 1.254 0 1 -0.635 1.640 ]
+ % -128/500 1+127/500 0 1 -127/200 1+128/200
+ /MatrixABC [ 1 1 1 1 0 0 0 0 -1]
+ /WhitePoint [D50]
+ /BlackPoint [BP]
+ /RenderingIntent (intent)
+ ]
+
+
+ Color Rendering Dictionaries (CRD)
+ ==================================
+ These are always implemented as CLUT, and always are using Lab. Since CRD are expected to
+ be used as resources, the code adds the definition as well.
+
+ <<
+ /ColorRenderingType 1
+ /WhitePoint [ D50 ]
+ /BlackPoint [BP]
+ /MatrixPQR [ Bradford ]
+ /RangePQR [-0.125 1.375 -0.125 1.375 -0.125 1.375 ]
+ /TransformPQR [
+ {4 index 3 get div 2 index 3 get mul exch pop exch pop exch pop exch pop } bind
+ {4 index 4 get div 2 index 4 get mul exch pop exch pop exch pop exch pop } bind
+ {4 index 5 get div 2 index 5 get mul exch pop exch pop exch pop exch pop } bind
+ ]
+ /MatrixABC <...>
+ /EncodeABC <...>
+ /RangeABC <.. used for XYZ -> Lab>
+ /EncodeLMN
+ /RenderTable [ p p p [<...>]]
+
+ /RenderingIntent (Perceptual)
+ >>
+ /Current exch /ColorRendering defineresource pop
+
+
+ The following stages are used to convert from XYZ to Lab
+ --------------------------------------------------------
+
+ Input is given at LMN stage on X, Y, Z
+
+ Encode LMN gives us f(X/Xn), f(Y/Yn), f(Z/Zn)
+
+ /EncodeLMN [
+
+ { 0.964200 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
+ { 1.000000 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
+ { 0.824900 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
+
+ ]
+
+
+ MatrixABC is used to compute f(Y/Yn), f(X/Xn) - f(Y/Yn), f(Y/Yn) - f(Z/Zn)
+
+ | 0 1 0|
+ | 1 -1 0|
+ | 0 1 -1|
+
+ /MatrixABC [ 0 1 0 1 -1 1 0 0 -1 ]
+
+ EncodeABC finally gives Lab values.
+
+ /EncodeABC [
+ { 116 mul 16 sub 100 div } bind
+ { 500 mul 128 add 255 div } bind
+ { 200 mul 128 add 255 div } bind
+ ]
+
+ The following stages are used to convert Lab to XYZ
+ ----------------------------------------------------
+
+ /RangeABC [ 0 1 0 1 0 1]
+ /DecodeABC [ { 100 mul 16 add 116 div } bind
+ { 255 mul 128 sub 500 div } bind
+ { 255 mul 128 sub 200 div } bind
+ ]
+
+ /MatrixABC [ 1 1 1 1 0 0 0 0 -1]
+ /DecodeLMN [
+ {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.964200 mul} bind
+ {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse } bind
+ {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.824900 mul} bind
+ ]
+
+
+*/
+
+/*
+
+ PostScript algorithms discussion.
+ =========================================================================================================
+
+ 1D interpolation algorithm
+
+
+ 1D interpolation (float)
+ ------------------------
+
+ val2 = Domain * Value;
+
+ cell0 = (int) floor(val2);
+ cell1 = (int) ceil(val2);
+
+ rest = val2 - cell0;
+
+ y0 = LutTable[cell0] ;
+ y1 = LutTable[cell1] ;
+
+ y = y0 + (y1 - y0) * rest;
+
+
+
+ PostScript code Stack
+ ================================================
+
+ { % v
+ <check 0..1.0>
+ [array] % v tab
+ dup % v tab tab
+ length 1 sub % v tab dom
+
+ 3 -1 roll % tab dom v
+
+ mul % tab val2
+ dup % tab val2 val2
+ dup % tab val2 val2 val2
+ floor cvi % tab val2 val2 cell0
+ exch % tab val2 cell0 val2
+ ceiling cvi % tab val2 cell0 cell1
+
+ 3 index % tab val2 cell0 cell1 tab
+ exch % tab val2 cell0 tab cell1
+ get % tab val2 cell0 y1
+
+ 4 -1 roll % val2 cell0 y1 tab
+ 3 -1 roll % val2 y1 tab cell0
+ get % val2 y1 y0
+
+ dup % val2 y1 y0 y0
+ 3 1 roll % val2 y0 y1 y0
+
+ sub % val2 y0 (y1-y0)
+ 3 -1 roll % y0 (y1-y0) val2
+ dup % y0 (y1-y0) val2 val2
+ floor cvi % y0 (y1-y0) val2 floor(val2)
+ sub % y0 (y1-y0) rest
+ mul % y0 t1
+ add % y
+ 65535 div % result
+
+ } bind
+
+
+*/
+
+
+// This struct holds the memory block currently being write
+typedef struct {
+ _cmsStageCLutData* Pipeline;
+ cmsIOHANDLER* m;
+
+ int FirstComponent;
+ int SecondComponent;
+
+ const char* PreMaj;
+ const char* PostMaj;
+ const char* PreMin;
+ const char* PostMin;
+
+ int FixWhite; // Force mapping of pure white
+
+ cmsColorSpaceSignature ColorSpace; // ColorSpace of profile
+
+
+} cmsPsSamplerCargo;
+
+static int _cmsPSActualColumn = 0;
+
+
+// Convert to byte
+static
+cmsUInt8Number Word2Byte(cmsUInt16Number w)
+{
+ return (cmsUInt8Number) floor((cmsFloat64Number) w / 257.0 + 0.5);
+}
+
+
+// Convert to byte (using ICC2 notation)
+/*
+static
+cmsUInt8Number L2Byte(cmsUInt16Number w)
+{
+ int ww = w + 0x0080;
+
+ if (ww > 0xFFFF) return 0xFF;
+
+ return (cmsUInt8Number) ((cmsUInt16Number) (ww >> 8) & 0xFF);
+}
+*/
+
+// Write a cooked byte
+
+static
+void WriteByte(cmsIOHANDLER* m, cmsUInt8Number b)
+{
+ _cmsIOPrintf(m, "%02x", b);
+ _cmsPSActualColumn += 2;
+
+ if (_cmsPSActualColumn > MAXPSCOLS) {
+
+ _cmsIOPrintf(m, "\n");
+ _cmsPSActualColumn = 0;
+ }
+}
+
+// ----------------------------------------------------------------- PostScript generation
+
+
+// Removes offending Carriage returns
+static
+char* RemoveCR(const char* txt)
+{
+ static char Buffer[2048];
+ char* pt;
+
+ strncpy(Buffer, txt, 2047);
+ Buffer[2047] = 0;
+ for (pt = Buffer; *pt; pt++)
+ if (*pt == '\n' || *pt == '\r') *pt = ' ';
+
+ return Buffer;
+
+}
+
+static
+void EmitHeader(cmsIOHANDLER* m, const char* Title, cmsHPROFILE hProfile)
+{
+ time_t timer;
+ cmsMLU *Description, *Copyright;
+ char DescASCII[256], CopyrightASCII[256];
+
+ time(&timer);
+
+ Description = (cmsMLU*) cmsReadTag(hProfile, cmsSigProfileDescriptionTag);
+ Copyright = (cmsMLU*) cmsReadTag(hProfile, cmsSigCopyrightTag);
+
+ DescASCII[0] = DescASCII[255] = 0;
+ CopyrightASCII[0] = CopyrightASCII[255] = 0;
+
+ if (Description != NULL) cmsMLUgetASCII(Description, cmsNoLanguage, cmsNoCountry, DescASCII, 255);
+ if (Copyright != NULL) cmsMLUgetASCII(Copyright, cmsNoLanguage, cmsNoCountry, CopyrightASCII, 255);
+
+ _cmsIOPrintf(m, "%%!PS-Adobe-3.0\n");
+ _cmsIOPrintf(m, "%%\n");
+ _cmsIOPrintf(m, "%% %s\n", Title);
+ _cmsIOPrintf(m, "%% Source: %s\n", RemoveCR(DescASCII));
+ _cmsIOPrintf(m, "%% %s\n", RemoveCR(CopyrightASCII));
+ _cmsIOPrintf(m, "%% Created: %s", ctime(&timer)); // ctime appends a \n!!!
+ _cmsIOPrintf(m, "%%\n");
+ _cmsIOPrintf(m, "%%%%BeginResource\n");
+
+}
+
+
+// Emits White & Black point. White point is always D50, Black point is the device
+// Black point adapted to D50.
+
+static
+void EmitWhiteBlackD50(cmsIOHANDLER* m, cmsCIEXYZ* BlackPoint)
+{
+
+ _cmsIOPrintf(m, "/BlackPoint [%f %f %f]\n", BlackPoint -> X,
+ BlackPoint -> Y,
+ BlackPoint -> Z);
+
+ _cmsIOPrintf(m, "/WhitePoint [%f %f %f]\n", cmsD50_XYZ()->X,
+ cmsD50_XYZ()->Y,
+ cmsD50_XYZ()->Z);
+}
+
+
+static
+void EmitRangeCheck(cmsIOHANDLER* m)
+{
+ _cmsIOPrintf(m, "dup 0.0 lt { pop 0.0 } if "
+ "dup 1.0 gt { pop 1.0 } if ");
+
+}
+
+// Does write the intent
+
+static
+void EmitIntent(cmsIOHANDLER* m, int RenderingIntent)
+{
+ const char *intent;
+
+ switch (RenderingIntent) {
+
+ case INTENT_PERCEPTUAL: intent = "Perceptual"; break;
+ case INTENT_RELATIVE_COLORIMETRIC: intent = "RelativeColorimetric"; break;
+ case INTENT_ABSOLUTE_COLORIMETRIC: intent = "AbsoluteColorimetric"; break;
+ case INTENT_SATURATION: intent = "Saturation"; break;
+
+ default: intent = "Undefined"; break;
+ }
+
+ _cmsIOPrintf(m, "/RenderingIntent (%s)\n", intent );
+}
+
+//
+// Convert L* to Y
+//
+// Y = Yn*[ (L* + 16) / 116] ^ 3 if (L*) >= 6 / 29
+// = Yn*( L* / 116) / 7.787 if (L*) < 6 / 29
+//
+
+/*
+static
+void EmitL2Y(cmsIOHANDLER* m)
+{
+ _cmsIOPrintf(m,
+ "{ "
+ "100 mul 16 add 116 div " // (L * 100 + 16) / 116
+ "dup 6 29 div ge " // >= 6 / 29 ?
+ "{ dup dup mul mul } " // yes, ^3 and done
+ "{ 4 29 div sub 108 841 div mul } " // no, slope limiting
+ "ifelse } bind ");
+}
+*/
+
+
+// Lab -> XYZ, see the discussion above
+
+static
+void EmitLab2XYZ(cmsIOHANDLER* m)
+{
+ _cmsIOPrintf(m, "/RangeABC [ 0 1 0 1 0 1]\n");
+ _cmsIOPrintf(m, "/DecodeABC [\n");
+ _cmsIOPrintf(m, "{100 mul 16 add 116 div } bind\n");
+ _cmsIOPrintf(m, "{255 mul 128 sub 500 div } bind\n");
+ _cmsIOPrintf(m, "{255 mul 128 sub 200 div } bind\n");
+ _cmsIOPrintf(m, "]\n");
+ _cmsIOPrintf(m, "/MatrixABC [ 1 1 1 1 0 0 0 0 -1]\n");
+ _cmsIOPrintf(m, "/RangeLMN [ -0.236 1.254 0 1 -0.635 1.640 ]\n");
+ _cmsIOPrintf(m, "/DecodeLMN [\n");
+ _cmsIOPrintf(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.964200 mul} bind\n");
+ _cmsIOPrintf(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse } bind\n");
+ _cmsIOPrintf(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.824900 mul} bind\n");
+ _cmsIOPrintf(m, "]\n");
+}
+
+
+
+// Outputs a table of words. It does use 16 bits
+
+static
+void Emit1Gamma(cmsIOHANDLER* m, cmsToneCurve* Table)
+{
+ cmsUInt32Number i;
+ cmsFloat64Number gamma;
+
+ if (Table == NULL) return; // Error
+
+ if (Table ->nEntries <= 0) return; // Empty table
+
+ // Suppress whole if identity
+ if (cmsIsToneCurveLinear(Table)) return;
+
+ // Check if is really an exponential. If so, emit "exp"
+ gamma = cmsEstimateGamma(Table, 0.001);
+ if (gamma > 0) {
+ _cmsIOPrintf(m, "{ %g exp } bind ", gamma);
+ return;
+ }
+
+ _cmsIOPrintf(m, "{ ");
+
+ // Bounds check
+ EmitRangeCheck(m);
+
+ // Emit intepolation code
+
+ // PostScript code Stack
+ // =============== ========================
+ // v
+ _cmsIOPrintf(m, " [");
+
+ for (i=0; i < Table->nEntries; i++) {
+ _cmsIOPrintf(m, "%d ", Table->Table16[i]);
+ }
+
+ _cmsIOPrintf(m, "] "); // v tab
+
+ _cmsIOPrintf(m, "dup "); // v tab tab
+ _cmsIOPrintf(m, "length 1 sub "); // v tab dom
+ _cmsIOPrintf(m, "3 -1 roll "); // tab dom v
+ _cmsIOPrintf(m, "mul "); // tab val2
+ _cmsIOPrintf(m, "dup "); // tab val2 val2
+ _cmsIOPrintf(m, "dup "); // tab val2 val2 val2
+ _cmsIOPrintf(m, "floor cvi "); // tab val2 val2 cell0
+ _cmsIOPrintf(m, "exch "); // tab val2 cell0 val2
+ _cmsIOPrintf(m, "ceiling cvi "); // tab val2 cell0 cell1
+ _cmsIOPrintf(m, "3 index "); // tab val2 cell0 cell1 tab
+ _cmsIOPrintf(m, "exch "); // tab val2 cell0 tab cell1
+ _cmsIOPrintf(m, "get "); // tab val2 cell0 y1
+ _cmsIOPrintf(m, "4 -1 roll "); // val2 cell0 y1 tab
+ _cmsIOPrintf(m, "3 -1 roll "); // val2 y1 tab cell0
+ _cmsIOPrintf(m, "get "); // val2 y1 y0
+ _cmsIOPrintf(m, "dup "); // val2 y1 y0 y0
+ _cmsIOPrintf(m, "3 1 roll "); // val2 y0 y1 y0
+ _cmsIOPrintf(m, "sub "); // val2 y0 (y1-y0)
+ _cmsIOPrintf(m, "3 -1 roll "); // y0 (y1-y0) val2
+ _cmsIOPrintf(m, "dup "); // y0 (y1-y0) val2 val2
+ _cmsIOPrintf(m, "floor cvi "); // y0 (y1-y0) val2 floor(val2)
+ _cmsIOPrintf(m, "sub "); // y0 (y1-y0) rest
+ _cmsIOPrintf(m, "mul "); // y0 t1
+ _cmsIOPrintf(m, "add "); // y
+ _cmsIOPrintf(m, "65535 div "); // result
+
+ _cmsIOPrintf(m, " } bind ");
+}
+
+
+// Compare gamma table
+
+static
+cmsBool GammaTableEquals(cmsUInt16Number* g1, cmsUInt16Number* g2, int nEntries)
+{
+ return memcmp(g1, g2, nEntries* sizeof(cmsUInt16Number)) == 0;
+}
+
+
+// Does write a set of gamma curves
+
+static
+void EmitNGamma(cmsIOHANDLER* m, int n, cmsToneCurve* g[])
+{
+ int i;
+
+ for( i=0; i < n; i++ )
+ {
+ if (g[i] == NULL) return; // Error
+
+ if (i > 0 && GammaTableEquals(g[i-1]->Table16, g[i]->Table16, g[i]->nEntries)) {
+
+ _cmsIOPrintf(m, "dup ");
+ }
+ else {
+ Emit1Gamma(m, g[i]);
+ }
+ }
+
+}
+
+
+
+
+
+// Following code dumps a LUT onto memory stream
+
+
+// This is the sampler. Intended to work in SAMPLER_INSPECT mode,
+// that is, the callback will be called for each knot with
+//
+// In[] The grid location coordinates, normalized to 0..ffff
+// Out[] The Pipeline values, normalized to 0..ffff
+//
+// Returning a value other than 0 does terminate the sampling process
+//
+// Each row contains Pipeline values for all but first component. So, I
+// detect row changing by keeping a copy of last value of first
+// component. -1 is used to mark begining of whole block.
+
+static
+int OutputValueSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ cmsPsSamplerCargo* sc = (cmsPsSamplerCargo*) Cargo;
+ cmsUInt32Number i;
+
+
+ if (sc -> FixWhite) {
+
+ if (In[0] == 0xFFFF) { // Only in L* = 100, ab = [-8..8]
+
+ if ((In[1] >= 0x7800 && In[1] <= 0x8800) &&
+ (In[2] >= 0x7800 && In[2] <= 0x8800)) {
+
+ cmsUInt16Number* Black;
+ cmsUInt16Number* White;
+ cmsUInt32Number nOutputs;
+
+ if (!_cmsEndPointsBySpace(sc ->ColorSpace, &White, &Black, &nOutputs))
+ return 0;
+
+ for (i=0; i < nOutputs; i++)
+ Out[i] = White[i];
+ }
+
+
+ }
+ }
+
+
+ // Hadle the parenthesis on rows
+
+ if (In[0] != sc ->FirstComponent) {
+
+ if (sc ->FirstComponent != -1) {
+
+ _cmsIOPrintf(sc ->m, sc ->PostMin);
+ sc ->SecondComponent = -1;
+ _cmsIOPrintf(sc ->m, sc ->PostMaj);
+ }
+
+ // Begin block
+ _cmsPSActualColumn = 0;
+
+ _cmsIOPrintf(sc ->m, sc ->PreMaj);
+ sc ->FirstComponent = In[0];
+ }
+
+
+ if (In[1] != sc ->SecondComponent) {
+
+ if (sc ->SecondComponent != -1) {
+
+ _cmsIOPrintf(sc ->m, sc ->PostMin);
+ }
+
+ _cmsIOPrintf(sc ->m, sc ->PreMin);
+ sc ->SecondComponent = In[1];
+ }
+
+ // Dump table.
+
+ for (i=0; i < sc -> Pipeline ->Params->nOutputs; i++) {
+
+ cmsUInt16Number wWordOut = Out[i];
+ cmsUInt8Number wByteOut; // Value as byte
+
+
+ // We always deal with Lab4
+
+ wByteOut = Word2Byte(wWordOut);
+ WriteByte(sc -> m, wByteOut);
+ }
+
+ return 1;
+}
+
+// Writes a Pipeline on memstream. Could be 8 or 16 bits based
+
+static
+void WriteCLUT(cmsIOHANDLER* m, cmsStage* mpe, const char* PreMaj,
+ const char* PostMaj,
+ const char* PreMin,
+ const char* PostMin,
+ int FixWhite,
+ cmsColorSpaceSignature ColorSpace)
+{
+ cmsUInt32Number i;
+ cmsPsSamplerCargo sc;
+
+ sc.FirstComponent = -1;
+ sc.SecondComponent = -1;
+ sc.Pipeline = (_cmsStageCLutData *) mpe ->Data;
+ sc.m = m;
+ sc.PreMaj = PreMaj;
+ sc.PostMaj= PostMaj;
+
+ sc.PreMin = PreMin;
+ sc.PostMin = PostMin;
+ sc.FixWhite = FixWhite;
+ sc.ColorSpace = ColorSpace;
+
+ _cmsIOPrintf(m, "[");
+
+ for (i=0; i < sc.Pipeline->Params->nInputs; i++)
+ _cmsIOPrintf(m, " %d ", sc.Pipeline->Params->nSamples[i]);
+
+ _cmsIOPrintf(m, " [\n");
+
+ cmsStageSampleCLut16bit(mpe, OutputValueSampler, (void*) &sc, SAMPLER_INSPECT);
+
+ _cmsIOPrintf(m, PostMin);
+ _cmsIOPrintf(m, PostMaj);
+ _cmsIOPrintf(m, "] ");
+
+}
+
+
+// Dumps CIEBasedA Color Space Array
+
+static
+int EmitCIEBasedA(cmsIOHANDLER* m, cmsToneCurve* Curve, cmsCIEXYZ* BlackPoint)
+{
+
+ _cmsIOPrintf(m, "[ /CIEBasedA\n");
+ _cmsIOPrintf(m, " <<\n");
+
+ _cmsIOPrintf(m, "/DecodeA ");
+
+ Emit1Gamma(m, Curve);
+
+ _cmsIOPrintf(m, " \n");
+
+ _cmsIOPrintf(m, "/MatrixA [ 0.9642 1.0000 0.8249 ]\n");
+ _cmsIOPrintf(m, "/RangeLMN [ 0.0 0.9642 0.0 1.0000 0.0 0.8249 ]\n");
+
+ EmitWhiteBlackD50(m, BlackPoint);
+ EmitIntent(m, INTENT_PERCEPTUAL);
+
+ _cmsIOPrintf(m, ">>\n");
+ _cmsIOPrintf(m, "]\n");
+
+ return 1;
+}
+
+
+// Dumps CIEBasedABC Color Space Array
+
+static
+int EmitCIEBasedABC(cmsIOHANDLER* m, cmsFloat64Number* Matrix, cmsToneCurve** CurveSet, cmsCIEXYZ* BlackPoint)
+{
+ int i;
+
+ _cmsIOPrintf(m, "[ /CIEBasedABC\n");
+ _cmsIOPrintf(m, "<<\n");
+ _cmsIOPrintf(m, "/DecodeABC [ ");
+
+ EmitNGamma(m, 3, CurveSet);
+
+ _cmsIOPrintf(m, "]\n");
+
+ _cmsIOPrintf(m, "/MatrixABC [ " );
+
+ for( i=0; i < 3; i++ ) {
+
+ _cmsIOPrintf(m, "%.6f %.6f %.6f ", Matrix[i + 3*0],
+ Matrix[i + 3*1],
+ Matrix[i + 3*2]);
+ }
+
+
+ _cmsIOPrintf(m, "]\n");
+
+ _cmsIOPrintf(m, "/RangeLMN [ 0.0 0.9642 0.0 1.0000 0.0 0.8249 ]\n");
+
+ EmitWhiteBlackD50(m, BlackPoint);
+ EmitIntent(m, INTENT_PERCEPTUAL);
+
+ _cmsIOPrintf(m, ">>\n");
+ _cmsIOPrintf(m, "]\n");
+
+
+ return 1;
+}
+
+
+static
+int EmitCIEBasedDEF(cmsIOHANDLER* m, cmsPipeline* Pipeline, int Intent, cmsCIEXYZ* BlackPoint)
+{
+ const char* PreMaj;
+ const char* PostMaj;
+ const char* PreMin, *PostMin;
+ cmsStage* mpe;
+
+ mpe = Pipeline ->Elements;
+
+ switch (cmsStageInputChannels(mpe)) {
+ case 3:
+
+ _cmsIOPrintf(m, "[ /CIEBasedDEF\n");
+ PreMaj ="<";
+ PostMaj= ">\n";
+ PreMin = PostMin = "";
+ break;
+ case 4:
+ _cmsIOPrintf(m, "[ /CIEBasedDEFG\n");
+ PreMaj = "[";
+ PostMaj = "]\n";
+ PreMin = "<";
+ PostMin = ">\n";
+ break;
+ default:
+ return 0;
+
+ }
+
+ _cmsIOPrintf(m, "<<\n");
+
+ if (cmsStageType(mpe) == cmsSigCurveSetElemType) {
+
+ _cmsIOPrintf(m, "/DecodeDEF [ ");
+ EmitNGamma(m, cmsStageOutputChannels(mpe), _cmsStageGetPtrToCurveSet(mpe));
+ _cmsIOPrintf(m, "]\n");
+
+ mpe = mpe ->Next;
+ }
+
+ if (cmsStageType(mpe) == cmsSigCLutElemType) {
+
+ _cmsIOPrintf(m, "/Table ");
+ WriteCLUT(m, mpe, PreMaj, PostMaj, PreMin, PostMin, FALSE, (cmsColorSpaceSignature) 0);
+ _cmsIOPrintf(m, "]\n");
+ }
+
+ EmitLab2XYZ(m);
+ EmitWhiteBlackD50(m, BlackPoint);
+ EmitIntent(m, Intent);
+
+ _cmsIOPrintf(m, " >>\n");
+ _cmsIOPrintf(m, "]\n");
+
+ return 1;
+}
+
+// Generates a curve from a gray profile
+
+static
+ cmsToneCurve* ExtractGray2Y(cmsContext ContextID, cmsHPROFILE hProfile, int Intent)
+{
+ cmsToneCurve* Out = cmsBuildTabulatedToneCurve16(ContextID, 256, NULL);
+ cmsHPROFILE hXYZ = cmsCreateXYZProfile();
+ cmsHTRANSFORM xform = cmsCreateTransformTHR(ContextID, hProfile, TYPE_GRAY_8, hXYZ, TYPE_XYZ_DBL, Intent, cmsFLAGS_NOOPTIMIZE);
+ int i;
+
+ if (Out != NULL) {
+ for (i=0; i < 256; i++) {
+
+ cmsUInt8Number Gray = (cmsUInt8Number) i;
+ cmsCIEXYZ XYZ;
+
+ cmsDoTransform(xform, &Gray, &XYZ, 1);
+
+ Out ->Table16[i] =_cmsQuickSaturateWord(XYZ.Y * 65535.0);
+ }
+ }
+
+ cmsDeleteTransform(xform);
+ cmsCloseProfile(hXYZ);
+ return Out;
+}
+
+
+
+// Because PostScript has only 8 bits in /Table, we should use
+// a more perceptually uniform space... I do choose Lab.
+
+static
+int WriteInputLUT(cmsIOHANDLER* m, cmsHPROFILE hProfile, int Intent, cmsUInt32Number dwFlags)
+{
+ cmsHPROFILE hLab;
+ cmsHTRANSFORM xform;
+ cmsUInt32Number nChannels;
+ cmsUInt32Number InputFormat;
+ int rc;
+ cmsHPROFILE Profiles[2];
+ cmsCIEXYZ BlackPointAdaptedToD50;
+
+ // Does create a device-link based transform.
+ // The DeviceLink is next dumped as working CSA.
+
+ InputFormat = cmsFormatterForColorspaceOfProfile(hProfile, 2, FALSE);
+ nChannels = T_CHANNELS(InputFormat);
+
+
+ cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, Intent, 0);
+
+ // Adjust output to Lab4
+ hLab = cmsCreateLab4ProfileTHR(m ->ContextID, NULL);
+
+ Profiles[0] = hProfile;
+ Profiles[1] = hLab;
+
+ xform = cmsCreateMultiprofileTransform(Profiles, 2, InputFormat, TYPE_Lab_DBL, Intent, 0);
+ cmsCloseProfile(hLab);
+
+ if (xform == NULL) {
+
+ cmsSignalError(m ->ContextID, cmsERROR_COLORSPACE_CHECK, "Cannot create transform Profile -> Lab");
+ return 0;
+ }
+
+ // Only 1, 3 and 4 channels are allowed
+
+ switch (nChannels) {
+
+ case 1: {
+ cmsToneCurve* Gray2Y = ExtractGray2Y(m ->ContextID, hProfile, Intent);
+ EmitCIEBasedA(m, Gray2Y, &BlackPointAdaptedToD50);
+ cmsFreeToneCurve(Gray2Y);
+ }
+ break;
+
+ case 3:
+ case 4: {
+ cmsUInt32Number OutFrm = TYPE_Lab_16;
+ cmsPipeline* DeviceLink;
+ _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
+
+ DeviceLink = cmsPipelineDup(v ->Lut);
+ if (DeviceLink == NULL) return 0;
+
+ dwFlags |= cmsFLAGS_FORCE_CLUT;
+ _cmsOptimizePipeline(m->ContextID, &DeviceLink, Intent, &InputFormat, &OutFrm, &dwFlags);
+
+ rc = EmitCIEBasedDEF(m, DeviceLink, Intent, &BlackPointAdaptedToD50);
+ cmsPipelineFree(DeviceLink);
+ if (rc == 0) return 0;
+ }
+ break;
+
+ default:
+
+ cmsSignalError(m ->ContextID, cmsERROR_COLORSPACE_CHECK, "Only 3, 4 channels supported for CSA. This profile has %d channels.", nChannels);
+ return 0;
+ }
+
+
+ cmsDeleteTransform(xform);
+
+ return 1;
+}
+
+static
+cmsFloat64Number* GetPtrToMatrix(const cmsStage* mpe)
+{
+ _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
+
+ return Data -> Double;
+}
+
+
+// Does create CSA based on matrix-shaper. Allowed types are gray and RGB based
+
+static
+int WriteInputMatrixShaper(cmsIOHANDLER* m, cmsHPROFILE hProfile, cmsStage* Matrix, cmsStage* Shaper)
+{
+ cmsColorSpaceSignature ColorSpace;
+ int rc;
+ cmsCIEXYZ BlackPointAdaptedToD50;
+
+ ColorSpace = cmsGetColorSpace(hProfile);
+
+ cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, INTENT_RELATIVE_COLORIMETRIC, 0);
+
+ if (ColorSpace == cmsSigGrayData) {
+
+ cmsToneCurve** ShaperCurve = _cmsStageGetPtrToCurveSet(Shaper);
+ rc = EmitCIEBasedA(m, ShaperCurve[0], &BlackPointAdaptedToD50);
+
+ }
+ else
+ if (ColorSpace == cmsSigRgbData) {
+
+ cmsMAT3 Mat;
+ int i, j;
+
+ memmove(&Mat, GetPtrToMatrix(Matrix), sizeof(Mat));
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ Mat.v[i].n[j] *= MAX_ENCODEABLE_XYZ;
+
+ rc = EmitCIEBasedABC(m, (cmsFloat64Number *) &Mat,
+ _cmsStageGetPtrToCurveSet(Shaper),
+ &BlackPointAdaptedToD50);
+ }
+ else {
+
+ cmsSignalError(m ->ContextID, cmsERROR_COLORSPACE_CHECK, "Profile is not suitable for CSA. Unsupported colorspace.");
+ return 0;
+ }
+
+ return rc;
+}
+
+
+
+// Creates a PostScript color list from a named profile data.
+// This is a HP extension, and it works in Lab instead of XYZ
+
+static
+int WriteNamedColorCSA(cmsIOHANDLER* m, cmsHPROFILE hNamedColor, int Intent)
+{
+ cmsHTRANSFORM xform;
+ cmsHPROFILE hLab;
+ int i, nColors;
+ char ColorName[32];
+ cmsNAMEDCOLORLIST* NamedColorList;
+
+ hLab = cmsCreateLab4ProfileTHR(m ->ContextID, NULL);
+ xform = cmsCreateTransform(hNamedColor, TYPE_NAMED_COLOR_INDEX, hLab, TYPE_Lab_DBL, Intent, 0);
+ if (xform == NULL) return 0;
+
+ NamedColorList = cmsGetNamedColorList(xform);
+ if (NamedColorList == NULL) return 0;
+
+ _cmsIOPrintf(m, "<<\n");
+ _cmsIOPrintf(m, "(colorlistcomment) (%s)\n", "Named color CSA");
+ _cmsIOPrintf(m, "(Prefix) [ (Pantone ) (PANTONE ) ]\n");
+ _cmsIOPrintf(m, "(Suffix) [ ( CV) ( CVC) ( C) ]\n");
+
+ nColors = cmsNamedColorCount(NamedColorList);
+
+
+ for (i=0; i < nColors; i++) {
+
+ cmsUInt16Number In[1];
+ cmsCIELab Lab;
+
+ In[0] = (cmsUInt16Number) i;
+
+ if (!cmsNamedColorInfo(NamedColorList, i, ColorName, NULL, NULL, NULL, NULL))
+ continue;
+
+ cmsDoTransform(xform, In, &Lab, 1);
+ _cmsIOPrintf(m, " (%s) [ %.3f %.3f %.3f ]\n", ColorName, Lab.L, Lab.a, Lab.b);
+ }
+
+
+
+ _cmsIOPrintf(m, ">>\n");
+
+ cmsDeleteTransform(xform);
+ cmsCloseProfile(hLab);
+ return 1;
+}
+
+
+// Does create a Color Space Array on XYZ colorspace for PostScript usage
+static
+cmsUInt32Number GenerateCSA(cmsContext ContextID,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags,
+ cmsIOHANDLER* mem)
+{
+ cmsUInt32Number dwBytesUsed;
+ cmsPipeline* lut = NULL;
+ cmsStage* Matrix, *Shaper;
+
+
+ // Is a named color profile?
+ if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
+
+ if (!WriteNamedColorCSA(mem, hProfile, Intent)) goto Error;
+ }
+ else {
+
+
+ // Any profile class are allowed (including devicelink), but
+ // output (PCS) colorspace must be XYZ or Lab
+ cmsColorSpaceSignature ColorSpace = cmsGetPCS(hProfile);
+
+ if (ColorSpace != cmsSigXYZData &&
+ ColorSpace != cmsSigLabData) {
+
+ cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "Invalid output color space");
+ goto Error;
+ }
+
+
+ // Read the lut with all necessary conversion stages
+ lut = _cmsReadInputLUT(hProfile, Intent);
+ if (lut == NULL) goto Error;
+
+
+ // Tone curves + matrix can be implemented without any LUT
+ if (cmsPipelineCheckAndRetreiveStages(lut, 2, cmsSigCurveSetElemType, cmsSigMatrixElemType, &Shaper, &Matrix)) {
+
+ if (!WriteInputMatrixShaper(mem, hProfile, Matrix, Shaper)) goto Error;
+
+ }
+ else {
+ // We need a LUT for the rest
+ if (!WriteInputLUT(mem, hProfile, Intent, dwFlags)) goto Error;
+ }
+ }
+
+
+ // Done, keep memory usage
+ dwBytesUsed = mem ->UsedSpace;
+
+ // Get rid of LUT
+ if (lut != NULL) cmsPipelineFree(lut);
+
+ // Finally, return used byte count
+ return dwBytesUsed;
+
+Error:
+ if (lut != NULL) cmsPipelineFree(lut);
+ return 0;
+}
+
+// ------------------------------------------------------ Color Rendering Dictionary (CRD)
+
+
+
+/*
+
+ Black point compensation plus chromatic adaptation:
+
+ Step 1 - Chromatic adaptation
+ =============================
+
+ WPout
+ X = ------- PQR
+ Wpin
+
+ Step 2 - Black point compensation
+ =================================
+
+ (WPout - BPout)*X - WPout*(BPin - BPout)
+ out = ---------------------------------------
+ WPout - BPin
+
+
+ Algorithm discussion
+ ====================
+
+ TransformPQR(WPin, BPin, WPout, BPout, PQR)
+
+ Wpin,etc= { Xws Yws Zws Pws Qws Rws }
+
+
+ Algorithm Stack 0...n
+ ===========================================================
+ PQR BPout WPout BPin WPin
+ 4 index 3 get WPin PQR BPout WPout BPin WPin
+ div (PQR/WPin) BPout WPout BPin WPin
+ 2 index 3 get WPout (PQR/WPin) BPout WPout BPin WPin
+ mult WPout*(PQR/WPin) BPout WPout BPin WPin
+
+ 2 index 3 get WPout WPout*(PQR/WPin) BPout WPout BPin WPin
+ 2 index 3 get BPout WPout WPout*(PQR/WPin) BPout WPout BPin WPin
+ sub (WPout-BPout) WPout*(PQR/WPin) BPout WPout BPin WPin
+ mult (WPout-BPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+
+ 2 index 3 get WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+ 4 index 3 get BPin WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+ 3 index 3 get BPout BPin WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+
+ sub (BPin-BPout) WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+ mult (BPin-BPout)*WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
+ sub (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
+
+ 3 index 3 get BPin (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
+ 3 index 3 get WPout BPin (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
+ exch
+ sub (WPout-BPin) (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
+ div
+
+ exch pop
+ exch pop
+ exch pop
+ exch pop
+
+*/
+
+
+static
+void EmitPQRStage(cmsIOHANDLER* m, cmsHPROFILE hProfile, int DoBPC, int lIsAbsolute)
+{
+
+
+ if (lIsAbsolute) {
+
+ // For absolute colorimetric intent, encode back to relative
+ // and generate a relative Pipeline
+
+ // Relative encoding is obtained across XYZpcs*(D50/WhitePoint)
+
+ cmsCIEXYZ White;
+
+ _cmsReadMediaWhitePoint(&White, hProfile);
+
+ _cmsIOPrintf(m,"/MatrixPQR [1 0 0 0 1 0 0 0 1 ]\n");
+ _cmsIOPrintf(m,"/RangePQR [ -0.5 2 -0.5 2 -0.5 2 ]\n");
+
+ _cmsIOPrintf(m, "%% Absolute colorimetric -- encode to relative to maximize LUT usage\n"
+ "/TransformPQR [\n"
+ "{0.9642 mul %g div exch pop exch pop exch pop exch pop} bind\n"
+ "{1.0000 mul %g div exch pop exch pop exch pop exch pop} bind\n"
+ "{0.8249 mul %g div exch pop exch pop exch pop exch pop} bind\n]\n",
+ White.X, White.Y, White.Z);
+ return;
+ }
+
+
+ _cmsIOPrintf(m,"%% Bradford Cone Space\n"
+ "/MatrixPQR [0.8951 -0.7502 0.0389 0.2664 1.7135 -0.0685 -0.1614 0.0367 1.0296 ] \n");
+
+ _cmsIOPrintf(m, "/RangePQR [ -0.5 2 -0.5 2 -0.5 2 ]\n");
+
+
+ // No BPC
+
+ if (!DoBPC) {
+
+ _cmsIOPrintf(m, "%% VonKries-like transform in Bradford Cone Space\n"
+ "/TransformPQR [\n"
+ "{exch pop exch 3 get mul exch pop exch 3 get div} bind\n"
+ "{exch pop exch 4 get mul exch pop exch 4 get div} bind\n"
+ "{exch pop exch 5 get mul exch pop exch 5 get div} bind\n]\n");
+ } else {
+
+ // BPC
+
+ _cmsIOPrintf(m, "%% VonKries-like transform in Bradford Cone Space plus BPC\n"
+ "/TransformPQR [\n");
+
+ _cmsIOPrintf(m, "{4 index 3 get div 2 index 3 get mul "
+ "2 index 3 get 2 index 3 get sub mul "
+ "2 index 3 get 4 index 3 get 3 index 3 get sub mul sub "
+ "3 index 3 get 3 index 3 get exch sub div "
+ "exch pop exch pop exch pop exch pop } bind\n");
+
+ _cmsIOPrintf(m, "{4 index 4 get div 2 index 4 get mul "
+ "2 index 4 get 2 index 4 get sub mul "
+ "2 index 4 get 4 index 4 get 3 index 4 get sub mul sub "
+ "3 index 4 get 3 index 4 get exch sub div "
+ "exch pop exch pop exch pop exch pop } bind\n");
+
+ _cmsIOPrintf(m, "{4 index 5 get div 2 index 5 get mul "
+ "2 index 5 get 2 index 5 get sub mul "
+ "2 index 5 get 4 index 5 get 3 index 5 get sub mul sub "
+ "3 index 5 get 3 index 5 get exch sub div "
+ "exch pop exch pop exch pop exch pop } bind\n]\n");
+
+ }
+
+
+}
+
+
+static
+void EmitXYZ2Lab(cmsIOHANDLER* m)
+{
+ _cmsIOPrintf(m, "/RangeLMN [ -0.635 2.0 0 2 -0.635 2.0 ]\n");
+ _cmsIOPrintf(m, "/EncodeLMN [\n");
+ _cmsIOPrintf(m, "{ 0.964200 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
+ _cmsIOPrintf(m, "{ 1.000000 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
+ _cmsIOPrintf(m, "{ 0.824900 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
+ _cmsIOPrintf(m, "]\n");
+ _cmsIOPrintf(m, "/MatrixABC [ 0 1 0 1 -1 1 0 0 -1 ]\n");
+ _cmsIOPrintf(m, "/EncodeABC [\n");
+
+
+ _cmsIOPrintf(m, "{ 116 mul 16 sub 100 div } bind\n");
+ _cmsIOPrintf(m, "{ 500 mul 128 add 256 div } bind\n");
+ _cmsIOPrintf(m, "{ 200 mul 128 add 256 div } bind\n");
+
+
+ _cmsIOPrintf(m, "]\n");
+
+
+}
+
+// Due to impedance mismatch between XYZ and almost all RGB and CMYK spaces
+// I choose to dump LUTS in Lab instead of XYZ. There is still a lot of wasted
+// space on 3D CLUT, but since space seems not to be a problem here, 33 points
+// would give a reasonable accurancy. Note also that CRD tables must operate in
+// 8 bits.
+
+static
+int WriteOutputLUT(cmsIOHANDLER* m, cmsHPROFILE hProfile, int Intent, cmsUInt32Number dwFlags)
+{
+ cmsHPROFILE hLab;
+ cmsHTRANSFORM xform;
+ int i, nChannels;
+ cmsUInt32Number OutputFormat;
+ _cmsTRANSFORM* v;
+ cmsPipeline* DeviceLink;
+ cmsHPROFILE Profiles[3];
+ cmsCIEXYZ BlackPointAdaptedToD50;
+ cmsBool lDoBPC = (dwFlags & cmsFLAGS_BLACKPOINTCOMPENSATION);
+ cmsBool lFixWhite = !(dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP);
+ cmsUInt32Number InFrm = TYPE_Lab_16;
+ int RelativeEncodingIntent;
+ cmsColorSpaceSignature ColorSpace;
+
+
+ hLab = cmsCreateLab4ProfileTHR(m ->ContextID, NULL);
+ if (hLab == NULL) return 0;
+
+ OutputFormat = cmsFormatterForColorspaceOfProfile(hProfile, 2, FALSE);
+ nChannels = T_CHANNELS(OutputFormat);
+
+ ColorSpace = cmsGetColorSpace(hProfile);
+
+ // For absolute colorimetric, the LUT is encoded as relative in order to preserve precision.
+
+ RelativeEncodingIntent = Intent;
+ if (RelativeEncodingIntent == INTENT_ABSOLUTE_COLORIMETRIC)
+ RelativeEncodingIntent = INTENT_RELATIVE_COLORIMETRIC;
+
+
+ // Use V4 Lab always
+ Profiles[0] = hLab;
+ Profiles[1] = hProfile;
+
+ xform = cmsCreateMultiprofileTransformTHR(m ->ContextID,
+ Profiles, 2, TYPE_Lab_DBL,
+ OutputFormat, RelativeEncodingIntent, 0);
+ cmsCloseProfile(hLab);
+
+ if (xform == NULL) {
+
+ cmsSignalError(m ->ContextID, cmsERROR_COLORSPACE_CHECK, "Cannot create transform Lab -> Profile in CRD creation");
+ return 0;
+ }
+
+ // Get a copy of the internal devicelink
+ v = (_cmsTRANSFORM*) xform;
+ DeviceLink = cmsPipelineDup(v ->Lut);
+ if (DeviceLink == NULL) return 0;
+
+
+ // We need a CLUT
+ dwFlags |= cmsFLAGS_FORCE_CLUT;
+ _cmsOptimizePipeline(m->ContextID, &DeviceLink, RelativeEncodingIntent, &InFrm, &OutputFormat, &dwFlags);
+
+ _cmsIOPrintf(m, "<<\n");
+ _cmsIOPrintf(m, "/ColorRenderingType 1\n");
+
+
+ cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, Intent, 0);
+
+ // Emit headers, etc.
+ EmitWhiteBlackD50(m, &BlackPointAdaptedToD50);
+ EmitPQRStage(m, hProfile, lDoBPC, Intent == INTENT_ABSOLUTE_COLORIMETRIC);
+ EmitXYZ2Lab(m);
+
+
+ // FIXUP: map Lab (100, 0, 0) to perfect white, because the particular encoding for Lab
+ // does map a=b=0 not falling into any specific node. Since range a,b goes -128..127,
+ // zero is slightly moved towards right, so assure next node (in L=100 slice) is mapped to
+ // zero. This would sacrifice a bit of highlights, but failure to do so would cause
+ // scum dot. Ouch.
+
+ if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
+ lFixWhite = FALSE;
+
+ _cmsIOPrintf(m, "/RenderTable ");
+
+
+ WriteCLUT(m, cmsPipelineGetPtrToFirstStage(DeviceLink), "<", ">\n", "", "", lFixWhite, ColorSpace);
+
+ _cmsIOPrintf(m, " %d {} bind ", nChannels);
+
+ for (i=1; i < nChannels; i++)
+ _cmsIOPrintf(m, "dup ");
+
+ _cmsIOPrintf(m, "]\n");
+
+
+ EmitIntent(m, Intent);
+
+ _cmsIOPrintf(m, ">>\n");
+
+ if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
+
+ _cmsIOPrintf(m, "/Current exch /ColorRendering defineresource pop\n");
+ }
+
+ cmsPipelineFree(DeviceLink);
+ cmsDeleteTransform(xform);
+
+ return 1;
+}
+
+
+// Builds a ASCII string containing colorant list in 0..1.0 range
+static
+void BuildColorantList(char *Colorant, int nColorant, cmsUInt16Number Out[])
+{
+ char Buff[32];
+ int j;
+
+ Colorant[0] = 0;
+ if (nColorant > cmsMAXCHANNELS)
+ nColorant = cmsMAXCHANNELS;
+
+ for (j=0; j < nColorant; j++) {
+
+ sprintf(Buff, "%.3f", Out[j] / 65535.0);
+ strcat(Colorant, Buff);
+ if (j < nColorant -1)
+ strcat(Colorant, " ");
+
+ }
+}
+
+
+// Creates a PostScript color list from a named profile data.
+// This is a HP extension.
+
+static
+int WriteNamedColorCRD(cmsIOHANDLER* m, cmsHPROFILE hNamedColor, int Intent, cmsUInt32Number dwFlags)
+{
+ cmsHTRANSFORM xform;
+ int i, nColors, nColorant;
+ cmsUInt32Number OutputFormat;
+ char ColorName[32];
+ char Colorant[128];
+ cmsNAMEDCOLORLIST* NamedColorList;
+
+
+ OutputFormat = cmsFormatterForColorspaceOfProfile(hNamedColor, 2, FALSE);
+ nColorant = T_CHANNELS(OutputFormat);
+
+
+ xform = cmsCreateTransform(hNamedColor, TYPE_NAMED_COLOR_INDEX, NULL, OutputFormat, Intent, dwFlags);
+ if (xform == NULL) return 0;
+
+
+ NamedColorList = cmsGetNamedColorList(xform);
+ if (NamedColorList == NULL) return 0;
+
+ _cmsIOPrintf(m, "<<\n");
+ _cmsIOPrintf(m, "(colorlistcomment) (%s) \n", "Named profile");
+ _cmsIOPrintf(m, "(Prefix) [ (Pantone ) (PANTONE ) ]\n");
+ _cmsIOPrintf(m, "(Suffix) [ ( CV) ( CVC) ( C) ]\n");
+
+ nColors = cmsNamedColorCount(NamedColorList);
+
+ for (i=0; i < nColors; i++) {
+
+ cmsUInt16Number In[1];
+ cmsUInt16Number Out[cmsMAXCHANNELS];
+
+ In[0] = (cmsUInt16Number) i;
+
+ if (!cmsNamedColorInfo(NamedColorList, i, ColorName, NULL, NULL, NULL, NULL))
+ continue;
+
+ cmsDoTransform(xform, In, Out, 1);
+ BuildColorantList(Colorant, nColorant, Out);
+ _cmsIOPrintf(m, " (%s) [ %s ]\n", ColorName, Colorant);
+ }
+
+ _cmsIOPrintf(m, " >>");
+
+ if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
+
+ _cmsIOPrintf(m, " /Current exch /HPSpotTable defineresource pop\n");
+ }
+
+ cmsDeleteTransform(xform);
+ return 1;
+}
+
+
+
+// This one does create a Color Rendering Dictionary.
+// CRD are always LUT-Based, no matter if profile is
+// implemented as matrix-shaper.
+
+static
+cmsUInt32Number GenerateCRD(cmsContext ContextID,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent, cmsUInt32Number dwFlags,
+ cmsIOHANDLER* mem)
+{
+ cmsUInt32Number dwBytesUsed;
+
+ if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
+
+ EmitHeader(mem, "Color Rendering Dictionary (CRD)", hProfile);
+ }
+
+
+ // Is a named color profile?
+ if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
+
+ if (!WriteNamedColorCRD(mem, hProfile, Intent, dwFlags)) {
+ return 0;
+ }
+ }
+ else {
+
+ // CRD are always implemented as LUT
+
+ if (!WriteOutputLUT(mem, hProfile, Intent, dwFlags)) {
+ return 0;
+ }
+ }
+
+ if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
+
+ _cmsIOPrintf(mem, "%%%%EndResource\n");
+ _cmsIOPrintf(mem, "\n%% CRD End\n");
+ }
+
+ // Done, keep memory usage
+ dwBytesUsed = mem ->UsedSpace;
+
+ // Finally, return used byte count
+ return dwBytesUsed;
+
+ cmsUNUSED_PARAMETER(ContextID);
+}
+
+
+
+
+cmsUInt32Number CMSEXPORT cmsGetPostScriptColorResource(cmsContext ContextID,
+ cmsPSResourceType Type,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags,
+ cmsIOHANDLER* io)
+{
+ cmsUInt32Number rc;
+
+
+ switch (Type) {
+
+ case cmsPS_RESOURCE_CSA:
+ rc = GenerateCSA(ContextID, hProfile, Intent, dwFlags, io);
+ break;
+
+ default:
+ case cmsPS_RESOURCE_CRD:
+ rc = GenerateCRD(ContextID, hProfile, Intent, dwFlags, io);
+ break;
+ }
+
+ return rc;
+}
+
+
+
+cmsUInt32Number CMSEXPORT cmsGetPostScriptCRD(cmsContext ContextID,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent, cmsUInt32Number dwFlags,
+ void* Buffer, cmsUInt32Number dwBufferLen)
+{
+ cmsIOHANDLER* mem;
+ cmsUInt32Number dwBytesUsed;
+
+ // Set up the serialization engine
+ if (Buffer == NULL)
+ mem = cmsOpenIOhandlerFromNULL(ContextID);
+ else
+ mem = cmsOpenIOhandlerFromMem(ContextID, Buffer, dwBufferLen, "w");
+
+ if (!mem) return 0;
+
+ dwBytesUsed = cmsGetPostScriptColorResource(ContextID, cmsPS_RESOURCE_CRD, hProfile, Intent, dwFlags, mem);
+
+ // Get rid of memory stream
+ cmsCloseIOhandler(mem);
+
+ return dwBytesUsed;
+}
+
+
+
+// Does create a Color Space Array on XYZ colorspace for PostScript usage
+cmsUInt32Number CMSEXPORT cmsGetPostScriptCSA(cmsContext ContextID,
+ cmsHPROFILE hProfile,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags,
+ void* Buffer,
+ cmsUInt32Number dwBufferLen)
+{
+ cmsIOHANDLER* mem;
+ cmsUInt32Number dwBytesUsed;
+
+ if (Buffer == NULL)
+ mem = cmsOpenIOhandlerFromNULL(ContextID);
+ else
+ mem = cmsOpenIOhandlerFromMem(ContextID, Buffer, dwBufferLen, "w");
+
+ if (!mem) return 0;
+
+ dwBytesUsed = cmsGetPostScriptColorResource(ContextID, cmsPS_RESOURCE_CSA, hProfile, Intent, dwFlags, mem);
+
+ // Get rid of memory stream
+ cmsCloseIOhandler(mem);
+
+ return dwBytesUsed;
+
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssamp.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssamp.c
new file mode 100644
index 0000000000..70e469161f
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssamp.c
@@ -0,0 +1,572 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2010 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"
+
+
+#define cmsmin(a, b) (((a) < (b)) ? (a) : (b))
+#define cmsmax(a, b) (((a) > (b)) ? (a) : (b))
+
+// This file contains routines for resampling and LUT optimization, black point detection
+// and black preservation.
+
+// Black point detection -------------------------------------------------------------------------
+
+
+// PCS -> PCS round trip transform, always uses relative intent on the device -> pcs
+static
+cmsHTRANSFORM CreateRoundtripXForm(cmsHPROFILE hProfile, cmsUInt32Number nIntent)
+{
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsHPROFILE hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
+ cmsHTRANSFORM xform;
+ cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
+ cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
+ cmsHPROFILE hProfiles[4];
+ cmsUInt32Number Intents[4];
+
+ hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
+ Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
+
+ xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents,
+ States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
+
+ cmsCloseProfile(hLab);
+ return xform;
+}
+
+// Use darker colorants to obtain black point. This works in the relative colorimetric intent and
+// assumes more ink results in darker colors. No ink limit is assumed.
+static
+cmsBool BlackPointAsDarkerColorant(cmsHPROFILE hInput,
+ cmsUInt32Number Intent,
+ cmsCIEXYZ* BlackPoint,
+ cmsUInt32Number dwFlags)
+{
+ cmsUInt16Number *Black;
+ cmsHTRANSFORM xform;
+ cmsColorSpaceSignature Space;
+ cmsUInt32Number nChannels;
+ cmsUInt32Number dwFormat;
+ cmsHPROFILE hLab;
+ cmsCIELab Lab;
+ cmsCIEXYZ BlackXYZ;
+ cmsContext ContextID = cmsGetProfileContextID(hInput);
+
+ // If the profile does not support input direction, assume Black point 0
+ if (!cmsIsIntentSupported(hInput, Intent, LCMS_USED_AS_INPUT)) {
+
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Create a formatter which has n channels and floating point
+ dwFormat = cmsFormatterForColorspaceOfProfile(hInput, 2, FALSE);
+
+ // Try to get black by using black colorant
+ Space = cmsGetColorSpace(hInput);
+
+ // This function returns darker colorant in 16 bits for several spaces
+ if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) {
+
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ if (nChannels != T_CHANNELS(dwFormat)) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Lab will be used as the output space, but lab2 will avoid recursion
+ hLab = cmsCreateLab2ProfileTHR(ContextID, NULL);
+ if (hLab == NULL) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Create the transform
+ xform = cmsCreateTransformTHR(ContextID, hInput, dwFormat,
+ hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
+ cmsCloseProfile(hLab);
+
+ if (xform == NULL) {
+
+ // Something went wrong. Get rid of open resources and return zero as black
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Convert black to Lab
+ cmsDoTransform(xform, Black, &Lab, 1);
+
+ // Force it to be neutral, clip to max. L* of 50
+ Lab.a = Lab.b = 0;
+ if (Lab.L > 50) Lab.L = 50;
+
+ // Free the resources
+ cmsDeleteTransform(xform);
+
+ // Convert from Lab (which is now clipped) to XYZ.
+ cmsLab2XYZ(NULL, &BlackXYZ, &Lab);
+
+ if (BlackPoint != NULL)
+ *BlackPoint = BlackXYZ;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(dwFlags);
+}
+
+// Get a black point of output CMYK profile, discounting any ink-limiting embedded
+// in the profile. For doing that, we use perceptual intent in input direction:
+// Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
+static
+cmsBool BlackPointUsingPerceptualBlack(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
+{
+ cmsHTRANSFORM hRoundTrip;
+ cmsCIELab LabIn, LabOut;
+ cmsCIEXYZ BlackXYZ;
+
+ // Is the intent supported by the profile?
+ if (!cmsIsIntentSupported(hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
+
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return TRUE;
+ }
+
+ hRoundTrip = CreateRoundtripXForm(hProfile, INTENT_PERCEPTUAL);
+ if (hRoundTrip == NULL) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ LabIn.L = LabIn.a = LabIn.b = 0;
+ cmsDoTransform(hRoundTrip, &LabIn, &LabOut, 1);
+
+ // Clip Lab to reasonable limits
+ if (LabOut.L > 50) LabOut.L = 50;
+ LabOut.a = LabOut.b = 0;
+
+ cmsDeleteTransform(hRoundTrip);
+
+ // Convert it to XYZ
+ cmsLab2XYZ(NULL, &BlackXYZ, &LabOut);
+
+ if (BlackPoint != NULL)
+ *BlackPoint = BlackXYZ;
+
+ return TRUE;
+}
+
+// This function shouldn't exist at all -- there is such quantity of broken
+// profiles on black point tag, that we must somehow fix chromaticity to
+// avoid huge tint when doing Black point compensation. This function does
+// just that. There is a special flag for using black point tag, but turned
+// off by default because it is bogus on most profiles. The detection algorithm
+// involves to turn BP to neutral and to use only L component.
+cmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
+{
+ cmsProfileClassSignature devClass;
+
+ // Make sure the device class is adequate
+ devClass = cmsGetDeviceClass(hProfile);
+ if (devClass == cmsSigLinkClass ||
+ devClass == cmsSigAbstractClass ||
+ devClass == cmsSigNamedColorClass) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Make sure intent is adequate
+ if (Intent != INTENT_PERCEPTUAL &&
+ Intent != INTENT_RELATIVE_COLORIMETRIC &&
+ Intent != INTENT_SATURATION) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // v4 + perceptual & saturation intents does have its own black point, and it is
+ // well specified enough to use it. Black point tag is deprecated in V4.
+ if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&
+ (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
+
+ // Matrix shaper share MRC & perceptual intents
+ if (cmsIsMatrixShaper(hProfile))
+ return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
+
+ // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
+ BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
+ BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
+ BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
+
+ return TRUE;
+ }
+
+
+#ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
+
+ // v2, v4 rel/abs colorimetric
+ if (cmsIsTag(hProfile, cmsSigMediaBlackPointTag) &&
+ Intent == INTENT_RELATIVE_COLORIMETRIC) {
+
+ cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
+ cmsCIELab Lab;
+
+ // If black point is specified, then use it,
+
+ BlackPtr = cmsReadTag(hProfile, cmsSigMediaBlackPointTag);
+ if (BlackPtr != NULL) {
+
+ BlackXYZ = *BlackPtr;
+ _cmsReadMediaWhitePoint(&MediaWhite, hProfile);
+
+ // Black point is absolute XYZ, so adapt to D50 to get PCS value
+ cmsAdaptToIlluminant(&UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(), &BlackXYZ);
+
+ // Force a=b=0 to get rid of any chroma
+ cmsXYZ2Lab(NULL, &Lab, &UntrustedBlackPoint);
+ Lab.a = Lab.b = 0;
+ if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
+ cmsLab2XYZ(NULL, &TrustedBlackPoint, &Lab);
+
+ if (BlackPoint != NULL)
+ *BlackPoint = TrustedBlackPoint;
+
+ return TRUE;
+ }
+ }
+#endif
+
+ // That is about v2 profiles.
+
+ // If output profile, discount ink-limiting and that's all
+ if (Intent == INTENT_RELATIVE_COLORIMETRIC &&
+ (cmsGetDeviceClass(hProfile) == cmsSigOutputClass) &&
+ (cmsGetColorSpace(hProfile) == cmsSigCmykData))
+ return BlackPointUsingPerceptualBlack(BlackPoint, hProfile);
+
+ // Nope, compute BP using current intent.
+ return BlackPointAsDarkerColorant(hProfile, Intent, BlackPoint, dwFlags);
+}
+
+
+
+// ---------------------------------------------------------------------------------------------------------
+
+// Least Squares Fit of a Quadratic Curve to Data
+// http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html
+
+static
+cmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(int n, cmsFloat64Number x[], cmsFloat64Number y[])
+{
+ double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0;
+ double sum_y = 0, sum_yx = 0, sum_yx2 = 0;
+ double d, a, b, c;
+ int i;
+ cmsMAT3 m;
+ cmsVEC3 v, res;
+
+ if (n < 4) return 0;
+
+ for (i=0; i < n; i++) {
+
+ double xn = x[i];
+ double yn = y[i];
+
+ sum_x += xn;
+ sum_x2 += xn*xn;
+ sum_x3 += xn*xn*xn;
+ sum_x4 += xn*xn*xn*xn;
+
+ sum_y += yn;
+ sum_yx += yn*xn;
+ sum_yx2 += yn*xn*xn;
+ }
+
+ _cmsVEC3init(&m.v[0], n, sum_x, sum_x2);
+ _cmsVEC3init(&m.v[1], sum_x, sum_x2, sum_x3);
+ _cmsVEC3init(&m.v[2], sum_x2, sum_x3, sum_x4);
+
+ _cmsVEC3init(&v, sum_y, sum_yx, sum_yx2);
+
+ if (!_cmsMAT3solve(&res, &m, &v)) return 0;
+
+
+ a = res.n[2];
+ b = res.n[1];
+ c = res.n[0];
+
+ if (fabs(a) < 1.0E-10) {
+
+ return cmsmin(0, cmsmax(50, -c/b ));
+ }
+ else {
+
+ d = b*b - 4.0 * a * c;
+ if (d <= 0) {
+ return 0;
+ }
+ else {
+
+ double rt = (-b + sqrt(d)) / (2.0 * a);
+
+ return cmsmax(0, cmsmin(50, rt));
+ }
+ }
+
+}
+
+/*
+static
+cmsBool IsMonotonic(int n, const cmsFloat64Number Table[])
+{
+ int i;
+ cmsFloat64Number last;
+
+ last = Table[n-1];
+
+ for (i = n-2; i >= 0; --i) {
+
+ if (Table[i] > last)
+
+ return FALSE;
+ else
+ last = Table[i];
+
+ }
+
+ return TRUE;
+}
+*/
+
+// Calculates the black point of a destination profile.
+// This algorithm comes from the Adobe paper disclosing its black point compensation method.
+cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
+{
+ cmsColorSpaceSignature ColorSpace;
+ cmsHTRANSFORM hRoundTrip = NULL;
+ cmsCIELab InitialLab, destLab, Lab;
+ cmsFloat64Number inRamp[256], outRamp[256];
+ cmsFloat64Number MinL, MaxL;
+ cmsBool NearlyStraightMidrange = TRUE;
+ cmsFloat64Number yRamp[256];
+ cmsFloat64Number x[256], y[256];
+ cmsFloat64Number lo, hi;
+ int n, l;
+ cmsProfileClassSignature devClass;
+
+ // Make sure the device class is adequate
+ devClass = cmsGetDeviceClass(hProfile);
+ if (devClass == cmsSigLinkClass ||
+ devClass == cmsSigAbstractClass ||
+ devClass == cmsSigNamedColorClass) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+ // Make sure intent is adequate
+ if (Intent != INTENT_PERCEPTUAL &&
+ Intent != INTENT_RELATIVE_COLORIMETRIC &&
+ Intent != INTENT_SATURATION) {
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+
+ // v4 + perceptual & saturation intents does have its own black point, and it is
+ // well specified enough to use it. Black point tag is deprecated in V4.
+ if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&
+ (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
+
+ // Matrix shaper share MRC & perceptual intents
+ if (cmsIsMatrixShaper(hProfile))
+ return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
+
+ // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
+ BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
+ BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
+ BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
+ return TRUE;
+ }
+
+
+ // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document)
+ ColorSpace = cmsGetColorSpace(hProfile);
+ if (!cmsIsCLUT(hProfile, Intent, LCMS_USED_AS_OUTPUT ) ||
+ (ColorSpace != cmsSigGrayData &&
+ ColorSpace != cmsSigRgbData &&
+ ColorSpace != cmsSigCmykData)) {
+
+ // In this case, handle as input case
+ return cmsDetectBlackPoint(BlackPoint, hProfile, Intent, dwFlags);
+ }
+
+ // It is one of the valid cases!, use Adobe algorithm
+
+
+ // Set a first guess, that should work on good profiles.
+ if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
+
+ cmsCIEXYZ IniXYZ;
+
+ // calculate initial Lab as source black point
+ if (!cmsDetectBlackPoint(&IniXYZ, hProfile, Intent, dwFlags)) {
+ return FALSE;
+ }
+
+ // convert the XYZ to lab
+ cmsXYZ2Lab(NULL, &InitialLab, &IniXYZ);
+
+ } else {
+
+ // set the initial Lab to zero, that should be the black point for perceptual and saturation
+ InitialLab.L = 0;
+ InitialLab.a = 0;
+ InitialLab.b = 0;
+ }
+
+
+ // Step 2
+ // ======
+
+ // Create a roundtrip. Define a Transform BT for all x in L*a*b*
+ hRoundTrip = CreateRoundtripXForm(hProfile, Intent);
+ if (hRoundTrip == NULL) return FALSE;
+
+ // Compute ramps
+
+ for (l=0; l < 256; l++) {
+
+ Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0;
+ Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a));
+ Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b));
+
+ cmsDoTransform(hRoundTrip, &Lab, &destLab, 1);
+
+ inRamp[l] = Lab.L;
+ outRamp[l] = destLab.L;
+ }
+
+ // Make monotonic
+ for (l = 254; l > 0; --l) {
+ outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]);
+ }
+
+ // Check
+ if (! (outRamp[0] < outRamp[255])) {
+
+ cmsDeleteTransform(hRoundTrip);
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+
+ // Test for mid range straight (only on relative colorimetric)
+
+ NearlyStraightMidrange = TRUE;
+ MinL = outRamp[0]; MaxL = outRamp[255];
+ if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
+
+ for (l=0; l < 256; l++) {
+
+ if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) ||
+ (fabs(inRamp[l] - outRamp[l]) < 4.0 )))
+ NearlyStraightMidrange = FALSE;
+ }
+
+ // If the mid range is straight (as determined above) then the
+ // DestinationBlackPoint shall be the same as initialLab.
+ // Otherwise, the DestinationBlackPoint shall be determined
+ // using curve fitting.
+
+ if (NearlyStraightMidrange) {
+
+ cmsLab2XYZ(NULL, BlackPoint, &InitialLab);
+ cmsDeleteTransform(hRoundTrip);
+ return TRUE;
+ }
+ }
+
+
+ // curve fitting: The round-trip curve normally looks like a nearly constant section at the black point,
+ // with a corner and a nearly straight line to the white point.
+
+ for (l=0; l < 256; l++) {
+
+ yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL);
+ }
+
+ // find the black point using the least squares error quadratic curve fitting
+
+ if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
+ lo = 0.1;
+ hi = 0.5;
+ }
+ else {
+
+ // Perceptual and saturation
+ lo = 0.03;
+ hi = 0.25;
+ }
+
+ // Capture shadow points for the fitting.
+ n = 0;
+ for (l=0; l < 256; l++) {
+
+ cmsFloat64Number ff = yRamp[l];
+
+ if (ff >= lo && ff < hi) {
+ x[n] = inRamp[l];
+ y[n] = yRamp[l];
+ n++;
+ }
+ }
+
+
+ // No suitable points
+ if (n < 3 ) {
+ cmsDeleteTransform(hRoundTrip);
+ BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
+ return FALSE;
+ }
+
+
+ // fit and get the vertex of quadratic curve
+ Lab.L = RootOfLeastSquaresFitQuadraticCurve(n, x, y);
+
+ if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative
+ Lab.L = 0;
+ }
+
+ Lab.a = InitialLab.a;
+ Lab.b = InitialLab.b;
+
+ cmsLab2XYZ(NULL, BlackPoint, &Lab);
+
+ cmsDeleteTransform(hRoundTrip);
+ return TRUE;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssm.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssm.c
new file mode 100644
index 0000000000..5836e15506
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmssm.c
@@ -0,0 +1,734 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2011 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"
+
+
+// ------------------------------------------------------------------------
+
+// Gamut boundary description by using Jan Morovic's Segment maxima method
+// Many thanks to Jan for allowing me to use his algorithm.
+
+// r = C*
+// alpha = Hab
+// theta = L*
+
+#define SECTORS 16 // number of divisions in alpha and theta
+
+// Spherical coordinates
+typedef struct {
+
+ cmsFloat64Number r;
+ cmsFloat64Number alpha;
+ cmsFloat64Number theta;
+
+} cmsSpherical;
+
+typedef enum {
+ GP_EMPTY,
+ GP_SPECIFIED,
+ GP_MODELED
+
+ } GDBPointType;
+
+
+typedef struct {
+
+ GDBPointType Type;
+ cmsSpherical p; // Keep also alpha & theta of maximum
+
+} cmsGDBPoint;
+
+
+typedef struct {
+
+ cmsContext ContextID;
+ cmsGDBPoint Gamut[SECTORS][SECTORS];
+
+} cmsGDB;
+
+
+// A line using the parametric form
+// P = a + t*u
+typedef struct {
+
+ cmsVEC3 a;
+ cmsVEC3 u;
+
+} cmsLine;
+
+
+// A plane using the parametric form
+// Q = b + r*v + s*w
+typedef struct {
+
+ cmsVEC3 b;
+ cmsVEC3 v;
+ cmsVEC3 w;
+
+} cmsPlane;
+
+
+
+// --------------------------------------------------------------------------------------------
+
+// ATAN2() which always returns degree positive numbers
+
+static
+cmsFloat64Number _cmsAtan2(cmsFloat64Number y, cmsFloat64Number x)
+{
+ cmsFloat64Number a;
+
+ // Deal with undefined case
+ if (x == 0.0 && y == 0.0) return 0;
+
+ a = (atan2(y, x) * 180.0) / M_PI;
+
+ while (a < 0) {
+ a += 360;
+ }
+
+ return a;
+}
+
+// Convert to spherical coordinates
+static
+void ToSpherical(cmsSpherical* sp, const cmsVEC3* v)
+{
+
+ cmsFloat64Number L, a, b;
+
+ L = v ->n[VX];
+ a = v ->n[VY];
+ b = v ->n[VZ];
+
+ sp ->r = sqrt( L*L + a*a + b*b );
+
+ if (sp ->r == 0) {
+ sp ->alpha = sp ->theta = 0;
+ return;
+ }
+
+ sp ->alpha = _cmsAtan2(a, b);
+ sp ->theta = _cmsAtan2(sqrt(a*a + b*b), L);
+}
+
+
+// Convert to cartesian from spherical
+static
+void ToCartesian(cmsVEC3* v, const cmsSpherical* sp)
+{
+ cmsFloat64Number sin_alpha;
+ cmsFloat64Number cos_alpha;
+ cmsFloat64Number sin_theta;
+ cmsFloat64Number cos_theta;
+ cmsFloat64Number L, a, b;
+
+ sin_alpha = sin((M_PI * sp ->alpha) / 180.0);
+ cos_alpha = cos((M_PI * sp ->alpha) / 180.0);
+ sin_theta = sin((M_PI * sp ->theta) / 180.0);
+ cos_theta = cos((M_PI * sp ->theta) / 180.0);
+
+ a = sp ->r * sin_theta * sin_alpha;
+ b = sp ->r * sin_theta * cos_alpha;
+ L = sp ->r * cos_theta;
+
+ v ->n[VX] = L;
+ v ->n[VY] = a;
+ v ->n[VZ] = b;
+}
+
+
+// Quantize sector of a spherical coordinate. Saturate 360, 180 to last sector
+// The limits are the centers of each sector, so
+static
+void QuantizeToSector(const cmsSpherical* sp, int* alpha, int* theta)
+{
+ *alpha = (int) floor(((sp->alpha * (SECTORS)) / 360.0) );
+ *theta = (int) floor(((sp->theta * (SECTORS)) / 180.0) );
+
+ if (*alpha >= SECTORS)
+ *alpha = SECTORS-1;
+ if (*theta >= SECTORS)
+ *theta = SECTORS-1;
+}
+
+
+// Line determined by 2 points
+static
+void LineOf2Points(cmsLine* line, cmsVEC3* a, cmsVEC3* b)
+{
+
+ _cmsVEC3init(&line ->a, a ->n[VX], a ->n[VY], a ->n[VZ]);
+ _cmsVEC3init(&line ->u, b ->n[VX] - a ->n[VX],
+ b ->n[VY] - a ->n[VY],
+ b ->n[VZ] - a ->n[VZ]);
+}
+
+
+// Evaluate parametric line
+static
+void GetPointOfLine(cmsVEC3* p, const cmsLine* line, cmsFloat64Number t)
+{
+ p ->n[VX] = line ->a.n[VX] + t * line->u.n[VX];
+ p ->n[VY] = line ->a.n[VY] + t * line->u.n[VY];
+ p ->n[VZ] = line ->a.n[VZ] + t * line->u.n[VZ];
+}
+
+
+
+/*
+ Closest point in sector line1 to sector line2 (both are defined as 0 <=t <= 1)
+ http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
+
+ Copyright 2001, softSurfer (www.softsurfer.com)
+ This code may be freely used and modified for any purpose
+ providing that this copyright notice is included with it.
+ SoftSurfer makes no warranty for this code, and cannot be held
+ liable for any real or imagined damage resulting from its use.
+ Users of this code must verify correctness for their application.
+
+*/
+
+static
+cmsBool ClosestLineToLine(cmsVEC3* r, const cmsLine* line1, const cmsLine* line2)
+{
+ cmsFloat64Number a, b, c, d, e, D;
+ cmsFloat64Number sc, sN, sD;
+ cmsFloat64Number tc, tN, tD;
+ cmsVEC3 w0;
+
+ _cmsVEC3minus(&w0, &line1 ->a, &line2 ->a);
+
+ a = _cmsVEC3dot(&line1 ->u, &line1 ->u);
+ b = _cmsVEC3dot(&line1 ->u, &line2 ->u);
+ c = _cmsVEC3dot(&line2 ->u, &line2 ->u);
+ d = _cmsVEC3dot(&line1 ->u, &w0);
+ e = _cmsVEC3dot(&line2 ->u, &w0);
+
+ D = a*c - b * b; // Denominator
+ sD = tD = D; // default sD = D >= 0
+
+ if (D < MATRIX_DET_TOLERANCE) { // the lines are almost parallel
+
+ sN = 0.0; // force using point P0 on segment S1
+ sD = 1.0; // to prevent possible division by 0.0 later
+ tN = e;
+ tD = c;
+ }
+ else { // get the closest points on the infinite lines
+
+ sN = (b*e - c*d);
+ tN = (a*e - b*d);
+
+ if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
+
+ sN = 0.0;
+ tN = e;
+ tD = c;
+ }
+ else if (sN > sD) { // sc > 1 => the s=1 edge is visible
+ sN = sD;
+ tN = e + b;
+ tD = c;
+ }
+ }
+
+ if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
+
+ tN = 0.0;
+ // recompute sc for this edge
+ if (-d < 0.0)
+ sN = 0.0;
+ else if (-d > a)
+ sN = sD;
+ else {
+ sN = -d;
+ sD = a;
+ }
+ }
+ else if (tN > tD) { // tc > 1 => the t=1 edge is visible
+
+ tN = tD;
+
+ // recompute sc for this edge
+ if ((-d + b) < 0.0)
+ sN = 0;
+ else if ((-d + b) > a)
+ sN = sD;
+ else {
+ sN = (-d + b);
+ sD = a;
+ }
+ }
+ // finally do the division to get sc and tc
+ sc = (fabs(sN) < MATRIX_DET_TOLERANCE ? 0.0 : sN / sD);
+ tc = (fabs(tN) < MATRIX_DET_TOLERANCE ? 0.0 : tN / tD);
+
+ GetPointOfLine(r, line1, sc);
+ return TRUE;
+}
+
+
+
+// ------------------------------------------------------------------ Wrapper
+
+
+// Allocate & free structure
+cmsHANDLE CMSEXPORT cmsGBDAlloc(cmsContext ContextID)
+{
+ cmsGDB* gbd = (cmsGDB*) _cmsMallocZero(ContextID, sizeof(cmsGDB));
+ if (gbd == NULL) return NULL;
+
+ gbd -> ContextID = ContextID;
+
+ return (cmsHANDLE) gbd;
+}
+
+
+void CMSEXPORT cmsGBDFree(cmsHANDLE hGBD)
+{
+ cmsGDB* gbd = (cmsGDB*) hGBD;
+ if (hGBD != NULL)
+ _cmsFree(gbd->ContextID, (void*) gbd);
+}
+
+
+// Auxiliar to retrieve a pointer to the segmentr containing the Lab value
+static
+cmsGDBPoint* GetPoint(cmsGDB* gbd, const cmsCIELab* Lab, cmsSpherical* sp)
+{
+ cmsVEC3 v;
+ int alpha, theta;
+
+ // Housekeeping
+ _cmsAssert(gbd != NULL);
+ _cmsAssert(Lab != NULL);
+ _cmsAssert(sp != NULL);
+
+ // Center L* by substracting half of its domain, that's 50
+ _cmsVEC3init(&v, Lab ->L - 50.0, Lab ->a, Lab ->b);
+
+ // Convert to spherical coordinates
+ ToSpherical(sp, &v);
+
+ if (sp ->r < 0 || sp ->alpha < 0 || sp->theta < 0) {
+ cmsSignalError(gbd ->ContextID, cmsERROR_RANGE, "spherical value out of range");
+ return NULL;
+ }
+
+ // On which sector it falls?
+ QuantizeToSector(sp, &alpha, &theta);
+
+ if (alpha < 0 || theta < 0 || alpha >= SECTORS || theta >= SECTORS) {
+ cmsSignalError(gbd ->ContextID, cmsERROR_RANGE, " quadrant out of range");
+ return NULL;
+ }
+
+ // Get pointer to the sector
+ return &gbd ->Gamut[theta][alpha];
+}
+
+// Add a point to gamut descriptor. Point to add is in Lab color space.
+// GBD is centered on a=b=0 and L*=50
+cmsBool CMSEXPORT cmsGDBAddPoint(cmsHANDLE hGBD, const cmsCIELab* Lab)
+{
+ cmsGDB* gbd = (cmsGDB*) hGBD;
+ cmsGDBPoint* ptr;
+ cmsSpherical sp;
+
+
+ // Get pointer to the sector
+ ptr = GetPoint(gbd, Lab, &sp);
+ if (ptr == NULL) return FALSE;
+
+ // If no samples at this sector, add it
+ if (ptr ->Type == GP_EMPTY) {
+
+ ptr -> Type = GP_SPECIFIED;
+ ptr -> p = sp;
+ }
+ else {
+
+
+ // Substitute only if radius is greater
+ if (sp.r > ptr -> p.r) {
+
+ ptr -> Type = GP_SPECIFIED;
+ ptr -> p = sp;
+ }
+ }
+
+ return TRUE;
+}
+
+// Check if a given point falls inside gamut
+cmsBool CMSEXPORT cmsGDBCheckPoint(cmsHANDLE hGBD, const cmsCIELab* Lab)
+{
+ cmsGDB* gbd = (cmsGDB*) hGBD;
+ cmsGDBPoint* ptr;
+ cmsSpherical sp;
+
+ // Get pointer to the sector
+ ptr = GetPoint(gbd, Lab, &sp);
+ if (ptr == NULL) return FALSE;
+
+ // If no samples at this sector, return no data
+ if (ptr ->Type == GP_EMPTY) return FALSE;
+
+ // In gamut only if radius is greater
+
+ return (sp.r <= ptr -> p.r);
+}
+
+// -----------------------------------------------------------------------------------------------------------------------
+
+// Find near sectors. The list of sectors found is returned on Close[].
+// The function returns the number of sectors as well.
+
+// 24 9 10 11 12
+// 23 8 1 2 13
+// 22 7 * 3 14
+// 21 6 5 4 15
+// 20 19 18 17 16
+//
+// Those are the relative movements
+// {-2,-2}, {-1, -2}, {0, -2}, {+1, -2}, {+2, -2},
+// {-2,-1}, {-1, -1}, {0, -1}, {+1, -1}, {+2, -1},
+// {-2, 0}, {-1, 0}, {0, 0}, {+1, 0}, {+2, 0},
+// {-2,+1}, {-1, +1}, {0, +1}, {+1, +1}, {+2, +1},
+// {-2,+2}, {-1, +2}, {0, +2}, {+1, +2}, {+2, +2}};
+
+
+static
+const struct _spiral {
+
+ int AdvX, AdvY;
+
+ } Spiral[] = { {0, -1}, {+1, -1}, {+1, 0}, {+1, +1}, {0, +1}, {-1, +1},
+ {-1, 0}, {-1, -1}, {-1, -2}, {0, -2}, {+1, -2}, {+2, -2},
+ {+2, -1}, {+2, 0}, {+2, +1}, {+2, +2}, {+1, +2}, {0, +2},
+ {-1, +2}, {-2, +2}, {-2, +1}, {-2, 0}, {-2, -1}, {-2, -2} };
+
+#define NSTEPS (sizeof(Spiral) / sizeof(struct _spiral))
+
+static
+int FindNearSectors(cmsGDB* gbd, int alpha, int theta, cmsGDBPoint* Close[])
+{
+ int nSectors = 0;
+ int a, t;
+ cmsUInt32Number i;
+ cmsGDBPoint* pt;
+
+ for (i=0; i < NSTEPS; i++) {
+
+ a = alpha + Spiral[i].AdvX;
+ t = theta + Spiral[i].AdvY;
+
+ // Cycle at the end
+ a %= SECTORS;
+ t %= SECTORS;
+
+ // Cycle at the begin
+ if (a < 0) a = SECTORS + a;
+ if (t < 0) t = SECTORS + t;
+
+ pt = &gbd ->Gamut[t][a];
+
+ if (pt -> Type != GP_EMPTY) {
+
+ Close[nSectors++] = pt;
+ }
+ }
+
+ return nSectors;
+}
+
+
+// Interpolate a missing sector. Method identifies whatever this is top, bottom or mid
+static
+cmsBool InterpolateMissingSector(cmsGDB* gbd, int alpha, int theta)
+{
+ cmsSpherical sp;
+ cmsVEC3 Lab;
+ cmsVEC3 Centre;
+ cmsLine ray;
+ int nCloseSectors;
+ cmsGDBPoint* Close[NSTEPS + 1];
+ cmsSpherical closel, templ;
+ cmsLine edge;
+ int k, m;
+
+ // Is that point already specified?
+ if (gbd ->Gamut[theta][alpha].Type != GP_EMPTY) return TRUE;
+
+ // Fill close points
+ nCloseSectors = FindNearSectors(gbd, alpha, theta, Close);
+
+
+ // Find a central point on the sector
+ sp.alpha = (cmsFloat64Number) ((alpha + 0.5) * 360.0) / (SECTORS);
+ sp.theta = (cmsFloat64Number) ((theta + 0.5) * 180.0) / (SECTORS);
+ sp.r = 50.0;
+
+ // Convert to Cartesian
+ ToCartesian(&Lab, &sp);
+
+ // Create a ray line from centre to this point
+ _cmsVEC3init(&Centre, 50.0, 0, 0);
+ LineOf2Points(&ray, &Lab, &Centre);
+
+ // For all close sectors
+ closel.r = 0.0;
+ closel.alpha = 0;
+ closel.theta = 0;
+
+ for (k=0; k < nCloseSectors; k++) {
+
+ for(m = k+1; m < nCloseSectors; m++) {
+
+ cmsVEC3 temp, a1, a2;
+
+ // A line from sector to sector
+ ToCartesian(&a1, &Close[k]->p);
+ ToCartesian(&a2, &Close[m]->p);
+
+ LineOf2Points(&edge, &a1, &a2);
+
+ // Find a line
+ ClosestLineToLine(&temp, &ray, &edge);
+
+ // Convert to spherical
+ ToSpherical(&templ, &temp);
+
+
+ if ( templ.r > closel.r &&
+ templ.theta >= (theta*180.0/SECTORS) &&
+ templ.theta <= ((theta+1)*180.0/SECTORS) &&
+ templ.alpha >= (alpha*360.0/SECTORS) &&
+ templ.alpha <= ((alpha+1)*360.0/SECTORS)) {
+
+ closel = templ;
+ }
+ }
+ }
+
+ gbd ->Gamut[theta][alpha].p = closel;
+ gbd ->Gamut[theta][alpha].Type = GP_MODELED;
+
+ return TRUE;
+
+}
+
+
+// Interpolate missing parts. The algorithm fist computes slices at
+// theta=0 and theta=Max.
+cmsBool CMSEXPORT cmsGDBCompute(cmsHANDLE hGBD, cmsUInt32Number dwFlags)
+{
+ int alpha, theta;
+ cmsGDB* gbd = (cmsGDB*) hGBD;
+
+ _cmsAssert(hGBD != NULL);
+
+ // Interpolate black
+ for (alpha = 0; alpha < SECTORS; alpha++) {
+
+ if (!InterpolateMissingSector(gbd, alpha, 0)) return FALSE;
+ }
+
+ // Interpolate white
+ for (alpha = 0; alpha < SECTORS; alpha++) {
+
+ if (!InterpolateMissingSector(gbd, alpha, SECTORS-1)) return FALSE;
+ }
+
+
+ // Interpolate Mid
+ for (theta = 1; theta < SECTORS; theta++) {
+ for (alpha = 0; alpha < SECTORS; alpha++) {
+
+ if (!InterpolateMissingSector(gbd, alpha, theta)) return FALSE;
+ }
+ }
+
+ // Done
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(dwFlags);
+}
+
+
+
+
+// --------------------------------------------------------------------------------------------------------
+
+// Great for debug, but not suitable for real use
+
+#if 0
+cmsBool cmsGBDdumpVRML(cmsHANDLE hGBD, const char* fname)
+{
+ FILE* fp;
+ int i, j;
+ cmsGDB* gbd = (cmsGDB*) hGBD;
+ cmsGDBPoint* pt;
+
+ fp = fopen (fname, "wt");
+ if (fp == NULL)
+ return FALSE;
+
+ fprintf (fp, "#VRML V2.0 utf8\n");
+
+ // set the viewing orientation and distance
+ fprintf (fp, "DEF CamTest Group {\n");
+ fprintf (fp, "\tchildren [\n");
+ fprintf (fp, "\t\tDEF Cameras Group {\n");
+ fprintf (fp, "\t\t\tchildren [\n");
+ fprintf (fp, "\t\t\t\tDEF DefaultView Viewpoint {\n");
+ fprintf (fp, "\t\t\t\t\tposition 0 0 340\n");
+ fprintf (fp, "\t\t\t\t\torientation 0 0 1 0\n");
+ fprintf (fp, "\t\t\t\t\tdescription \"default view\"\n");
+ fprintf (fp, "\t\t\t\t}\n");
+ fprintf (fp, "\t\t\t]\n");
+ fprintf (fp, "\t\t},\n");
+ fprintf (fp, "\t]\n");
+ fprintf (fp, "}\n");
+
+ // Output the background stuff
+ fprintf (fp, "Background {\n");
+ fprintf (fp, "\tskyColor [\n");
+ fprintf (fp, "\t\t.5 .5 .5\n");
+ fprintf (fp, "\t]\n");
+ fprintf (fp, "}\n");
+
+ // Output the shape stuff
+ fprintf (fp, "Transform {\n");
+ fprintf (fp, "\tscale .3 .3 .3\n");
+ fprintf (fp, "\tchildren [\n");
+
+ // Draw the axes as a shape:
+ fprintf (fp, "\t\tShape {\n");
+ fprintf (fp, "\t\t\tappearance Appearance {\n");
+ fprintf (fp, "\t\t\t\tmaterial Material {\n");
+ fprintf (fp, "\t\t\t\t\tdiffuseColor 0 0.8 0\n");
+ fprintf (fp, "\t\t\t\t\temissiveColor 1.0 1.0 1.0\n");
+ fprintf (fp, "\t\t\t\t\tshininess 0.8\n");
+ fprintf (fp, "\t\t\t\t}\n");
+ fprintf (fp, "\t\t\t}\n");
+ fprintf (fp, "\t\t\tgeometry IndexedLineSet {\n");
+ fprintf (fp, "\t\t\t\tcoord Coordinate {\n");
+ fprintf (fp, "\t\t\t\t\tpoint [\n");
+ fprintf (fp, "\t\t\t\t\t0.0 0.0 0.0,\n");
+ fprintf (fp, "\t\t\t\t\t%f 0.0 0.0,\n", 255.0);
+ fprintf (fp, "\t\t\t\t\t0.0 %f 0.0,\n", 255.0);
+ fprintf (fp, "\t\t\t\t\t0.0 0.0 %f]\n", 255.0);
+ fprintf (fp, "\t\t\t\t}\n");
+ fprintf (fp, "\t\t\t\tcoordIndex [\n");
+ fprintf (fp, "\t\t\t\t\t0, 1, -1\n");
+ fprintf (fp, "\t\t\t\t\t0, 2, -1\n");
+ fprintf (fp, "\t\t\t\t\t0, 3, -1]\n");
+ fprintf (fp, "\t\t\t}\n");
+ fprintf (fp, "\t\t}\n");
+
+
+ fprintf (fp, "\t\tShape {\n");
+ fprintf (fp, "\t\t\tappearance Appearance {\n");
+ fprintf (fp, "\t\t\t\tmaterial Material {\n");
+ fprintf (fp, "\t\t\t\t\tdiffuseColor 0 0.8 0\n");
+ fprintf (fp, "\t\t\t\t\temissiveColor 1 1 1\n");
+ fprintf (fp, "\t\t\t\t\tshininess 0.8\n");
+ fprintf (fp, "\t\t\t\t}\n");
+ fprintf (fp, "\t\t\t}\n");
+ fprintf (fp, "\t\t\tgeometry PointSet {\n");
+
+ // fill in the points here
+ fprintf (fp, "\t\t\t\tcoord Coordinate {\n");
+ fprintf (fp, "\t\t\t\t\tpoint [\n");
+
+ // We need to transverse all gamut hull.
+ for (i=0; i < SECTORS; i++)
+ for (j=0; j < SECTORS; j++) {
+
+ cmsVEC3 v;
+
+ pt = &gbd ->Gamut[i][j];
+ ToCartesian(&v, &pt ->p);
+
+ fprintf (fp, "\t\t\t\t\t%g %g %g", v.n[0]+50, v.n[1], v.n[2]);
+
+ if ((j == SECTORS - 1) && (i == SECTORS - 1))
+ fprintf (fp, "]\n");
+ else
+ fprintf (fp, ",\n");
+
+ }
+
+ fprintf (fp, "\t\t\t\t}\n");
+
+
+
+ // fill in the face colors
+ fprintf (fp, "\t\t\t\tcolor Color {\n");
+ fprintf (fp, "\t\t\t\t\tcolor [\n");
+
+ for (i=0; i < SECTORS; i++)
+ for (j=0; j < SECTORS; j++) {
+
+ cmsVEC3 v;
+
+ pt = &gbd ->Gamut[i][j];
+
+
+ ToCartesian(&v, &pt ->p);
+
+
+ if (pt ->Type == GP_EMPTY)
+ fprintf (fp, "\t\t\t\t\t%g %g %g", 0.0, 0.0, 0.0);
+ else
+ if (pt ->Type == GP_MODELED)
+ fprintf (fp, "\t\t\t\t\t%g %g %g", 1.0, .5, .5);
+ else {
+ fprintf (fp, "\t\t\t\t\t%g %g %g", 1.0, 1.0, 1.0);
+
+ }
+
+ if ((j == SECTORS - 1) && (i == SECTORS - 1))
+ fprintf (fp, "]\n");
+ else
+ fprintf (fp, ",\n");
+ }
+ fprintf (fp, "\t\t\t}\n");
+
+
+ fprintf (fp, "\t\t\t}\n");
+ fprintf (fp, "\t\t}\n");
+ fprintf (fp, "\t]\n");
+ fprintf (fp, "}\n");
+
+ fclose (fp);
+
+ return TRUE;
+}
+#endif
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmstypes.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmstypes.c
new file mode 100644
index 0000000000..06742b5ad6
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmstypes.c
@@ -0,0 +1,5564 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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"
+
+// Tag Serialization -----------------------------------------------------------------------------
+// This file implements every single tag and tag type as described in the ICC spec. Some types
+// have been deprecated, like ncl and Data. There is no implementation for those types as there
+// are no profiles holding them. The programmer can also extend this list by defining his own types
+// by using the appropiate plug-in. There are three types of plug ins regarding that. First type
+// allows to define new tags using any existing type. Next plug-in type allows to define new types
+// and the third one is very specific: allows to extend the number of elements in the multiprocessing
+// elements special type.
+//--------------------------------------------------------------------------------------------------
+
+// Some broken types
+#define cmsCorbisBrokenXYZtype ((cmsTagTypeSignature) 0x17A505B8)
+#define cmsMonacoBrokenCurveType ((cmsTagTypeSignature) 0x9478ee00)
+
+// This is the linked list that keeps track of the defined types
+typedef struct _cmsTagTypeLinkedList_st {
+
+ cmsTagTypeHandler Handler;
+ struct _cmsTagTypeLinkedList_st* Next;
+
+} _cmsTagTypeLinkedList;
+
+// Some macros to define callbacks.
+#define READ_FN(x) Type_##x##_Read
+#define WRITE_FN(x) Type_##x##_Write
+#define FREE_FN(x) Type_##x##_Free
+#define DUP_FN(x) Type_##x##_Dup
+
+// Helper macro to define a handler. Callbacks do have a fixed naming convention.
+#define TYPE_HANDLER(t, x) { (t), READ_FN(x), WRITE_FN(x), DUP_FN(x), FREE_FN(x), NULL, 0 }
+
+// Helper macro to define a MPE handler. Callbacks do have a fixed naming convention
+#define TYPE_MPE_HANDLER(t, x) { (t), READ_FN(x), WRITE_FN(x), GenericMPEdup, GenericMPEfree, NULL, 0 }
+
+// Register a new type handler. This routine is shared between normal types and MPE. LinkedList points to the optional list head
+static
+cmsBool RegisterTypesPlugin(cmsContext id, cmsPluginBase* Data, _cmsMemoryClient pos)
+{
+ cmsPluginTagType* Plugin = (cmsPluginTagType*) Data;
+ _cmsTagTypePluginChunkType* ctx = ( _cmsTagTypePluginChunkType*) _cmsContextGetClientChunk(id, pos);
+ _cmsTagTypeLinkedList *pt;
+
+ // Calling the function with NULL as plug-in would unregister the plug in.
+ if (Data == NULL) {
+
+ // There is no need to set free the memory, as pool is destroyed as a whole.
+ ctx ->TagTypes = NULL;
+ return TRUE;
+ }
+
+ // Registering happens in plug-in memory pool.
+ pt = (_cmsTagTypeLinkedList*) _cmsPluginMalloc(id, sizeof(_cmsTagTypeLinkedList));
+ if (pt == NULL) return FALSE;
+
+ pt ->Handler = Plugin ->Handler;
+ pt ->Next = ctx ->TagTypes;
+
+ ctx ->TagTypes = pt;
+
+ return TRUE;
+}
+
+// Return handler for a given type or NULL if not found. Shared between normal types and MPE. It first tries the additons
+// made by plug-ins and then the built-in defaults.
+static
+cmsTagTypeHandler* GetHandler(cmsTagTypeSignature sig, _cmsTagTypeLinkedList* PluginLinkedList, _cmsTagTypeLinkedList* DefaultLinkedList)
+{
+ _cmsTagTypeLinkedList* pt;
+
+ for (pt = PluginLinkedList;
+ pt != NULL;
+ pt = pt ->Next) {
+
+ if (sig == pt -> Handler.Signature) return &pt ->Handler;
+ }
+
+ for (pt = DefaultLinkedList;
+ pt != NULL;
+ pt = pt ->Next) {
+
+ if (sig == pt -> Handler.Signature) return &pt ->Handler;
+ }
+
+ return NULL;
+}
+
+
+// Auxiliar to convert UTF-32 to UTF-16 in some cases
+static
+cmsBool _cmsWriteWCharArray(cmsIOHANDLER* io, cmsUInt32Number n, const wchar_t* Array)
+{
+ cmsUInt32Number i;
+
+ _cmsAssert(io != NULL);
+ _cmsAssert(!(Array == NULL && n > 0));
+
+ for (i=0; i < n; i++) {
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) Array[i])) return FALSE;
+ }
+
+ return TRUE;
+}
+
+// Auxiliar to read an array of wchar_t
+static
+cmsBool _cmsReadWCharArray(cmsIOHANDLER* io, cmsUInt32Number n, wchar_t* Array)
+{
+ cmsUInt32Number i;
+ cmsUInt16Number tmp;
+
+ _cmsAssert(io != NULL);
+
+ for (i=0; i < n; i++) {
+
+ if (Array != NULL) {
+
+ if (!_cmsReadUInt16Number(io, &tmp)) return FALSE;
+ Array[i] = (wchar_t) tmp;
+ }
+ else {
+ if (!_cmsReadUInt16Number(io, NULL)) return FALSE;
+ }
+
+ }
+ return TRUE;
+}
+
+// To deal with position tables
+typedef cmsBool (* PositionTableEntryFn)(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag);
+
+// Helper function to deal with position tables as decribed in ICC spec 4.3
+// A table of n elements is readed, where first comes n records containing offsets and sizes and
+// then a block containing the data itself. This allows to reuse same data in more than one entry
+static
+cmsBool ReadPositionTable(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ cmsUInt32Number Count,
+ cmsUInt32Number BaseOffset,
+ void *Cargo,
+ PositionTableEntryFn ElementFn)
+{
+ cmsUInt32Number i;
+ cmsUInt32Number *ElementOffsets = NULL, *ElementSizes = NULL;
+
+ // Let's take the offsets to each element
+ ElementOffsets = (cmsUInt32Number *) _cmsCalloc(io ->ContextID, Count, sizeof(cmsUInt32Number));
+ if (ElementOffsets == NULL) goto Error;
+
+ ElementSizes = (cmsUInt32Number *) _cmsCalloc(io ->ContextID, Count, sizeof(cmsUInt32Number));
+ if (ElementSizes == NULL) goto Error;
+
+ for (i=0; i < Count; i++) {
+
+ if (!_cmsReadUInt32Number(io, &ElementOffsets[i])) goto Error;
+ if (!_cmsReadUInt32Number(io, &ElementSizes[i])) goto Error;
+
+ ElementOffsets[i] += BaseOffset;
+ }
+
+ // Seek to each element and read it
+ for (i=0; i < Count; i++) {
+
+ if (!io -> Seek(io, ElementOffsets[i])) goto Error;
+
+ // This is the reader callback
+ if (!ElementFn(self, io, Cargo, i, ElementSizes[i])) goto Error;
+ }
+
+ // Success
+ if (ElementOffsets != NULL) _cmsFree(io ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(io ->ContextID, ElementSizes);
+ return TRUE;
+
+Error:
+ if (ElementOffsets != NULL) _cmsFree(io ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(io ->ContextID, ElementSizes);
+ return FALSE;
+}
+
+// Same as anterior, but for write position tables
+static
+cmsBool WritePositionTable(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ cmsUInt32Number SizeOfTag,
+ cmsUInt32Number Count,
+ cmsUInt32Number BaseOffset,
+ void *Cargo,
+ PositionTableEntryFn ElementFn)
+{
+ cmsUInt32Number i;
+ cmsUInt32Number DirectoryPos, CurrentPos, Before;
+ cmsUInt32Number *ElementOffsets = NULL, *ElementSizes = NULL;
+
+ // Create table
+ ElementOffsets = (cmsUInt32Number *) _cmsCalloc(io ->ContextID, Count, sizeof(cmsUInt32Number));
+ if (ElementOffsets == NULL) goto Error;
+
+ ElementSizes = (cmsUInt32Number *) _cmsCalloc(io ->ContextID, Count, sizeof(cmsUInt32Number));
+ if (ElementSizes == NULL) goto Error;
+
+ // Keep starting position of curve offsets
+ DirectoryPos = io ->Tell(io);
+
+ // Write a fake directory to be filled latter on
+ for (i=0; i < Count; i++) {
+
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error; // Offset
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error; // size
+ }
+
+ // Write each element. Keep track of the size as well.
+ for (i=0; i < Count; i++) {
+
+ Before = io ->Tell(io);
+ ElementOffsets[i] = Before - BaseOffset;
+
+ // Callback to write...
+ if (!ElementFn(self, io, Cargo, i, SizeOfTag)) goto Error;
+
+ // Now the size
+ ElementSizes[i] = io ->Tell(io) - Before;
+ }
+
+ // Write the directory
+ CurrentPos = io ->Tell(io);
+ if (!io ->Seek(io, DirectoryPos)) goto Error;
+
+ for (i=0; i < Count; i++) {
+ if (!_cmsWriteUInt32Number(io, ElementOffsets[i])) goto Error;
+ if (!_cmsWriteUInt32Number(io, ElementSizes[i])) goto Error;
+ }
+
+ if (!io ->Seek(io, CurrentPos)) goto Error;
+
+ if (ElementOffsets != NULL) _cmsFree(io ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(io ->ContextID, ElementSizes);
+ return TRUE;
+
+Error:
+ if (ElementOffsets != NULL) _cmsFree(io ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(io ->ContextID, ElementSizes);
+ return FALSE;
+}
+
+
+// ********************************************************************************
+// Type XYZ. Only one value is allowed
+// ********************************************************************************
+
+//The XYZType contains an array of three encoded values for the XYZ tristimulus
+//values. Tristimulus values must be non-negative. The signed encoding allows for
+//implementation optimizations by minimizing the number of fixed formats.
+
+
+static
+void *Type_XYZ_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsCIEXYZ* xyz;
+
+ *nItems = 0;
+ xyz = (cmsCIEXYZ*) _cmsMallocZero(self ->ContextID, sizeof(cmsCIEXYZ));
+ if (xyz == NULL) return NULL;
+
+ if (!_cmsReadXYZNumber(io, xyz)) {
+ _cmsFree(self ->ContextID, xyz);
+ return NULL;
+ }
+
+ *nItems = 1;
+ return (void*) xyz;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool Type_XYZ_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ return _cmsWriteXYZNumber(io, (cmsCIEXYZ*) Ptr);
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_XYZ_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsCIEXYZ));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_XYZ_Free(struct _cms_typehandler_struct* self, void *Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+static
+cmsTagTypeSignature DecideXYZtype(cmsFloat64Number ICCVersion, const void *Data)
+{
+ return cmsSigXYZType;
+
+ cmsUNUSED_PARAMETER(ICCVersion);
+ cmsUNUSED_PARAMETER(Data);
+}
+
+
+// ********************************************************************************
+// Type chromaticity. Only one value is allowed
+// ********************************************************************************
+// The chromaticity tag type provides basic chromaticity data and type of
+// phosphors or colorants of a monitor to applications and utilities.
+
+static
+void *Type_Chromaticity_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsCIExyYTRIPLE* chrm;
+ cmsUInt16Number nChans, Table;
+
+ *nItems = 0;
+ chrm = (cmsCIExyYTRIPLE*) _cmsMallocZero(self ->ContextID, sizeof(cmsCIExyYTRIPLE));
+ if (chrm == NULL) return NULL;
+
+ if (!_cmsReadUInt16Number(io, &nChans)) goto Error;
+
+ // Let's recover from a bug introduced in early versions of lcms1
+ if (nChans == 0 && SizeOfTag == 32) {
+
+ if (!_cmsReadUInt16Number(io, NULL)) goto Error;
+ if (!_cmsReadUInt16Number(io, &nChans)) goto Error;
+ }
+
+ if (nChans != 3) goto Error;
+
+ if (!_cmsReadUInt16Number(io, &Table)) goto Error;
+
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Red.x)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Red.y)) goto Error;
+
+ chrm ->Red.Y = 1.0;
+
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Green.x)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Green.y)) goto Error;
+
+ chrm ->Green.Y = 1.0;
+
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Blue.x)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &chrm ->Blue.y)) goto Error;
+
+ chrm ->Blue.Y = 1.0;
+
+ *nItems = 1;
+ return (void*) chrm;
+
+Error:
+ _cmsFree(self ->ContextID, (void*) chrm);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool SaveOneChromaticity(cmsFloat64Number x, cmsFloat64Number y, cmsIOHANDLER* io)
+{
+ if (!_cmsWriteUInt32Number(io, _cmsDoubleTo15Fixed16(x))) return FALSE;
+ if (!_cmsWriteUInt32Number(io, _cmsDoubleTo15Fixed16(y))) return FALSE;
+
+ return TRUE;
+}
+
+static
+cmsBool Type_Chromaticity_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsCIExyYTRIPLE* chrm = (cmsCIExyYTRIPLE*) Ptr;
+
+ if (!_cmsWriteUInt16Number(io, 3)) return FALSE; // nChannels
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE; // Table
+
+ if (!SaveOneChromaticity(chrm -> Red.x, chrm -> Red.y, io)) return FALSE;
+ if (!SaveOneChromaticity(chrm -> Green.x, chrm -> Green.y, io)) return FALSE;
+ if (!SaveOneChromaticity(chrm -> Blue.x, chrm -> Blue.y, io)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_Chromaticity_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsCIExyYTRIPLE));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_Chromaticity_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+// ********************************************************************************
+// Type cmsSigColorantOrderType
+// ********************************************************************************
+
+// This is an optional tag which specifies the laydown order in which colorants will
+// be printed on an n-colorant device. The laydown order may be the same as the
+// channel generation order listed in the colorantTableTag or the channel order of a
+// colour space such as CMYK, in which case this tag is not needed. When this is not
+// the case (for example, ink-towers sometimes use the order KCMY), this tag may be
+// used to specify the laydown order of the colorants.
+
+
+static
+void *Type_ColorantOrderType_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt8Number* ColorantOrder;
+ cmsUInt32Number Count;
+
+ *nItems = 0;
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+ if (Count > cmsMAXCHANNELS) return NULL;
+
+ ColorantOrder = (cmsUInt8Number*) _cmsCalloc(self ->ContextID, cmsMAXCHANNELS, sizeof(cmsUInt8Number));
+ if (ColorantOrder == NULL) return NULL;
+
+ // We use FF as end marker
+ memset(ColorantOrder, 0xFF, cmsMAXCHANNELS * sizeof(cmsUInt8Number));
+
+ if (io ->Read(io, ColorantOrder, sizeof(cmsUInt8Number), Count) != Count) {
+
+ _cmsFree(self ->ContextID, (void*) ColorantOrder);
+ return NULL;
+ }
+
+ *nItems = 1;
+ return (void*) ColorantOrder;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool Type_ColorantOrderType_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt8Number* ColorantOrder = (cmsUInt8Number*) Ptr;
+ cmsUInt32Number i, sz, Count;
+
+ // Get the length
+ for (Count=i=0; i < cmsMAXCHANNELS; i++) {
+ if (ColorantOrder[i] != 0xFF) Count++;
+ }
+
+ if (!_cmsWriteUInt32Number(io, Count)) return FALSE;
+
+ sz = Count * sizeof(cmsUInt8Number);
+ if (!io -> Write(io, sz, ColorantOrder)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_ColorantOrderType_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, cmsMAXCHANNELS * sizeof(cmsUInt8Number));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+
+static
+void Type_ColorantOrderType_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigS15Fixed16ArrayType
+// ********************************************************************************
+// This type represents an array of generic 4-byte/32-bit fixed point quantity.
+// The number of values is determined from the size of the tag.
+
+static
+void *Type_S15Fixed16_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsFloat64Number* array_double;
+ cmsUInt32Number i, n;
+
+ *nItems = 0;
+ n = SizeOfTag / sizeof(cmsUInt32Number);
+ array_double = (cmsFloat64Number*) _cmsCalloc(self ->ContextID, n, sizeof(cmsFloat64Number));
+ if (array_double == NULL) return NULL;
+
+ for (i=0; i < n; i++) {
+
+ if (!_cmsRead15Fixed16Number(io, &array_double[i])) {
+
+ _cmsFree(self ->ContextID, array_double);
+ return NULL;
+ }
+ }
+
+ *nItems = n;
+ return (void*) array_double;
+}
+
+static
+cmsBool Type_S15Fixed16_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsFloat64Number* Value = (cmsFloat64Number*) Ptr;
+ cmsUInt32Number i;
+
+ for (i=0; i < nItems; i++) {
+
+ if (!_cmsWrite15Fixed16Number(io, Value[i])) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_S15Fixed16_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, n * sizeof(cmsFloat64Number));
+}
+
+
+static
+void Type_S15Fixed16_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigU16Fixed16ArrayType
+// ********************************************************************************
+// This type represents an array of generic 4-byte/32-bit quantity.
+// The number of values is determined from the size of the tag.
+
+
+static
+void *Type_U16Fixed16_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsFloat64Number* array_double;
+ cmsUInt32Number v;
+ cmsUInt32Number i, n;
+
+ *nItems = 0;
+ n = SizeOfTag / sizeof(cmsUInt32Number);
+ array_double = (cmsFloat64Number*) _cmsCalloc(self ->ContextID, n, sizeof(cmsFloat64Number));
+ if (array_double == NULL) return NULL;
+
+ for (i=0; i < n; i++) {
+
+ if (!_cmsReadUInt32Number(io, &v)) {
+ _cmsFree(self ->ContextID, (void*) array_double);
+ return NULL;
+ }
+
+ // Convert to cmsFloat64Number
+ array_double[i] = (cmsFloat64Number) (v / 65536.0);
+ }
+
+ *nItems = n;
+ return (void*) array_double;
+}
+
+static
+cmsBool Type_U16Fixed16_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsFloat64Number* Value = (cmsFloat64Number*) Ptr;
+ cmsUInt32Number i;
+
+ for (i=0; i < nItems; i++) {
+
+ cmsUInt32Number v = (cmsUInt32Number) floor(Value[i]*65536.0 + 0.5);
+
+ if (!_cmsWriteUInt32Number(io, v)) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_U16Fixed16_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, n * sizeof(cmsFloat64Number));
+}
+
+static
+void Type_U16Fixed16_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigSignatureType
+// ********************************************************************************
+//
+// The signatureType contains a four-byte sequence, Sequences of less than four
+// characters are padded at the end with spaces, 20h.
+// Typically this type is used for registered tags that can be displayed on many
+// development systems as a sequence of four characters.
+
+static
+void *Type_Signature_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsSignature* SigPtr = (cmsSignature*) _cmsMalloc(self ->ContextID, sizeof(cmsSignature));
+ if (SigPtr == NULL) return NULL;
+
+ if (!_cmsReadUInt32Number(io, SigPtr)) return NULL;
+ *nItems = 1;
+
+ return SigPtr;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool Type_Signature_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsSignature* SigPtr = (cmsSignature*) Ptr;
+
+ return _cmsWriteUInt32Number(io, *SigPtr);
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_Signature_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, n * sizeof(cmsSignature));
+}
+
+static
+void Type_Signature_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+// ********************************************************************************
+// Type cmsSigTextType
+// ********************************************************************************
+//
+// The textType is a simple text structure that contains a 7-bit ASCII text string.
+// The length of the string is obtained by subtracting 8 from the element size portion
+// of the tag itself. This string must be terminated with a 00h byte.
+
+static
+void *Type_Text_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ char* Text = NULL;
+ cmsMLU* mlu = NULL;
+
+ // Create a container
+ mlu = cmsMLUalloc(self ->ContextID, 1);
+ if (mlu == NULL) return NULL;
+
+ *nItems = 0;
+
+ // We need to store the "\0" at the end, so +1
+ if (SizeOfTag == UINT_MAX) goto Error;
+
+ Text = (char*) _cmsMalloc(self ->ContextID, SizeOfTag + 1);
+ if (Text == NULL) goto Error;
+
+ if (io -> Read(io, Text, sizeof(char), SizeOfTag) != SizeOfTag) goto Error;
+
+ // Make sure text is properly ended
+ Text[SizeOfTag] = 0;
+ *nItems = 1;
+
+ // Keep the result
+ if (!cmsMLUsetASCII(mlu, cmsNoLanguage, cmsNoCountry, Text)) goto Error;
+
+ _cmsFree(self ->ContextID, Text);
+ return (void*) mlu;
+
+Error:
+ if (mlu != NULL)
+ cmsMLUfree(mlu);
+ if (Text != NULL)
+ _cmsFree(self ->ContextID, Text);
+
+ return NULL;
+}
+
+// The conversion implies to choose a language. So, we choose the actual language.
+static
+cmsBool Type_Text_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsMLU* mlu = (cmsMLU*) Ptr;
+ cmsUInt32Number size;
+ cmsBool rc;
+ char* Text;
+
+ // Get the size of the string. Note there is an extra "\0" at the end
+ size = cmsMLUgetASCII(mlu, cmsNoLanguage, cmsNoCountry, NULL, 0);
+ if (size == 0) return FALSE; // Cannot be zero!
+
+ // Create memory
+ Text = (char*) _cmsMalloc(self ->ContextID, size);
+ if (Text == NULL) return FALSE;
+
+ cmsMLUgetASCII(mlu, cmsNoLanguage, cmsNoCountry, Text, size);
+
+ // Write it, including separator
+ rc = io ->Write(io, size, Text);
+
+ _cmsFree(self ->ContextID, Text);
+ return rc;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_Text_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsMLUdup((cmsMLU*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void Type_Text_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsMLU* mlu = (cmsMLU*) Ptr;
+ cmsMLUfree(mlu);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+cmsTagTypeSignature DecideTextType(cmsFloat64Number ICCVersion, const void *Data)
+{
+ if (ICCVersion >= 4.0)
+ return cmsSigMultiLocalizedUnicodeType;
+
+ return cmsSigTextType;
+
+ cmsUNUSED_PARAMETER(Data);
+}
+
+
+// ********************************************************************************
+// Type cmsSigDataType
+// ********************************************************************************
+
+// General purpose data type
+static
+void *Type_Data_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsICCData* BinData;
+ cmsUInt32Number LenOfData;
+
+ *nItems = 0;
+
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+
+ LenOfData = SizeOfTag - sizeof(cmsUInt32Number);
+ if (LenOfData > INT_MAX) return NULL;
+
+ BinData = (cmsICCData*) _cmsMalloc(self ->ContextID, sizeof(cmsICCData) + LenOfData - 1);
+ if (BinData == NULL) return NULL;
+
+ BinData ->len = LenOfData;
+ if (!_cmsReadUInt32Number(io, &BinData->flag)) {
+ _cmsFree(self ->ContextID, BinData);
+ return NULL;
+ }
+
+ if (io -> Read(io, BinData ->data, sizeof(cmsUInt8Number), LenOfData) != LenOfData) {
+
+ _cmsFree(self ->ContextID, BinData);
+ return NULL;
+ }
+
+ *nItems = 1;
+
+ return (void*) BinData;
+}
+
+
+static
+cmsBool Type_Data_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsICCData* BinData = (cmsICCData*) Ptr;
+
+ if (!_cmsWriteUInt32Number(io, BinData ->flag)) return FALSE;
+
+ return io ->Write(io, BinData ->len, BinData ->data);
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_Data_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ cmsICCData* BinData = (cmsICCData*) Ptr;
+
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsICCData) + BinData ->len - 1);
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_Data_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigTextDescriptionType
+// ********************************************************************************
+
+static
+void *Type_Text_Description_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ char* Text = NULL;
+ cmsMLU* mlu = NULL;
+ cmsUInt32Number AsciiCount;
+ cmsUInt32Number i, UnicodeCode, UnicodeCount;
+ cmsUInt16Number ScriptCodeCode, Dummy;
+ cmsUInt8Number ScriptCodeCount;
+
+ *nItems = 0;
+
+ // One dword should be there
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+
+ // Read len of ASCII
+ if (!_cmsReadUInt32Number(io, &AsciiCount)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ // Check for size
+ if (SizeOfTag < AsciiCount) return NULL;
+
+ // All seems Ok, allocate the container
+ mlu = cmsMLUalloc(self ->ContextID, 1);
+ if (mlu == NULL) return NULL;
+
+ // As many memory as size of tag
+ Text = (char*) _cmsMalloc(self ->ContextID, AsciiCount + 1);
+ if (Text == NULL) goto Error;
+
+ // Read it
+ if (io ->Read(io, Text, sizeof(char), AsciiCount) != AsciiCount) goto Error;
+ SizeOfTag -= AsciiCount;
+
+ // Make sure there is a terminator
+ Text[AsciiCount] = 0;
+
+ // Set the MLU entry. From here we can be tolerant to wrong types
+ if (!cmsMLUsetASCII(mlu, cmsNoLanguage, cmsNoCountry, Text)) goto Error;
+ _cmsFree(self ->ContextID, (void*) Text);
+ Text = NULL;
+
+ // Skip Unicode code
+ if (SizeOfTag < 2* sizeof(cmsUInt32Number)) goto Done;
+ if (!_cmsReadUInt32Number(io, &UnicodeCode)) goto Done;
+ if (!_cmsReadUInt32Number(io, &UnicodeCount)) goto Done;
+ SizeOfTag -= 2* sizeof(cmsUInt32Number);
+
+ if (SizeOfTag < UnicodeCount*sizeof(cmsUInt16Number)) goto Done;
+
+ for (i=0; i < UnicodeCount; i++) {
+ if (!io ->Read(io, &Dummy, sizeof(cmsUInt16Number), 1)) goto Done;
+ }
+ SizeOfTag -= UnicodeCount*sizeof(cmsUInt16Number);
+
+ // Skip ScriptCode code if present. Some buggy profiles does have less
+ // data that stricttly required. We need to skip it as this type may come
+ // embedded in other types.
+
+ if (SizeOfTag >= sizeof(cmsUInt16Number) + sizeof(cmsUInt8Number) + 67) {
+
+ if (!_cmsReadUInt16Number(io, &ScriptCodeCode)) goto Done;
+ if (!_cmsReadUInt8Number(io, &ScriptCodeCount)) goto Done;
+
+ // Skip rest of tag
+ for (i=0; i < 67; i++) {
+ if (!io ->Read(io, &Dummy, sizeof(cmsUInt8Number), 1)) goto Error;
+ }
+ }
+
+Done:
+
+ *nItems = 1;
+ return mlu;
+
+Error:
+ if (Text) _cmsFree(self ->ContextID, (void*) Text);
+ if (mlu) cmsMLUfree(mlu);
+ return NULL;
+}
+
+
+// This tag can come IN UNALIGNED SIZE. In order to prevent issues, we force zeros on description to align it
+static
+cmsBool Type_Text_Description_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsMLU* mlu = (cmsMLU*) Ptr;
+ char *Text = NULL;
+ wchar_t *Wide = NULL;
+ cmsUInt32Number len, len_aligned, len_filler_alignment;
+ cmsBool rc = FALSE;
+ char Filler[68];
+
+ // Used below for writting zeroes
+ memset(Filler, 0, sizeof(Filler));
+
+ // Get the len of string
+ len = cmsMLUgetASCII(mlu, cmsNoLanguage, cmsNoCountry, NULL, 0);
+
+ // From ICC3.4: It has been found that textDescriptionType can contain misaligned data
+ //(see clause 4.1 for the definition of “aligned?. Because the Unicode language
+ // code and Unicode count immediately follow the ASCII description, their
+ // alignment is not correct if the ASCII count is not a multiple of four. The
+ // ScriptCode code is misaligned when the ASCII count is odd. Profile reading and
+ // writing software must be written carefully in order to handle these alignment
+ // problems.
+
+ // Compute an aligned size
+ len_aligned = _cmsALIGNLONG(len);
+ len_filler_alignment = len_aligned - len;
+
+ // Null strings
+ if (len <= 0) {
+
+ Text = (char*) _cmsDupMem(self ->ContextID, "", sizeof(char));
+ Wide = (wchar_t*) _cmsDupMem(self ->ContextID, L"", sizeof(wchar_t));
+ }
+ else {
+ // Create independent buffers
+ Text = (char*) _cmsCalloc(self ->ContextID, len, sizeof(char));
+ if (Text == NULL) goto Error;
+
+ Wide = (wchar_t*) _cmsCalloc(self ->ContextID, len, sizeof(wchar_t));
+ if (Wide == NULL) goto Error;
+
+ // Get both representations.
+ cmsMLUgetASCII(mlu, cmsNoLanguage, cmsNoCountry, Text, len * sizeof(char));
+ cmsMLUgetWide(mlu, cmsNoLanguage, cmsNoCountry, Wide, len * sizeof(wchar_t));
+ }
+
+ // * cmsUInt32Number count; * Description length
+ // * cmsInt8Number desc[count] * NULL terminated ascii string
+ // * cmsUInt32Number ucLangCode; * UniCode language code
+ // * cmsUInt32Number ucCount; * UniCode description length
+ // * cmsInt16Number ucDesc[ucCount];* The UniCode description
+ // * cmsUInt16Number scCode; * ScriptCode code
+ // * cmsUInt8Number scCount; * ScriptCode count
+ // * cmsInt8Number scDesc[67]; * ScriptCode Description
+
+ if (!_cmsWriteUInt32Number(io, len_aligned)) goto Error;
+ if (!io ->Write(io, len, Text)) goto Error;
+ if (!io ->Write(io, len_filler_alignment, Filler)) goto Error;
+
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error; // ucLanguageCode
+
+ // This part is tricky: we need an aligned tag size, and the ScriptCode part
+ // takes 70 bytes, so we need 2 extra bytes to do the alignment
+
+ if (!_cmsWriteUInt32Number(io, len_aligned+1)) goto Error;
+
+ // Note that in some compilers sizeof(cmsUInt16Number) != sizeof(wchar_t)
+ if (!_cmsWriteWCharArray(io, len, Wide)) goto Error;
+ if (!_cmsWriteUInt16Array(io, len_filler_alignment+1, (cmsUInt16Number*) Filler)) goto Error;
+
+ // ScriptCode Code & count (unused)
+ if (!_cmsWriteUInt16Number(io, 0)) goto Error;
+ if (!_cmsWriteUInt8Number(io, 0)) goto Error;
+
+ if (!io ->Write(io, 67, Filler)) goto Error;
+
+ rc = TRUE;
+
+Error:
+ if (Text) _cmsFree(self ->ContextID, Text);
+ if (Wide) _cmsFree(self ->ContextID, Wide);
+
+ return rc;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_Text_Description_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsMLUdup((cmsMLU*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_Text_Description_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsMLU* mlu = (cmsMLU*) Ptr;
+
+ cmsMLUfree(mlu);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+cmsTagTypeSignature DecideTextDescType(cmsFloat64Number ICCVersion, const void *Data)
+{
+ if (ICCVersion >= 4.0)
+ return cmsSigMultiLocalizedUnicodeType;
+
+ return cmsSigTextDescriptionType;
+
+ cmsUNUSED_PARAMETER(Data);
+}
+
+
+// ********************************************************************************
+// Type cmsSigCurveType
+// ********************************************************************************
+
+static
+void *Type_Curve_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt32Number Count;
+ cmsToneCurve* NewGamma;
+
+ *nItems = 0;
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+
+ switch (Count) {
+
+ case 0: // Linear.
+ {
+ cmsFloat64Number SingleGamma = 1.0;
+
+ NewGamma = cmsBuildParametricToneCurve(self ->ContextID, 1, &SingleGamma);
+ if (!NewGamma) return NULL;
+ *nItems = 1;
+ return NewGamma;
+ }
+
+ case 1: // Specified as the exponent of gamma function
+ {
+ cmsUInt16Number SingleGammaFixed;
+ cmsFloat64Number SingleGamma;
+
+ if (!_cmsReadUInt16Number(io, &SingleGammaFixed)) return NULL;
+ SingleGamma = _cms8Fixed8toDouble(SingleGammaFixed);
+
+ *nItems = 1;
+ return cmsBuildParametricToneCurve(self ->ContextID, 1, &SingleGamma);
+ }
+
+ default: // Curve
+
+ if (Count > 0x7FFF)
+ return NULL; // This is to prevent bad guys for doing bad things
+
+ NewGamma = cmsBuildTabulatedToneCurve16(self ->ContextID, Count, NULL);
+ if (!NewGamma) return NULL;
+
+ if (!_cmsReadUInt16Array(io, Count, NewGamma -> Table16)) return NULL;
+
+ *nItems = 1;
+ return NewGamma;
+ }
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_Curve_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsToneCurve* Curve = (cmsToneCurve*) Ptr;
+
+ if (Curve ->nSegments == 1 && Curve ->Segments[0].Type == 1) {
+
+ // Single gamma, preserve number
+ cmsUInt16Number SingleGammaFixed = _cmsDoubleTo8Fixed8(Curve ->Segments[0].Params[0]);
+
+ if (!_cmsWriteUInt32Number(io, 1)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, SingleGammaFixed)) return FALSE;
+ return TRUE;
+
+ }
+
+ if (!_cmsWriteUInt32Number(io, Curve ->nEntries)) return FALSE;
+ return _cmsWriteUInt16Array(io, Curve ->nEntries, Curve ->Table16);
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_Curve_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsDupToneCurve((cmsToneCurve*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_Curve_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsToneCurve* gamma = (cmsToneCurve*) Ptr;
+
+ cmsFreeToneCurve(gamma);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigParametricCurveType
+// ********************************************************************************
+
+
+// Decide which curve type to use on writting
+static
+cmsTagTypeSignature DecideCurveType(cmsFloat64Number ICCVersion, const void *Data)
+{
+ cmsToneCurve* Curve = (cmsToneCurve*) Data;
+
+ if (ICCVersion < 4.0) return cmsSigCurveType;
+ if (Curve ->nSegments != 1) return cmsSigCurveType; // Only 1-segment curves can be saved as parametric
+ if (Curve ->Segments[0].Type < 0) return cmsSigCurveType; // Only non-inverted curves
+ if (Curve ->Segments[0].Type > 5) return cmsSigCurveType; // Only ICC parametric curves
+
+ return cmsSigParametricCurveType;
+}
+
+static
+void *Type_ParametricCurve_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ static const int ParamsByType[] = { 1, 3, 4, 5, 7 };
+ cmsFloat64Number Params[10];
+ cmsUInt16Number Type;
+ int i, n;
+ cmsToneCurve* NewGamma;
+
+ if (!_cmsReadUInt16Number(io, &Type)) return NULL;
+ if (!_cmsReadUInt16Number(io, NULL)) return NULL; // Reserved
+
+ if (Type > 4) {
+
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown parametric curve type '%d'", Type);
+ return NULL;
+ }
+
+ memset(Params, 0, sizeof(Params));
+ n = ParamsByType[Type];
+
+ for (i=0; i < n; i++) {
+
+ if (!_cmsRead15Fixed16Number(io, &Params[i])) return NULL;
+ }
+
+ NewGamma = cmsBuildParametricToneCurve(self ->ContextID, Type+1, Params);
+
+ *nItems = 1;
+ return NewGamma;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_ParametricCurve_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsToneCurve* Curve = (cmsToneCurve*) Ptr;
+ int i, nParams, typen;
+ static const int ParamsByType[] = { 0, 1, 3, 4, 5, 7 };
+
+ typen = Curve -> Segments[0].Type;
+
+ if (Curve ->nSegments > 1 || typen < 1) {
+
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Multisegment or Inverted parametric curves cannot be written");
+ return FALSE;
+ }
+
+ if (typen > 5) {
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported parametric curve");
+ return FALSE;
+ }
+
+ nParams = ParamsByType[typen];
+
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) (Curve ->Segments[0].Type - 1))) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE; // Reserved
+
+ for (i=0; i < nParams; i++) {
+
+ if (!_cmsWrite15Fixed16Number(io, Curve -> Segments[0].Params[i])) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_ParametricCurve_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsDupToneCurve((cmsToneCurve*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_ParametricCurve_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsToneCurve* gamma = (cmsToneCurve*) Ptr;
+
+ cmsFreeToneCurve(gamma);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigDateTimeType
+// ********************************************************************************
+
+// A 12-byte value representation of the time and date, where the byte usage is assigned
+// as specified in table 1. The actual values are encoded as 16-bit unsigned integers
+// (uInt16Number - see 5.1.6).
+//
+// All the dateTimeNumber values in a profile shall be in Coordinated Universal Time
+// (UTC, also known as GMT or ZULU Time). Profile writers are required to convert local
+// time to UTC when setting these values. Programmes that display these values may show
+// the dateTimeNumber as UTC, show the equivalent local time (at current locale), or
+// display both UTC and local versions of the dateTimeNumber.
+
+static
+void *Type_DateTime_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsDateTimeNumber timestamp;
+ struct tm * NewDateTime;
+
+ *nItems = 0;
+ NewDateTime = (struct tm*) _cmsMalloc(self ->ContextID, sizeof(struct tm));
+ if (NewDateTime == NULL) return NULL;
+
+ if (io->Read(io, &timestamp, sizeof(cmsDateTimeNumber), 1) != 1) return NULL;
+
+ _cmsDecodeDateTimeNumber(&timestamp, NewDateTime);
+
+ *nItems = 1;
+ return NewDateTime;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_DateTime_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ struct tm * DateTime = (struct tm*) Ptr;
+ cmsDateTimeNumber timestamp;
+
+ _cmsEncodeDateTimeNumber(&timestamp, DateTime);
+ if (!io ->Write(io, sizeof(cmsDateTimeNumber), &timestamp)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_DateTime_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(struct tm));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_DateTime_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+
+// ********************************************************************************
+// Type icMeasurementType
+// ********************************************************************************
+
+/*
+The measurementType information refers only to the internal profile data and is
+meant to provide profile makers an alternative to the default measurement
+specifications.
+*/
+
+static
+void *Type_Measurement_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsICCMeasurementConditions mc;
+
+
+ memset(&mc, 0, sizeof(mc));
+
+ if (!_cmsReadUInt32Number(io, &mc.Observer)) return NULL;
+ if (!_cmsReadXYZNumber(io, &mc.Backing)) return NULL;
+ if (!_cmsReadUInt32Number(io, &mc.Geometry)) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &mc.Flare)) return NULL;
+ if (!_cmsReadUInt32Number(io, &mc.IlluminantType)) return NULL;
+
+ *nItems = 1;
+ return _cmsDupMem(self ->ContextID, &mc, sizeof(cmsICCMeasurementConditions));
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_Measurement_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsICCMeasurementConditions* mc =(cmsICCMeasurementConditions*) Ptr;
+
+ if (!_cmsWriteUInt32Number(io, mc->Observer)) return FALSE;
+ if (!_cmsWriteXYZNumber(io, &mc->Backing)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, mc->Geometry)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, mc->Flare)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, mc->IlluminantType)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_Measurement_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsICCMeasurementConditions));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_Measurement_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+// ********************************************************************************
+// Type cmsSigMultiLocalizedUnicodeType
+// ********************************************************************************
+//
+// Do NOT trust SizeOfTag as there is an issue on the definition of profileSequenceDescTag. See the TechNote from
+// Max Derhak and Rohit Patil about this: basically the size of the string table should be guessed and cannot be
+// taken from the size of tag if this tag is embedded as part of bigger structures (profileSequenceDescTag, for instance)
+//
+
+static
+void *Type_MLU_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsMLU* mlu;
+ cmsUInt32Number Count, RecLen, NumOfWchar;
+ cmsUInt32Number SizeOfHeader;
+ cmsUInt32Number Len, Offset;
+ cmsUInt32Number i;
+ wchar_t* Block;
+ cmsUInt32Number BeginOfThisString, EndOfThisString, LargestPosition;
+
+ *nItems = 0;
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+ if (!_cmsReadUInt32Number(io, &RecLen)) return NULL;
+
+ if (RecLen != 12) {
+
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "multiLocalizedUnicodeType of len != 12 is not supported.");
+ return NULL;
+ }
+
+ mlu = cmsMLUalloc(self ->ContextID, Count);
+ if (mlu == NULL) return NULL;
+
+ mlu ->UsedEntries = Count;
+
+ SizeOfHeader = 12 * Count + sizeof(_cmsTagBase);
+ LargestPosition = 0;
+
+ for (i=0; i < Count; i++) {
+
+ if (!_cmsReadUInt16Number(io, &mlu ->Entries[i].Language)) goto Error;
+ if (!_cmsReadUInt16Number(io, &mlu ->Entries[i].Country)) goto Error;
+
+ // Now deal with Len and offset.
+ if (!_cmsReadUInt32Number(io, &Len)) goto Error;
+ if (!_cmsReadUInt32Number(io, &Offset)) goto Error;
+
+ // Check for overflow
+ if (Offset < (SizeOfHeader + 8)) goto Error;
+
+ // True begin of the string
+ BeginOfThisString = Offset - SizeOfHeader - 8;
+
+ // Ajust to wchar_t elements
+ mlu ->Entries[i].Len = (Len * sizeof(wchar_t)) / sizeof(cmsUInt16Number);
+ mlu ->Entries[i].StrW = (BeginOfThisString * sizeof(wchar_t)) / sizeof(cmsUInt16Number);
+
+ // To guess maximum size, add offset + len
+ EndOfThisString = BeginOfThisString + Len;
+ if (EndOfThisString > LargestPosition)
+ LargestPosition = EndOfThisString;
+ }
+
+ // Now read the remaining of tag and fill all strings. Substract the directory
+ SizeOfTag = (LargestPosition * sizeof(wchar_t)) / sizeof(cmsUInt16Number);
+ if (SizeOfTag == 0)
+ {
+ Block = NULL;
+ NumOfWchar = 0;
+
+ }
+ else
+ {
+ Block = (wchar_t*) _cmsMalloc(self ->ContextID, SizeOfTag);
+ if (Block == NULL) goto Error;
+ NumOfWchar = SizeOfTag / sizeof(wchar_t);
+ if (!_cmsReadWCharArray(io, NumOfWchar, Block)) goto Error;
+ }
+
+ mlu ->MemPool = Block;
+ mlu ->PoolSize = SizeOfTag;
+ mlu ->PoolUsed = SizeOfTag;
+
+ *nItems = 1;
+ return (void*) mlu;
+
+Error:
+ if (mlu) cmsMLUfree(mlu);
+ return NULL;
+}
+
+static
+cmsBool Type_MLU_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsMLU* mlu =(cmsMLU*) Ptr;
+ cmsUInt32Number HeaderSize;
+ cmsUInt32Number Len, Offset;
+ int i;
+
+ if (Ptr == NULL) {
+
+ // Empty placeholder
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 12)) return FALSE;
+ return TRUE;
+ }
+
+ if (!_cmsWriteUInt32Number(io, mlu ->UsedEntries)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 12)) return FALSE;
+
+ HeaderSize = 12 * mlu ->UsedEntries + sizeof(_cmsTagBase);
+
+ for (i=0; i < mlu ->UsedEntries; i++) {
+
+ Len = mlu ->Entries[i].Len;
+ Offset = mlu ->Entries[i].StrW;
+
+ Len = (Len * sizeof(cmsUInt16Number)) / sizeof(wchar_t);
+ Offset = (Offset * sizeof(cmsUInt16Number)) / sizeof(wchar_t) + HeaderSize + 8;
+
+ if (!_cmsWriteUInt16Number(io, mlu ->Entries[i].Language)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, mlu ->Entries[i].Country)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, Len)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, Offset)) return FALSE;
+ }
+
+ if (!_cmsWriteWCharArray(io, mlu ->PoolUsed / sizeof(wchar_t), (wchar_t*) mlu ->MemPool)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_MLU_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsMLUdup((cmsMLU*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_MLU_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsMLUfree((cmsMLU*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigLut8Type
+// ********************************************************************************
+
+// Decide which LUT type to use on writting
+static
+cmsTagTypeSignature DecideLUTtypeA2B(cmsFloat64Number ICCVersion, const void *Data)
+{
+ cmsPipeline* Lut = (cmsPipeline*) Data;
+
+ if (ICCVersion < 4.0) {
+ if (Lut ->SaveAs8Bits) return cmsSigLut8Type;
+ return cmsSigLut16Type;
+ }
+ else {
+ return cmsSigLutAtoBType;
+ }
+}
+
+static
+cmsTagTypeSignature DecideLUTtypeB2A(cmsFloat64Number ICCVersion, const void *Data)
+{
+ cmsPipeline* Lut = (cmsPipeline*) Data;
+
+ if (ICCVersion < 4.0) {
+ if (Lut ->SaveAs8Bits) return cmsSigLut8Type;
+ return cmsSigLut16Type;
+ }
+ else {
+ return cmsSigLutBtoAType;
+ }
+}
+
+/*
+This structure represents a colour transform using tables of 8-bit precision.
+This type contains four processing elements: a 3 by 3 matrix (which shall be
+the identity matrix unless the input colour space is XYZ), a set of one dimensional
+input tables, a multidimensional lookup table, and a set of one dimensional output
+tables. Data is processed using these elements via the following sequence:
+(matrix) -> (1d input tables) -> (multidimensional lookup table - CLUT) -> (1d output tables)
+
+Byte Position Field Length (bytes) Content Encoded as...
+8 1 Number of Input Channels (i) uInt8Number
+9 1 Number of Output Channels (o) uInt8Number
+10 1 Number of CLUT grid points (identical for each side) (g) uInt8Number
+11 1 Reserved for padding (fill with 00h)
+
+12..15 4 Encoded e00 parameter s15Fixed16Number
+*/
+
+
+// Read 8 bit tables as gamma functions
+static
+cmsBool Read8bitTables(cmsContext ContextID, cmsIOHANDLER* io, cmsPipeline* lut, int nChannels)
+{
+ cmsUInt8Number* Temp = NULL;
+ int i, j;
+ cmsToneCurve* Tables[cmsMAXCHANNELS];
+
+ if (nChannels > cmsMAXCHANNELS) return FALSE;
+ if (nChannels <= 0) return FALSE;
+
+ memset(Tables, 0, sizeof(Tables));
+
+ Temp = (cmsUInt8Number*) _cmsMalloc(ContextID, 256);
+ if (Temp == NULL) return FALSE;
+
+ for (i=0; i < nChannels; i++) {
+ Tables[i] = cmsBuildTabulatedToneCurve16(ContextID, 256, NULL);
+ if (Tables[i] == NULL) goto Error;
+ }
+
+ for (i=0; i < nChannels; i++) {
+
+ if (io ->Read(io, Temp, 256, 1) != 1) goto Error;
+
+ for (j=0; j < 256; j++)
+ Tables[i]->Table16[j] = (cmsUInt16Number) FROM_8_TO_16(Temp[j]);
+ }
+
+ _cmsFree(ContextID, Temp);
+ Temp = NULL;
+
+ if (!cmsPipelineInsertStage(lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, nChannels, Tables)))
+ goto Error;
+
+ for (i=0; i < nChannels; i++)
+ cmsFreeToneCurve(Tables[i]);
+
+ return TRUE;
+
+Error:
+ for (i=0; i < nChannels; i++) {
+ if (Tables[i]) cmsFreeToneCurve(Tables[i]);
+ }
+
+ if (Temp) _cmsFree(ContextID, Temp);
+ return FALSE;
+}
+
+
+static
+cmsBool Write8bitTables(cmsContext ContextID, cmsIOHANDLER* io, cmsUInt32Number n, _cmsStageToneCurvesData* Tables)
+{
+ int j;
+ cmsUInt32Number i;
+ cmsUInt8Number val;
+
+ for (i=0; i < n; i++) {
+
+ if (Tables) {
+
+ // Usual case of identity curves
+ if ((Tables ->TheCurves[i]->nEntries == 2) &&
+ (Tables->TheCurves[i]->Table16[0] == 0) &&
+ (Tables->TheCurves[i]->Table16[1] == 65535)) {
+
+ for (j=0; j < 256; j++) {
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) j)) return FALSE;
+ }
+ }
+ else
+ if (Tables ->TheCurves[i]->nEntries != 256) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "LUT8 needs 256 entries on prelinearization");
+ return FALSE;
+ }
+ else
+ for (j=0; j < 256; j++) {
+
+ if (Tables != NULL)
+ val = (cmsUInt8Number) FROM_16_TO_8(Tables->TheCurves[i]->Table16[j]);
+ else
+ val = (cmsUInt8Number) j;
+
+ if (!_cmsWriteUInt8Number(io, val)) return FALSE;
+ }
+ }
+ }
+ return TRUE;
+}
+
+
+// Check overflow
+static
+cmsUInt32Number uipow(cmsUInt32Number n, cmsUInt32Number a, cmsUInt32Number b)
+{
+ cmsUInt32Number rv = 1, rc;
+
+ if (a == 0) return 0;
+ if (n == 0) return 0;
+
+ for (; b > 0; b--) {
+
+ rv *= a;
+
+ // Check for overflow
+ if (rv > UINT_MAX / a) return (cmsUInt32Number) -1;
+
+ }
+
+ rc = rv * n;
+
+ if (rv != rc / n) return (cmsUInt32Number) -1;
+ return rc;
+}
+
+
+// That will create a MPE LUT with Matrix, pre tables, CLUT and post tables.
+// 8 bit lut may be scaled easely to v4 PCS, but we need also to properly adjust
+// PCS on BToAxx tags and AtoB if abstract. We need to fix input direction.
+
+static
+void *Type_LUT8_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt8Number InputChannels, OutputChannels, CLUTpoints;
+ cmsUInt8Number* Temp = NULL;
+ cmsPipeline* NewLUT = NULL;
+ cmsUInt32Number nTabSize, i;
+ cmsFloat64Number Matrix[3*3];
+
+ *nItems = 0;
+
+ if (!_cmsReadUInt8Number(io, &InputChannels)) goto Error;
+ if (!_cmsReadUInt8Number(io, &OutputChannels)) goto Error;
+ if (!_cmsReadUInt8Number(io, &CLUTpoints)) goto Error;
+
+ if (CLUTpoints == 1) goto Error; // Impossible value, 0 for no CLUT and then 2 at least
+
+ // Padding
+ if (!_cmsReadUInt8Number(io, NULL)) goto Error;
+
+ // Do some checking
+ if (InputChannels > cmsMAXCHANNELS) goto Error;
+ if (OutputChannels > cmsMAXCHANNELS) goto Error;
+
+ // Allocates an empty Pipeline
+ NewLUT = cmsPipelineAlloc(self ->ContextID, InputChannels, OutputChannels);
+ if (NewLUT == NULL) goto Error;
+
+ // Read the Matrix
+ if (!_cmsRead15Fixed16Number(io, &Matrix[0])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[1])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[2])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[3])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[4])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[5])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[6])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[7])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[8])) goto Error;
+
+
+ // Only operates if not identity...
+ if ((InputChannels == 3) && !_cmsMAT3isIdentity((cmsMAT3*) Matrix)) {
+
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_BEGIN, cmsStageAllocMatrix(self ->ContextID, 3, 3, Matrix, NULL)))
+ goto Error;
+ }
+
+ // Get input tables
+ if (!Read8bitTables(self ->ContextID, io, NewLUT, InputChannels)) goto Error;
+
+ // Get 3D CLUT. Check the overflow....
+ nTabSize = uipow(OutputChannels, CLUTpoints, InputChannels);
+ if (nTabSize == (cmsUInt32Number) -1) goto Error;
+ if (nTabSize > 0) {
+
+ cmsUInt16Number *PtrW, *T;
+
+ PtrW = T = (cmsUInt16Number*) _cmsCalloc(self ->ContextID, nTabSize, sizeof(cmsUInt16Number));
+ if (T == NULL) goto Error;
+
+ Temp = (cmsUInt8Number*) _cmsMalloc(self ->ContextID, nTabSize);
+ if (Temp == NULL) {
+ _cmsFree(self ->ContextID, T);
+ goto Error;
+ }
+
+ if (io ->Read(io, Temp, nTabSize, 1) != 1) {
+ _cmsFree(self ->ContextID, T);
+ _cmsFree(self ->ContextID, Temp);
+ goto Error;
+ }
+
+ for (i = 0; i < nTabSize; i++) {
+
+ *PtrW++ = FROM_8_TO_16(Temp[i]);
+ }
+ _cmsFree(self ->ContextID, Temp);
+ Temp = NULL;
+
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, cmsStageAllocCLut16bit(self ->ContextID, CLUTpoints, InputChannels, OutputChannels, T)))
+ goto Error;
+ _cmsFree(self ->ContextID, T);
+ }
+
+
+ // Get output tables
+ if (!Read8bitTables(self ->ContextID, io, NewLUT, OutputChannels)) goto Error;
+
+ *nItems = 1;
+ return NewLUT;
+
+Error:
+ if (NewLUT != NULL) cmsPipelineFree(NewLUT);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+// We only allow a specific MPE structure: Matrix plus prelin, plus clut, plus post-lin.
+static
+cmsBool Type_LUT8_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt32Number j, nTabSize;
+ cmsUInt8Number val;
+ cmsPipeline* NewLUT = (cmsPipeline*) Ptr;
+ cmsStage* mpe;
+ _cmsStageToneCurvesData* PreMPE = NULL, *PostMPE = NULL;
+ _cmsStageMatrixData* MatMPE = NULL;
+ _cmsStageCLutData* clut = NULL;
+ int clutPoints;
+
+ // Disassemble the LUT into components.
+ mpe = NewLUT -> Elements;
+ if (mpe ->Type == cmsSigMatrixElemType) {
+
+ MatMPE = (_cmsStageMatrixData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+ if (mpe != NULL && mpe ->Type == cmsSigCurveSetElemType) {
+ PreMPE = (_cmsStageToneCurvesData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+ if (mpe != NULL && mpe ->Type == cmsSigCLutElemType) {
+ clut = (_cmsStageCLutData*) mpe -> Data;
+ mpe = mpe ->Next;
+ }
+
+ if (mpe != NULL && mpe ->Type == cmsSigCurveSetElemType) {
+ PostMPE = (_cmsStageToneCurvesData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+ // That should be all
+ if (mpe != NULL) {
+ cmsSignalError(mpe->ContextID, cmsERROR_UNKNOWN_EXTENSION, "LUT is not suitable to be saved as LUT8");
+ return FALSE;
+ }
+
+
+ if (clut == NULL)
+ clutPoints = 0;
+ else
+ clutPoints = clut->Params->nSamples[0];
+
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) NewLUT ->InputChannels)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) NewLUT ->OutputChannels)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) clutPoints)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, 0)) return FALSE; // Padding
+
+
+ if (MatMPE != NULL) {
+
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[0])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[1])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[2])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[3])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[4])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[5])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[6])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[7])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[8])) return FALSE;
+
+ }
+ else {
+
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ }
+
+ // The prelinearization table
+ if (!Write8bitTables(self ->ContextID, io, NewLUT ->InputChannels, PreMPE)) return FALSE;
+
+ nTabSize = uipow(NewLUT->OutputChannels, clutPoints, NewLUT ->InputChannels);
+ if (nTabSize == (cmsUInt32Number) -1) return FALSE;
+ if (nTabSize > 0) {
+
+ // The 3D CLUT.
+ if (clut != NULL) {
+
+ for (j=0; j < nTabSize; j++) {
+
+ val = (cmsUInt8Number) FROM_16_TO_8(clut ->Tab.T[j]);
+ if (!_cmsWriteUInt8Number(io, val)) return FALSE;
+ }
+ }
+ }
+
+ // The postlinearization table
+ if (!Write8bitTables(self ->ContextID, io, NewLUT ->OutputChannels, PostMPE)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_LUT8_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsPipelineDup((cmsPipeline*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_LUT8_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsPipelineFree((cmsPipeline*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+// ********************************************************************************
+// Type cmsSigLut16Type
+// ********************************************************************************
+
+// Read 16 bit tables as gamma functions
+static
+cmsBool Read16bitTables(cmsContext ContextID, cmsIOHANDLER* io, cmsPipeline* lut, int nChannels, int nEntries)
+{
+ int i;
+ cmsToneCurve* Tables[cmsMAXCHANNELS];
+
+ // Maybe an empty table? (this is a lcms extension)
+ if (nEntries <= 0) return TRUE;
+
+ // Check for malicious profiles
+ if (nEntries < 2) return FALSE;
+ if (nChannels > cmsMAXCHANNELS) return FALSE;
+
+ // Init table to zero
+ memset(Tables, 0, sizeof(Tables));
+
+ for (i=0; i < nChannels; i++) {
+
+ Tables[i] = cmsBuildTabulatedToneCurve16(ContextID, nEntries, NULL);
+ if (Tables[i] == NULL) goto Error;
+
+ if (!_cmsReadUInt16Array(io, nEntries, Tables[i]->Table16)) goto Error;
+ }
+
+
+ // Add the table (which may certainly be an identity, but this is up to the optimizer, not the reading code)
+ if (!cmsPipelineInsertStage(lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, nChannels, Tables)))
+ goto Error;
+
+ for (i=0; i < nChannels; i++)
+ cmsFreeToneCurve(Tables[i]);
+
+ return TRUE;
+
+Error:
+ for (i=0; i < nChannels; i++) {
+ if (Tables[i]) cmsFreeToneCurve(Tables[i]);
+ }
+
+ return FALSE;
+}
+
+static
+cmsBool Write16bitTables(cmsContext ContextID, cmsIOHANDLER* io, _cmsStageToneCurvesData* Tables)
+{
+ int j;
+ cmsUInt32Number i;
+ cmsUInt16Number val;
+ int nEntries;
+
+ _cmsAssert(Tables != NULL);
+
+ nEntries = Tables->TheCurves[0]->nEntries;
+
+ for (i=0; i < Tables ->nCurves; i++) {
+
+ for (j=0; j < nEntries; j++) {
+
+ val = Tables->TheCurves[i]->Table16[j];
+ if (!_cmsWriteUInt16Number(io, val)) return FALSE;
+ }
+ }
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(ContextID);
+}
+
+static
+void *Type_LUT16_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt8Number InputChannels, OutputChannels, CLUTpoints;
+ cmsPipeline* NewLUT = NULL;
+ cmsUInt32Number nTabSize;
+ cmsFloat64Number Matrix[3*3];
+ cmsUInt16Number InputEntries, OutputEntries;
+
+ *nItems = 0;
+
+ if (!_cmsReadUInt8Number(io, &InputChannels)) return NULL;
+ if (!_cmsReadUInt8Number(io, &OutputChannels)) return NULL;
+ if (!_cmsReadUInt8Number(io, &CLUTpoints)) return NULL; // 255 maximum
+
+ // Padding
+ if (!_cmsReadUInt8Number(io, NULL)) return NULL;
+
+ // Do some checking
+ if (InputChannels > cmsMAXCHANNELS) goto Error;
+ if (OutputChannels > cmsMAXCHANNELS) goto Error;
+
+ // Allocates an empty LUT
+ NewLUT = cmsPipelineAlloc(self ->ContextID, InputChannels, OutputChannels);
+ if (NewLUT == NULL) goto Error;
+
+ // Read the Matrix
+ if (!_cmsRead15Fixed16Number(io, &Matrix[0])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[1])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[2])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[3])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[4])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[5])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[6])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[7])) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Matrix[8])) goto Error;
+
+
+ // Only operates on 3 channels
+ if ((InputChannels == 3) && !_cmsMAT3isIdentity((cmsMAT3*) Matrix)) {
+
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, cmsStageAllocMatrix(self ->ContextID, 3, 3, Matrix, NULL)))
+ goto Error;
+ }
+
+ if (!_cmsReadUInt16Number(io, &InputEntries)) goto Error;
+ if (!_cmsReadUInt16Number(io, &OutputEntries)) goto Error;
+
+ if (InputEntries > 0x7FFF || OutputEntries > 0x7FFF) goto Error;
+ if (CLUTpoints == 1) goto Error; // Impossible value, 0 for no CLUT and then 2 at least
+
+ // Get input tables
+ if (!Read16bitTables(self ->ContextID, io, NewLUT, InputChannels, InputEntries)) goto Error;
+
+ // Get 3D CLUT
+ nTabSize = uipow(OutputChannels, CLUTpoints, InputChannels);
+ if (nTabSize == (cmsUInt32Number) -1) goto Error;
+ if (nTabSize > 0) {
+
+ cmsUInt16Number *T;
+
+ T = (cmsUInt16Number*) _cmsCalloc(self ->ContextID, nTabSize, sizeof(cmsUInt16Number));
+ if (T == NULL) goto Error;
+
+ if (!_cmsReadUInt16Array(io, nTabSize, T)) {
+ _cmsFree(self ->ContextID, T);
+ goto Error;
+ }
+
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, cmsStageAllocCLut16bit(self ->ContextID, CLUTpoints, InputChannels, OutputChannels, T))) {
+ _cmsFree(self ->ContextID, T);
+ goto Error;
+ }
+ _cmsFree(self ->ContextID, T);
+ }
+
+
+ // Get output tables
+ if (!Read16bitTables(self ->ContextID, io, NewLUT, OutputChannels, OutputEntries)) goto Error;
+
+ *nItems = 1;
+ return NewLUT;
+
+Error:
+ if (NewLUT != NULL) cmsPipelineFree(NewLUT);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+// We only allow some specific MPE structures: Matrix plus prelin, plus clut, plus post-lin.
+// Some empty defaults are created for missing parts
+
+static
+cmsBool Type_LUT16_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt32Number nTabSize;
+ cmsPipeline* NewLUT = (cmsPipeline*) Ptr;
+ cmsStage* mpe;
+ _cmsStageToneCurvesData* PreMPE = NULL, *PostMPE = NULL;
+ _cmsStageMatrixData* MatMPE = NULL;
+ _cmsStageCLutData* clut = NULL;
+ int i, InputChannels, OutputChannels, clutPoints;
+
+ // Disassemble the LUT into components.
+ mpe = NewLUT -> Elements;
+ if (mpe != NULL && mpe ->Type == cmsSigMatrixElemType) {
+
+ MatMPE = (_cmsStageMatrixData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+
+ if (mpe != NULL && mpe ->Type == cmsSigCurveSetElemType) {
+ PreMPE = (_cmsStageToneCurvesData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+ if (mpe != NULL && mpe ->Type == cmsSigCLutElemType) {
+ clut = (_cmsStageCLutData*) mpe -> Data;
+ mpe = mpe ->Next;
+ }
+
+ if (mpe != NULL && mpe ->Type == cmsSigCurveSetElemType) {
+ PostMPE = (_cmsStageToneCurvesData*) mpe ->Data;
+ mpe = mpe -> Next;
+ }
+
+ // That should be all
+ if (mpe != NULL) {
+ cmsSignalError(mpe->ContextID, cmsERROR_UNKNOWN_EXTENSION, "LUT is not suitable to be saved as LUT16");
+ return FALSE;
+ }
+
+ InputChannels = cmsPipelineInputChannels(NewLUT);
+ OutputChannels = cmsPipelineOutputChannels(NewLUT);
+
+ if (clut == NULL)
+ clutPoints = 0;
+ else
+ clutPoints = clut->Params->nSamples[0];
+
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) InputChannels)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) OutputChannels)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) clutPoints)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, 0)) return FALSE; // Padding
+
+
+ if (MatMPE != NULL) {
+
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[0])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[1])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[2])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[3])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[4])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[5])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[6])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[7])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, MatMPE -> Double[8])) return FALSE;
+ }
+ else {
+
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 1)) return FALSE;
+ }
+
+
+ if (PreMPE != NULL) {
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) PreMPE ->TheCurves[0]->nEntries)) return FALSE;
+ } else {
+ if (!_cmsWriteUInt16Number(io, 2)) return FALSE;
+ }
+
+ if (PostMPE != NULL) {
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) PostMPE ->TheCurves[0]->nEntries)) return FALSE;
+ } else {
+ if (!_cmsWriteUInt16Number(io, 2)) return FALSE;
+
+ }
+
+ // The prelinearization table
+
+ if (PreMPE != NULL) {
+ if (!Write16bitTables(self ->ContextID, io, PreMPE)) return FALSE;
+ }
+ else {
+ for (i=0; i < InputChannels; i++) {
+
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 0xffff)) return FALSE;
+ }
+ }
+
+ nTabSize = uipow(OutputChannels, clutPoints, InputChannels);
+ if (nTabSize == (cmsUInt32Number) -1) return FALSE;
+ if (nTabSize > 0) {
+ // The 3D CLUT.
+ if (clut != NULL) {
+ if (!_cmsWriteUInt16Array(io, nTabSize, clut->Tab.T)) return FALSE;
+ }
+ }
+
+ // The postlinearization table
+ if (PostMPE != NULL) {
+ if (!Write16bitTables(self ->ContextID, io, PostMPE)) return FALSE;
+ }
+ else {
+ for (i=0; i < OutputChannels; i++) {
+
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 0xffff)) return FALSE;
+ }
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_LUT16_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsPipelineDup((cmsPipeline*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_LUT16_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsPipelineFree((cmsPipeline*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigLutAToBType
+// ********************************************************************************
+
+
+// V4 stuff. Read matrix for LutAtoB and LutBtoA
+
+static
+cmsStage* ReadMatrix(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number Offset)
+{
+ cmsFloat64Number dMat[3*3];
+ cmsFloat64Number dOff[3];
+ cmsStage* Mat;
+
+ // Go to address
+ if (!io -> Seek(io, Offset)) return NULL;
+
+ // Read the Matrix
+ if (!_cmsRead15Fixed16Number(io, &dMat[0])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[1])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[2])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[3])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[4])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[5])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[6])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[7])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dMat[8])) return NULL;
+
+ if (!_cmsRead15Fixed16Number(io, &dOff[0])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dOff[1])) return NULL;
+ if (!_cmsRead15Fixed16Number(io, &dOff[2])) return NULL;
+
+ Mat = cmsStageAllocMatrix(self ->ContextID, 3, 3, dMat, dOff);
+
+ return Mat;
+}
+
+
+
+
+// V4 stuff. Read CLUT part for LutAtoB and LutBtoA
+
+static
+cmsStage* ReadCLUT(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number Offset, int InputChannels, int OutputChannels)
+{
+ cmsUInt8Number gridPoints8[cmsMAXCHANNELS]; // Number of grid points in each dimension.
+ cmsUInt32Number GridPoints[cmsMAXCHANNELS], i;
+ cmsUInt8Number Precision;
+ cmsStage* CLUT;
+ _cmsStageCLutData* Data;
+
+ if (!io -> Seek(io, Offset)) return NULL;
+ if (io -> Read(io, gridPoints8, cmsMAXCHANNELS, 1) != 1) return NULL;
+
+
+ for (i=0; i < cmsMAXCHANNELS; i++) {
+
+ if (gridPoints8[i] == 1) return NULL; // Impossible value, 0 for no CLUT and then 2 at least
+ GridPoints[i] = gridPoints8[i];
+ }
+
+ if (!_cmsReadUInt8Number(io, &Precision)) return NULL;
+
+ if (!_cmsReadUInt8Number(io, NULL)) return NULL;
+ if (!_cmsReadUInt8Number(io, NULL)) return NULL;
+ if (!_cmsReadUInt8Number(io, NULL)) return NULL;
+
+ CLUT = cmsStageAllocCLut16bitGranular(self ->ContextID, GridPoints, InputChannels, OutputChannels, NULL);
+ if (CLUT == NULL) return NULL;
+
+ Data = (_cmsStageCLutData*) CLUT ->Data;
+
+ // Precision can be 1 or 2 bytes
+ if (Precision == 1) {
+
+ cmsUInt8Number v;
+
+ for (i=0; i < Data ->nEntries; i++) {
+
+ if (io ->Read(io, &v, sizeof(cmsUInt8Number), 1) != 1) return NULL;
+ Data ->Tab.T[i] = FROM_8_TO_16(v);
+ }
+
+ }
+ else
+ if (Precision == 2) {
+
+ if (!_cmsReadUInt16Array(io, Data->nEntries, Data ->Tab.T)) {
+ cmsStageFree(CLUT);
+ return NULL;
+ }
+ }
+ else {
+ cmsStageFree(CLUT);
+ cmsSignalError(self ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown precision of '%d'", Precision);
+ return NULL;
+ }
+
+ return CLUT;
+}
+
+static
+cmsToneCurve* ReadEmbeddedCurve(struct _cms_typehandler_struct* self, cmsIOHANDLER* io)
+{
+ cmsTagTypeSignature BaseType;
+ cmsUInt32Number nItems;
+
+ BaseType = _cmsReadTypeBase(io);
+ switch (BaseType) {
+
+ case cmsSigCurveType:
+ return (cmsToneCurve*) Type_Curve_Read(self, io, &nItems, 0);
+
+ case cmsSigParametricCurveType:
+ return (cmsToneCurve*) Type_ParametricCurve_Read(self, io, &nItems, 0);
+
+ default:
+ {
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) BaseType);
+ cmsSignalError(self ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown curve type '%s'", String);
+ }
+ return NULL;
+ }
+}
+
+
+// Read a set of curves from specific offset
+static
+cmsStage* ReadSetOfCurves(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number Offset, cmsUInt32Number nCurves)
+{
+ cmsToneCurve* Curves[cmsMAXCHANNELS];
+ cmsUInt32Number i;
+ cmsStage* Lin = NULL;
+
+ if (nCurves > cmsMAXCHANNELS) return FALSE;
+
+ if (!io -> Seek(io, Offset)) return FALSE;
+
+ for (i=0; i < nCurves; i++)
+ Curves[i] = NULL;
+
+ for (i=0; i < nCurves; i++) {
+
+ Curves[i] = ReadEmbeddedCurve(self, io);
+ if (Curves[i] == NULL) goto Error;
+ if (!_cmsReadAlignment(io)) goto Error;
+
+ }
+
+ Lin = cmsStageAllocToneCurves(self ->ContextID, nCurves, Curves);
+
+Error:
+ for (i=0; i < nCurves; i++)
+ cmsFreeToneCurve(Curves[i]);
+
+ return Lin;
+}
+
+
+// LutAtoB type
+
+// This structure represents a colour transform. The type contains up to five processing
+// elements which are stored in the AtoBTag tag in the following order: a set of one
+// dimensional curves, a 3 by 3 matrix with offset terms, a set of one dimensional curves,
+// a multidimensional lookup table, and a set of one dimensional output curves.
+// Data are processed using these elements via the following sequence:
+//
+//("A" curves) -> (multidimensional lookup table - CLUT) -> ("M" curves) -> (matrix) -> ("B" curves).
+//
+/*
+It is possible to use any or all of these processing elements. At least one processing element
+must be included.Only the following combinations are allowed:
+
+B
+M - Matrix - B
+A - CLUT - B
+A - CLUT - M - Matrix - B
+
+*/
+
+static
+void* Type_LUTA2B_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt32Number BaseOffset;
+ cmsUInt8Number inputChan; // Number of input channels
+ cmsUInt8Number outputChan; // Number of output channels
+ cmsUInt32Number offsetB; // Offset to first "B" curve
+ cmsUInt32Number offsetMat; // Offset to matrix
+ cmsUInt32Number offsetM; // Offset to first "M" curve
+ cmsUInt32Number offsetC; // Offset to CLUT
+ cmsUInt32Number offsetA; // Offset to first "A" curve
+ cmsPipeline* NewLUT = NULL;
+
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ if (!_cmsReadUInt8Number(io, &inputChan)) return NULL;
+ if (!_cmsReadUInt8Number(io, &outputChan)) return NULL;
+
+ if (!_cmsReadUInt16Number(io, NULL)) return NULL;
+
+ if (!_cmsReadUInt32Number(io, &offsetB)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetMat)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetM)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetC)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetA)) return NULL;
+
+ // Allocates an empty LUT
+ NewLUT = cmsPipelineAlloc(self ->ContextID, inputChan, outputChan);
+ if (NewLUT == NULL) return NULL;
+
+ if (offsetA!= 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetA, inputChan)))
+ goto Error;
+ }
+
+ if (offsetC != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadCLUT(self, io, BaseOffset + offsetC, inputChan, outputChan)))
+ goto Error;
+ }
+
+ if (offsetM != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetM, outputChan)))
+ goto Error;
+ }
+
+ if (offsetMat != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadMatrix(self, io, BaseOffset + offsetMat)))
+ goto Error;
+ }
+
+ if (offsetB != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetB, outputChan)))
+ goto Error;
+ }
+
+ *nItems = 1;
+ return NewLUT;
+Error:
+ cmsPipelineFree(NewLUT);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+// Write a set of curves
+static
+cmsBool WriteMatrix(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsStage* mpe)
+{
+ _cmsStageMatrixData* m = (_cmsStageMatrixData*) mpe -> Data;
+
+ // Write the Matrix
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[0])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[1])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[2])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[3])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[4])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[5])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[6])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[7])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Double[8])) return FALSE;
+
+ if (m ->Offset != NULL) {
+
+ if (!_cmsWrite15Fixed16Number(io, m -> Offset[0])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Offset[1])) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, m -> Offset[2])) return FALSE;
+ }
+ else {
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, 0)) return FALSE;
+
+ }
+
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// Write a set of curves
+static
+cmsBool WriteSetOfCurves(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsTagTypeSignature Type, cmsStage* mpe)
+{
+ cmsUInt32Number i, n;
+ cmsTagTypeSignature CurrentType;
+ cmsToneCurve** Curves;
+
+
+ n = cmsStageOutputChannels(mpe);
+ Curves = _cmsStageGetPtrToCurveSet(mpe);
+
+ for (i=0; i < n; i++) {
+
+ // If this is a table-based curve, use curve type even on V4
+ CurrentType = Type;
+
+ if ((Curves[i] ->nSegments == 0)||
+ ((Curves[i]->nSegments == 2) && (Curves[i] ->Segments[1].Type == 0)) )
+ CurrentType = cmsSigCurveType;
+ else
+ if (Curves[i] ->Segments[0].Type < 0)
+ CurrentType = cmsSigCurveType;
+
+ if (!_cmsWriteTypeBase(io, CurrentType)) return FALSE;
+
+ switch (CurrentType) {
+
+ case cmsSigCurveType:
+ if (!Type_Curve_Write(self, io, Curves[i], 1)) return FALSE;
+ break;
+
+ case cmsSigParametricCurveType:
+ if (!Type_ParametricCurve_Write(self, io, Curves[i], 1)) return FALSE;
+ break;
+
+ default:
+ {
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) Type);
+ cmsSignalError(self ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown curve type '%s'", String);
+ }
+ return FALSE;
+ }
+
+ if (!_cmsWriteAlignment(io)) return FALSE;
+ }
+
+
+ return TRUE;
+}
+
+
+static
+cmsBool WriteCLUT(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt8Number Precision, cmsStage* mpe)
+{
+ cmsUInt8Number gridPoints[cmsMAXCHANNELS]; // Number of grid points in each dimension.
+ cmsUInt32Number i;
+ _cmsStageCLutData* CLUT = ( _cmsStageCLutData*) mpe -> Data;
+
+ if (CLUT ->HasFloatValues) {
+ cmsSignalError(self ->ContextID, cmsERROR_NOT_SUITABLE, "Cannot save floating point data, CLUT are 8 or 16 bit only");
+ return FALSE;
+ }
+
+ memset(gridPoints, 0, sizeof(gridPoints));
+ for (i=0; i < (cmsUInt32Number) CLUT ->Params ->nInputs; i++)
+ gridPoints[i] = (cmsUInt8Number) CLUT ->Params ->nSamples[i];
+
+ if (!io -> Write(io, cmsMAXCHANNELS*sizeof(cmsUInt8Number), gridPoints)) return FALSE;
+
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) Precision)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, 0)) return FALSE;
+
+ // Precision can be 1 or 2 bytes
+ if (Precision == 1) {
+
+ for (i=0; i < CLUT->nEntries; i++) {
+
+ if (!_cmsWriteUInt8Number(io, FROM_16_TO_8(CLUT->Tab.T[i]))) return FALSE;
+ }
+ }
+ else
+ if (Precision == 2) {
+
+ if (!_cmsWriteUInt16Array(io, CLUT->nEntries, CLUT ->Tab.T)) return FALSE;
+ }
+ else {
+ cmsSignalError(self ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown precision of '%d'", Precision);
+ return FALSE;
+ }
+
+ if (!_cmsWriteAlignment(io)) return FALSE;
+
+ return TRUE;
+}
+
+
+
+
+static
+cmsBool Type_LUTA2B_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsPipeline* Lut = (cmsPipeline*) Ptr;
+ int inputChan, outputChan;
+ cmsStage *A = NULL, *B = NULL, *M = NULL;
+ cmsStage * Matrix = NULL;
+ cmsStage * CLUT = NULL;
+ cmsUInt32Number offsetB = 0, offsetMat = 0, offsetM = 0, offsetC = 0, offsetA = 0;
+ cmsUInt32Number BaseOffset, DirectoryPos, CurrentPos;
+
+ // Get the base for all offsets
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ if (Lut ->Elements != NULL)
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCurveSetElemType, &B))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType, &M, &Matrix, &B))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &A, &CLUT, &B))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 5, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType,
+ cmsSigMatrixElemType, cmsSigCurveSetElemType, &A, &CLUT, &M, &Matrix, &B)) {
+
+ cmsSignalError(self->ContextID, cmsERROR_NOT_SUITABLE, "LUT is not suitable to be saved as LutAToB");
+ return FALSE;
+ }
+
+ // Get input, output channels
+ inputChan = cmsPipelineInputChannels(Lut);
+ outputChan = cmsPipelineOutputChannels(Lut);
+
+ // Write channel count
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) inputChan)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) outputChan)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE;
+
+ // Keep directory to be filled latter
+ DirectoryPos = io ->Tell(io);
+
+ // Write the directory
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+
+ if (A != NULL) {
+
+ offsetA = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, A)) return FALSE;
+ }
+
+ if (CLUT != NULL) {
+ offsetC = io ->Tell(io) - BaseOffset;
+ if (!WriteCLUT(self, io, Lut ->SaveAs8Bits ? 1 : 2, CLUT)) return FALSE;
+
+ }
+ if (M != NULL) {
+
+ offsetM = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, M)) return FALSE;
+ }
+
+ if (Matrix != NULL) {
+ offsetMat = io ->Tell(io) - BaseOffset;
+ if (!WriteMatrix(self, io, Matrix)) return FALSE;
+ }
+
+ if (B != NULL) {
+
+ offsetB = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, B)) return FALSE;
+ }
+
+ CurrentPos = io ->Tell(io);
+
+ if (!io ->Seek(io, DirectoryPos)) return FALSE;
+
+ if (!_cmsWriteUInt32Number(io, offsetB)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetMat)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetM)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetC)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetA)) return FALSE;
+
+ if (!io ->Seek(io, CurrentPos)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_LUTA2B_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsPipelineDup((cmsPipeline*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_LUTA2B_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsPipelineFree((cmsPipeline*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// LutBToA type
+
+static
+void* Type_LUTB2A_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt8Number inputChan; // Number of input channels
+ cmsUInt8Number outputChan; // Number of output channels
+ cmsUInt32Number BaseOffset; // Actual position in file
+ cmsUInt32Number offsetB; // Offset to first "B" curve
+ cmsUInt32Number offsetMat; // Offset to matrix
+ cmsUInt32Number offsetM; // Offset to first "M" curve
+ cmsUInt32Number offsetC; // Offset to CLUT
+ cmsUInt32Number offsetA; // Offset to first "A" curve
+ cmsPipeline* NewLUT = NULL;
+
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ if (!_cmsReadUInt8Number(io, &inputChan)) return NULL;
+ if (!_cmsReadUInt8Number(io, &outputChan)) return NULL;
+
+ // Padding
+ if (!_cmsReadUInt16Number(io, NULL)) return NULL;
+
+ if (!_cmsReadUInt32Number(io, &offsetB)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetMat)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetM)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetC)) return NULL;
+ if (!_cmsReadUInt32Number(io, &offsetA)) return NULL;
+
+ // Allocates an empty LUT
+ NewLUT = cmsPipelineAlloc(self ->ContextID, inputChan, outputChan);
+ if (NewLUT == NULL) return NULL;
+
+ if (offsetB != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetB, inputChan)))
+ goto Error;
+ }
+
+ if (offsetMat != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadMatrix(self, io, BaseOffset + offsetMat)))
+ goto Error;
+ }
+
+ if (offsetM != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetM, inputChan)))
+ goto Error;
+ }
+
+ if (offsetC != 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadCLUT(self, io, BaseOffset + offsetC, inputChan, outputChan)))
+ goto Error;
+ }
+
+ if (offsetA!= 0) {
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, ReadSetOfCurves(self, io, BaseOffset + offsetA, outputChan)))
+ goto Error;
+ }
+
+ *nItems = 1;
+ return NewLUT;
+Error:
+ cmsPipelineFree(NewLUT);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+/*
+B
+B - Matrix - M
+B - CLUT - A
+B - Matrix - M - CLUT - A
+*/
+
+static
+cmsBool Type_LUTB2A_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsPipeline* Lut = (cmsPipeline*) Ptr;
+ int inputChan, outputChan;
+ cmsStage *A = NULL, *B = NULL, *M = NULL;
+ cmsStage *Matrix = NULL;
+ cmsStage *CLUT = NULL;
+ cmsUInt32Number offsetB = 0, offsetMat = 0, offsetM = 0, offsetC = 0, offsetA = 0;
+ cmsUInt32Number BaseOffset, DirectoryPos, CurrentPos;
+
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCurveSetElemType, &B))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType, &B, &Matrix, &M))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &B, &CLUT, &A))
+ if (!cmsPipelineCheckAndRetreiveStages(Lut, 5, cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
+ cmsSigCLutElemType, cmsSigCurveSetElemType, &B, &Matrix, &M, &CLUT, &A)) {
+ cmsSignalError(self->ContextID, cmsERROR_NOT_SUITABLE, "LUT is not suitable to be saved as LutBToA");
+ return FALSE;
+ }
+
+ inputChan = cmsPipelineInputChannels(Lut);
+ outputChan = cmsPipelineOutputChannels(Lut);
+
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) inputChan)) return FALSE;
+ if (!_cmsWriteUInt8Number(io, (cmsUInt8Number) outputChan)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 0)) return FALSE;
+
+ DirectoryPos = io ->Tell(io);
+
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+
+ if (A != NULL) {
+
+ offsetA = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, A)) return FALSE;
+ }
+
+ if (CLUT != NULL) {
+ offsetC = io ->Tell(io) - BaseOffset;
+ if (!WriteCLUT(self, io, Lut ->SaveAs8Bits ? 1 : 2, CLUT)) return FALSE;
+
+ }
+ if (M != NULL) {
+
+ offsetM = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, M)) return FALSE;
+ }
+
+ if (Matrix != NULL) {
+ offsetMat = io ->Tell(io) - BaseOffset;
+ if (!WriteMatrix(self, io, Matrix)) return FALSE;
+ }
+
+ if (B != NULL) {
+
+ offsetB = io ->Tell(io) - BaseOffset;
+ if (!WriteSetOfCurves(self, io, cmsSigParametricCurveType, B)) return FALSE;
+ }
+
+ CurrentPos = io ->Tell(io);
+
+ if (!io ->Seek(io, DirectoryPos)) return FALSE;
+
+ if (!_cmsWriteUInt32Number(io, offsetB)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetMat)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetM)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetC)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, offsetA)) return FALSE;
+
+ if (!io ->Seek(io, CurrentPos)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+
+static
+void* Type_LUTB2A_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsPipelineDup((cmsPipeline*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_LUTB2A_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsPipelineFree((cmsPipeline*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+
+// ********************************************************************************
+// Type cmsSigColorantTableType
+// ********************************************************************************
+/*
+The purpose of this tag is to identify the colorants used in the profile by a
+unique name and set of XYZ or L*a*b* values to give the colorant an unambiguous
+value. The first colorant listed is the colorant of the first device channel of
+a lut tag. The second colorant listed is the colorant of the second device channel
+of a lut tag, and so on.
+*/
+
+static
+void *Type_ColorantTable_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt32Number i, Count;
+ cmsNAMEDCOLORLIST* List;
+ char Name[34];
+ cmsUInt16Number PCS[3];
+
+
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+
+ if (Count > cmsMAXCHANNELS) {
+ cmsSignalError(self->ContextID, cmsERROR_RANGE, "Too many colorants '%d'", Count);
+ return NULL;
+ }
+
+ List = cmsAllocNamedColorList(self ->ContextID, Count, 0, "", "");
+ for (i=0; i < Count; i++) {
+
+ if (io ->Read(io, Name, 32, 1) != 1) goto Error;
+ Name[33] = 0;
+
+ if (!_cmsReadUInt16Array(io, 3, PCS)) goto Error;
+
+ if (!cmsAppendNamedColor(List, Name, PCS, NULL)) goto Error;
+
+ }
+
+ *nItems = 1;
+ return List;
+
+Error:
+ *nItems = 0;
+ cmsFreeNamedColorList(List);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+
+// Saves a colorant table. It is using the named color structure for simplicity sake
+static
+cmsBool Type_ColorantTable_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) Ptr;
+ int i, nColors;
+
+ nColors = cmsNamedColorCount(NamedColorList);
+
+ if (!_cmsWriteUInt32Number(io, nColors)) return FALSE;
+
+ for (i=0; i < nColors; i++) {
+
+ char root[33];
+ cmsUInt16Number PCS[3];
+
+ if (!cmsNamedColorInfo(NamedColorList, i, root, NULL, NULL, PCS, NULL)) return 0;
+ root[32] = 0;
+
+ if (!io ->Write(io, 32, root)) return FALSE;
+ if (!_cmsWriteUInt16Array(io, 3, PCS)) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_ColorantTable_Dup(struct _cms_typehandler_struct* self, const void* Ptr, cmsUInt32Number n)
+{
+ cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) Ptr;
+ return (void*) cmsDupNamedColorList(nc);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void Type_ColorantTable_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsFreeNamedColorList((cmsNAMEDCOLORLIST*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigNamedColor2Type
+// ********************************************************************************
+//
+//The namedColor2Type is a count value and array of structures that provide color
+//coordinates for 7-bit ASCII color names. For each named color, a PCS and optional
+//device representation of the color are given. Both representations are 16-bit values.
+//The device representation corresponds to the header’s “color space of data?field.
+//This representation should be consistent with the “number of device components?//field in the namedColor2Type. If this field is 0, device coordinates are not provided.
+//The PCS representation corresponds to the header’s PCS field. The PCS representation
+//is always provided. Color names are fixed-length, 32-byte fields including null
+//termination. In order to maintain maximum portability, it is strongly recommended
+//that special characters of the 7-bit ASCII set not be used.
+
+static
+void *Type_NamedColor_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+
+ cmsUInt32Number vendorFlag; // Bottom 16 bits for ICC use
+ cmsUInt32Number count; // Count of named colors
+ cmsUInt32Number nDeviceCoords; // Num of device coordinates
+ char prefix[32]; // Prefix for each color name
+ char suffix[32]; // Suffix for each color name
+ cmsNAMEDCOLORLIST* v;
+ cmsUInt32Number i;
+
+
+ *nItems = 0;
+ if (!_cmsReadUInt32Number(io, &vendorFlag)) return NULL;
+ if (!_cmsReadUInt32Number(io, &count)) return NULL;
+ if (!_cmsReadUInt32Number(io, &nDeviceCoords)) return NULL;
+
+ if (io -> Read(io, prefix, 32, 1) != 1) return NULL;
+ if (io -> Read(io, suffix, 32, 1) != 1) return NULL;
+
+ prefix[31] = suffix[31] = 0;
+
+ v = cmsAllocNamedColorList(self ->ContextID, count, nDeviceCoords, prefix, suffix);
+ if (v == NULL) {
+ cmsSignalError(self->ContextID, cmsERROR_RANGE, "Too many named colors '%d'", count);
+ return NULL;
+ }
+
+ if (nDeviceCoords > cmsMAXCHANNELS) {
+ cmsSignalError(self->ContextID, cmsERROR_RANGE, "Too many device coordinates '%d'", nDeviceCoords);
+ return 0;
+ }
+ for (i=0; i < count; i++) {
+
+ cmsUInt16Number PCS[3];
+ cmsUInt16Number Colorant[cmsMAXCHANNELS];
+ char Root[33];
+
+ memset(Colorant, 0, sizeof(Colorant));
+ if (io -> Read(io, Root, 32, 1) != 1) return NULL;
+ if (!_cmsReadUInt16Array(io, 3, PCS)) goto Error;
+ if (!_cmsReadUInt16Array(io, nDeviceCoords, Colorant)) goto Error;
+
+ if (!cmsAppendNamedColor(v, Root, PCS, Colorant)) goto Error;
+ }
+
+ *nItems = 1;
+ return (void*) v ;
+
+Error:
+ cmsFreeNamedColorList(v);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+// Saves a named color list into a named color profile
+static
+cmsBool Type_NamedColor_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsNAMEDCOLORLIST* NamedColorList = (cmsNAMEDCOLORLIST*) Ptr;
+ char prefix[32]; // Prefix for each color name
+ char suffix[32]; // Suffix for each color name
+ int i, nColors;
+
+ nColors = cmsNamedColorCount(NamedColorList);
+
+ if (!_cmsWriteUInt32Number(io, 0)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, nColors)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, NamedColorList ->ColorantCount)) return FALSE;
+
+ strncpy(prefix, (const char*) NamedColorList->Prefix, 32);
+ strncpy(suffix, (const char*) NamedColorList->Suffix, 32);
+
+ suffix[31] = prefix[31] = 0;
+
+ if (!io ->Write(io, 32, prefix)) return FALSE;
+ if (!io ->Write(io, 32, suffix)) return FALSE;
+
+ for (i=0; i < nColors; i++) {
+
+ cmsUInt16Number PCS[3];
+ cmsUInt16Number Colorant[cmsMAXCHANNELS];
+ char Root[33];
+
+ if (!cmsNamedColorInfo(NamedColorList, i, Root, NULL, NULL, PCS, Colorant)) return 0;
+ if (!io ->Write(io, 32 , Root)) return FALSE;
+ if (!_cmsWriteUInt16Array(io, 3, PCS)) return FALSE;
+ if (!_cmsWriteUInt16Array(io, NamedColorList ->ColorantCount, Colorant)) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void* Type_NamedColor_Dup(struct _cms_typehandler_struct* self, const void* Ptr, cmsUInt32Number n)
+{
+ cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) Ptr;
+
+ return (void*) cmsDupNamedColorList(nc);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void Type_NamedColor_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsFreeNamedColorList((cmsNAMEDCOLORLIST*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigProfileSequenceDescType
+// ********************************************************************************
+
+// This type is an array of structures, each of which contains information from the
+// header fields and tags from the original profiles which were combined to create
+// the final profile. The order of the structures is the order in which the profiles
+// were combined and includes a structure for the final profile. This provides a
+// description of the profile sequence from source to destination,
+// typically used with the DeviceLink profile.
+
+static
+cmsBool ReadEmbeddedText(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsMLU** mlu, cmsUInt32Number SizeOfTag)
+{
+ cmsTagTypeSignature BaseType;
+ cmsUInt32Number nItems;
+
+ BaseType = _cmsReadTypeBase(io);
+
+ switch (BaseType) {
+
+ case cmsSigTextType:
+ if (*mlu) cmsMLUfree(*mlu);
+ *mlu = (cmsMLU*)Type_Text_Read(self, io, &nItems, SizeOfTag);
+ return (*mlu != NULL);
+
+ case cmsSigTextDescriptionType:
+ if (*mlu) cmsMLUfree(*mlu);
+ *mlu = (cmsMLU*) Type_Text_Description_Read(self, io, &nItems, SizeOfTag);
+ return (*mlu != NULL);
+
+ /*
+ TBD: Size is needed for MLU, and we have no idea on which is the available size
+ */
+
+ case cmsSigMultiLocalizedUnicodeType:
+ if (*mlu) cmsMLUfree(*mlu);
+ *mlu = (cmsMLU*) Type_MLU_Read(self, io, &nItems, SizeOfTag);
+ return (*mlu != NULL);
+
+ default: return FALSE;
+ }
+}
+
+
+static
+void *Type_ProfileSequenceDesc_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsSEQ* OutSeq;
+ cmsUInt32Number i, Count;
+
+ *nItems = 0;
+
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+
+ OutSeq = cmsAllocProfileSequenceDescription(self ->ContextID, Count);
+ if (OutSeq == NULL) return NULL;
+
+ OutSeq ->n = Count;
+
+ // Get structures as well
+
+ for (i=0; i < Count; i++) {
+
+ cmsPSEQDESC* sec = &OutSeq -> seq[i];
+
+ if (!_cmsReadUInt32Number(io, &sec ->deviceMfg)) goto Error;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) goto Error;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ if (!_cmsReadUInt32Number(io, &sec ->deviceModel)) goto Error;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) goto Error;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ if (!_cmsReadUInt64Number(io, &sec ->attributes)) goto Error;
+ if (SizeOfTag < sizeof(cmsUInt64Number)) goto Error;
+ SizeOfTag -= sizeof(cmsUInt64Number);
+
+ if (!_cmsReadUInt32Number(io, (cmsUInt32Number *)&sec ->technology)) goto Error;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) goto Error;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ if (!ReadEmbeddedText(self, io, &sec ->Manufacturer, SizeOfTag)) goto Error;
+ if (!ReadEmbeddedText(self, io, &sec ->Model, SizeOfTag)) goto Error;
+ }
+
+ *nItems = 1;
+ return OutSeq;
+
+Error:
+ cmsFreeProfileSequenceDescription(OutSeq);
+ return NULL;
+}
+
+
+// Aux--Embed a text description type. It can be of type text description or multilocalized unicode
+// and it depends of the version number passed on cmsTagDescriptor structure instead of stack
+static
+cmsBool SaveDescription(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsMLU* Text)
+{
+ if (self ->ICCVersion < 0x4000000) {
+
+ if (!_cmsWriteTypeBase(io, cmsSigTextDescriptionType)) return FALSE;
+ return Type_Text_Description_Write(self, io, Text, 1);
+ }
+ else {
+ if (!_cmsWriteTypeBase(io, cmsSigMultiLocalizedUnicodeType)) return FALSE;
+ return Type_MLU_Write(self, io, Text, 1);
+ }
+}
+
+
+static
+cmsBool Type_ProfileSequenceDesc_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsSEQ* Seq = (cmsSEQ*) Ptr;
+ cmsUInt32Number i;
+
+ if (!_cmsWriteUInt32Number(io, Seq->n)) return FALSE;
+
+ for (i=0; i < Seq ->n; i++) {
+
+ cmsPSEQDESC* sec = &Seq -> seq[i];
+
+ if (!_cmsWriteUInt32Number(io, sec ->deviceMfg)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, sec ->deviceModel)) return FALSE;
+ if (!_cmsWriteUInt64Number(io, &sec ->attributes)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, sec ->technology)) return FALSE;
+
+ if (!SaveDescription(self, io, sec ->Manufacturer)) return FALSE;
+ if (!SaveDescription(self, io, sec ->Model)) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_ProfileSequenceDesc_Dup(struct _cms_typehandler_struct* self, const void* Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsDupProfileSequenceDescription((cmsSEQ*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_ProfileSequenceDesc_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsFreeProfileSequenceDescription((cmsSEQ*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigProfileSequenceIdType
+// ********************************************************************************
+/*
+In certain workflows using ICC Device Link Profiles, it is necessary to identify the
+original profiles that were combined to create the Device Link Profile.
+This type is an array of structures, each of which contains information for
+identification of a profile used in a sequence
+*/
+
+
+static
+cmsBool ReadSeqID(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag)
+{
+ cmsSEQ* OutSeq = (cmsSEQ*) Cargo;
+ cmsPSEQDESC* seq = &OutSeq ->seq[n];
+
+ if (io -> Read(io, seq ->ProfileID.ID8, 16, 1) != 1) return FALSE;
+ if (!ReadEmbeddedText(self, io, &seq ->Description, SizeOfTag)) return FALSE;
+
+ return TRUE;
+}
+
+
+
+static
+void *Type_ProfileSequenceId_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsSEQ* OutSeq;
+ cmsUInt32Number Count;
+ cmsUInt32Number BaseOffset;
+
+ *nItems = 0;
+
+ // Get actual position as a basis for element offsets
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // Get table count
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ // Allocate an empty structure
+ OutSeq = cmsAllocProfileSequenceDescription(self ->ContextID, Count);
+ if (OutSeq == NULL) return NULL;
+
+
+ // Read the position table
+ if (!ReadPositionTable(self, io, Count, BaseOffset, OutSeq, ReadSeqID)) {
+
+ cmsFreeProfileSequenceDescription(OutSeq);
+ return NULL;
+ }
+
+ // Success
+ *nItems = 1;
+ return OutSeq;
+
+}
+
+
+static
+cmsBool WriteSeqID(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag)
+{
+ cmsSEQ* Seq = (cmsSEQ*) Cargo;
+
+ if (!io ->Write(io, 16, Seq ->seq[n].ProfileID.ID8)) return FALSE;
+
+ // Store here the MLU
+ if (!SaveDescription(self, io, Seq ->seq[n].Description)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool Type_ProfileSequenceId_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsSEQ* Seq = (cmsSEQ*) Ptr;
+ cmsUInt32Number BaseOffset;
+
+ // Keep the base offset
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // This is the table count
+ if (!_cmsWriteUInt32Number(io, Seq ->n)) return FALSE;
+
+ // This is the position table and content
+ if (!WritePositionTable(self, io, 0, Seq ->n, BaseOffset, Seq, WriteSeqID)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_ProfileSequenceId_Dup(struct _cms_typehandler_struct* self, const void* Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsDupProfileSequenceDescription((cmsSEQ*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_ProfileSequenceId_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsFreeProfileSequenceDescription((cmsSEQ*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigUcrBgType
+// ********************************************************************************
+/*
+This type contains curves representing the under color removal and black
+generation and a text string which is a general description of the method used
+for the ucr/bg.
+*/
+
+static
+void *Type_UcrBg_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUcrBg* n = (cmsUcrBg*) _cmsMallocZero(self ->ContextID, sizeof(cmsUcrBg));
+ cmsUInt32Number CountUcr, CountBg;
+ char* ASCIIString;
+
+ *nItems = 0;
+ if (n == NULL) return NULL;
+
+ // First curve is Under color removal
+ if (!_cmsReadUInt32Number(io, &CountUcr)) return NULL;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ n ->Ucr = cmsBuildTabulatedToneCurve16(self ->ContextID, CountUcr, NULL);
+ if (n ->Ucr == NULL) return NULL;
+
+ if (!_cmsReadUInt16Array(io, CountUcr, n ->Ucr->Table16)) return NULL;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+ SizeOfTag -= CountUcr * sizeof(cmsUInt16Number);
+
+ // Second curve is Black generation
+ if (!_cmsReadUInt32Number(io, &CountBg)) return NULL;
+ if (SizeOfTag < sizeof(cmsUInt32Number)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ n ->Bg = cmsBuildTabulatedToneCurve16(self ->ContextID, CountBg, NULL);
+ if (n ->Bg == NULL) return NULL;
+ if (!_cmsReadUInt16Array(io, CountBg, n ->Bg->Table16)) return NULL;
+ if (SizeOfTag < CountBg * sizeof(cmsUInt16Number)) return NULL;
+ SizeOfTag -= CountBg * sizeof(cmsUInt16Number);
+ if (SizeOfTag == UINT_MAX) return NULL;
+
+ // Now comes the text. The length is specified by the tag size
+ n ->Desc = cmsMLUalloc(self ->ContextID, 1);
+ if (n ->Desc == NULL) return NULL;
+
+ ASCIIString = (char*) _cmsMalloc(self ->ContextID, SizeOfTag + 1);
+ if (io ->Read(io, ASCIIString, sizeof(char), SizeOfTag) != SizeOfTag) return NULL;
+ ASCIIString[SizeOfTag] = 0;
+ cmsMLUsetASCII(n ->Desc, cmsNoLanguage, cmsNoCountry, ASCIIString);
+ _cmsFree(self ->ContextID, ASCIIString);
+
+ *nItems = 1;
+ return (void*) n;
+}
+
+static
+cmsBool Type_UcrBg_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUcrBg* Value = (cmsUcrBg*) Ptr;
+ cmsUInt32Number TextSize;
+ char* Text;
+
+ // First curve is Under color removal
+ if (!_cmsWriteUInt32Number(io, Value ->Ucr ->nEntries)) return FALSE;
+ if (!_cmsWriteUInt16Array(io, Value ->Ucr ->nEntries, Value ->Ucr ->Table16)) return FALSE;
+
+ // Then black generation
+ if (!_cmsWriteUInt32Number(io, Value ->Bg ->nEntries)) return FALSE;
+ if (!_cmsWriteUInt16Array(io, Value ->Bg ->nEntries, Value ->Bg ->Table16)) return FALSE;
+
+ // Now comes the text. The length is specified by the tag size
+ TextSize = cmsMLUgetASCII(Value ->Desc, cmsNoLanguage, cmsNoCountry, NULL, 0);
+ Text = (char*) _cmsMalloc(self ->ContextID, TextSize);
+ if (cmsMLUgetASCII(Value ->Desc, cmsNoLanguage, cmsNoCountry, Text, TextSize) != TextSize) return FALSE;
+
+ if (!io ->Write(io, TextSize, Text)) return FALSE;
+ _cmsFree(self ->ContextID, Text);
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_UcrBg_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ cmsUcrBg* Src = (cmsUcrBg*) Ptr;
+ cmsUcrBg* NewUcrBg = (cmsUcrBg*) _cmsMallocZero(self ->ContextID, sizeof(cmsUcrBg));
+
+ if (NewUcrBg == NULL) return NULL;
+
+ NewUcrBg ->Bg = cmsDupToneCurve(Src ->Bg);
+ NewUcrBg ->Ucr = cmsDupToneCurve(Src ->Ucr);
+ NewUcrBg ->Desc = cmsMLUdup(Src ->Desc);
+
+ return (void*) NewUcrBg;
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+static
+void Type_UcrBg_Free(struct _cms_typehandler_struct* self, void *Ptr)
+{
+ cmsUcrBg* Src = (cmsUcrBg*) Ptr;
+
+ if (Src ->Ucr) cmsFreeToneCurve(Src ->Ucr);
+ if (Src ->Bg) cmsFreeToneCurve(Src ->Bg);
+ if (Src ->Desc) cmsMLUfree(Src ->Desc);
+
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigCrdInfoType
+// ********************************************************************************
+
+/*
+This type contains the PostScript product name to which this profile corresponds
+and the names of the companion CRDs. Recall that a single profile can generate
+multiple CRDs. It is implemented as a MLU being the language code "PS" and then
+country varies for each element:
+
+ nm: PostScript product name
+ #0: Rendering intent 0 CRD name
+ #1: Rendering intent 1 CRD name
+ #2: Rendering intent 2 CRD name
+ #3: Rendering intent 3 CRD name
+*/
+
+
+
+// Auxiliar, read an string specified as count + string
+static
+cmsBool ReadCountAndSting(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsMLU* mlu, cmsUInt32Number* SizeOfTag, const char* Section)
+{
+ cmsUInt32Number Count;
+ char* Text;
+
+ if (*SizeOfTag < sizeof(cmsUInt32Number)) return FALSE;
+
+ if (!_cmsReadUInt32Number(io, &Count)) return FALSE;
+
+ if (Count > UINT_MAX - sizeof(cmsUInt32Number)) return FALSE;
+ if (*SizeOfTag < Count + sizeof(cmsUInt32Number)) return FALSE;
+
+ Text = (char*) _cmsMalloc(self ->ContextID, Count+1);
+ if (Text == NULL) return FALSE;
+
+ if (io ->Read(io, Text, sizeof(cmsUInt8Number), Count) != Count) {
+ _cmsFree(self ->ContextID, Text);
+ return FALSE;
+ }
+
+ Text[Count] = 0;
+
+ cmsMLUsetASCII(mlu, "PS", Section, Text);
+ _cmsFree(self ->ContextID, Text);
+
+ *SizeOfTag -= (Count + sizeof(cmsUInt32Number));
+ return TRUE;
+}
+
+static
+cmsBool WriteCountAndSting(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsMLU* mlu, const char* Section)
+{
+ cmsUInt32Number TextSize;
+ char* Text;
+
+ TextSize = cmsMLUgetASCII(mlu, "PS", Section, NULL, 0);
+ Text = (char*) _cmsMalloc(self ->ContextID, TextSize);
+
+ if (!_cmsWriteUInt32Number(io, TextSize)) return FALSE;
+
+ if (cmsMLUgetASCII(mlu, "PS", Section, Text, TextSize) == 0) return FALSE;
+
+ if (!io ->Write(io, TextSize, Text)) return FALSE;
+ _cmsFree(self ->ContextID, Text);
+
+ return TRUE;
+}
+
+static
+void *Type_CrdInfo_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsMLU* mlu = cmsMLUalloc(self ->ContextID, 5);
+
+ *nItems = 0;
+ if (!ReadCountAndSting(self, io, mlu, &SizeOfTag, "nm")) goto Error;
+ if (!ReadCountAndSting(self, io, mlu, &SizeOfTag, "#0")) goto Error;
+ if (!ReadCountAndSting(self, io, mlu, &SizeOfTag, "#1")) goto Error;
+ if (!ReadCountAndSting(self, io, mlu, &SizeOfTag, "#2")) goto Error;
+ if (!ReadCountAndSting(self, io, mlu, &SizeOfTag, "#3")) goto Error;
+
+ *nItems = 1;
+ return (void*) mlu;
+
+Error:
+ cmsMLUfree(mlu);
+ return NULL;
+
+}
+
+static
+cmsBool Type_CrdInfo_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+
+ cmsMLU* mlu = (cmsMLU*) Ptr;
+
+ if (!WriteCountAndSting(self, io, mlu, "nm")) goto Error;
+ if (!WriteCountAndSting(self, io, mlu, "#0")) goto Error;
+ if (!WriteCountAndSting(self, io, mlu, "#1")) goto Error;
+ if (!WriteCountAndSting(self, io, mlu, "#2")) goto Error;
+ if (!WriteCountAndSting(self, io, mlu, "#3")) goto Error;
+
+ return TRUE;
+
+Error:
+ return FALSE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_CrdInfo_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsMLUdup((cmsMLU*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_CrdInfo_Free(struct _cms_typehandler_struct* self, void *Ptr)
+{
+ cmsMLUfree((cmsMLU*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+// ********************************************************************************
+// Type cmsSigScreeningType
+// ********************************************************************************
+//
+//The screeningType describes various screening parameters including screen
+//frequency, screening angle, and spot shape.
+
+static
+void *Type_Screening_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsScreening* sc = NULL;
+ cmsUInt32Number i;
+
+ sc = (cmsScreening*) _cmsMallocZero(self ->ContextID, sizeof(cmsScreening));
+ if (sc == NULL) return NULL;
+
+ *nItems = 0;
+
+ if (!_cmsReadUInt32Number(io, &sc ->Flag)) goto Error;
+ if (!_cmsReadUInt32Number(io, &sc ->nChannels)) goto Error;
+
+ if (sc ->nChannels > cmsMAXCHANNELS - 1)
+ sc ->nChannels = cmsMAXCHANNELS - 1;
+
+ for (i=0; i < sc ->nChannels; i++) {
+
+ if (!_cmsRead15Fixed16Number(io, &sc ->Channels[i].Frequency)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &sc ->Channels[i].ScreenAngle)) goto Error;
+ if (!_cmsReadUInt32Number(io, &sc ->Channels[i].SpotShape)) goto Error;
+ }
+
+
+ *nItems = 1;
+
+ return (void*) sc;
+
+Error:
+ if (sc != NULL)
+ _cmsFree(self ->ContextID, sc);
+
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_Screening_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsScreening* sc = (cmsScreening* ) Ptr;
+ cmsUInt32Number i;
+
+ if (!_cmsWriteUInt32Number(io, sc ->Flag)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, sc ->nChannels)) return FALSE;
+
+ for (i=0; i < sc ->nChannels; i++) {
+
+ if (!_cmsWrite15Fixed16Number(io, sc ->Channels[i].Frequency)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, sc ->Channels[i].ScreenAngle)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, sc ->Channels[i].SpotShape)) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_Screening_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsScreening));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+
+static
+void Type_Screening_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+// ********************************************************************************
+// Type cmsSigViewingConditionsType
+// ********************************************************************************
+//
+//This type represents a set of viewing condition parameters including:
+//CIE ’absolute?illuminant white point tristimulus values and CIE ’absolute?//surround tristimulus values.
+
+static
+void *Type_ViewingConditions_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsICCViewingConditions* vc = NULL;
+
+ vc = (cmsICCViewingConditions*) _cmsMallocZero(self ->ContextID, sizeof(cmsICCViewingConditions));
+ if (vc == NULL) return NULL;
+
+ *nItems = 0;
+
+ if (!_cmsReadXYZNumber(io, &vc ->IlluminantXYZ)) goto Error;
+ if (!_cmsReadXYZNumber(io, &vc ->SurroundXYZ)) goto Error;
+ if (!_cmsReadUInt32Number(io, &vc ->IlluminantType)) goto Error;
+
+ *nItems = 1;
+
+ return (void*) vc;
+
+Error:
+ if (vc != NULL)
+ _cmsFree(self ->ContextID, vc);
+
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+static
+cmsBool Type_ViewingConditions_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsICCViewingConditions* sc = (cmsICCViewingConditions* ) Ptr;
+
+ if (!_cmsWriteXYZNumber(io, &sc ->IlluminantXYZ)) return FALSE;
+ if (!_cmsWriteXYZNumber(io, &sc ->SurroundXYZ)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, sc ->IlluminantType)) return FALSE;
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void* Type_ViewingConditions_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return _cmsDupMem(self ->ContextID, Ptr, sizeof(cmsScreening));
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+
+static
+void Type_ViewingConditions_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+// ********************************************************************************
+// Type cmsSigMultiProcessElementType
+// ********************************************************************************
+
+
+static
+void* GenericMPEdup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsStageDup((cmsStage*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void GenericMPEfree(struct _cms_typehandler_struct* self, void *Ptr)
+{
+ cmsStageFree((cmsStage*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+// Each curve is stored in one or more curve segments, with break-points specified between curve segments.
+// The first curve segment always starts at –Infinity, and the last curve segment always ends at +Infinity. The
+// first and last curve segments shall be specified in terms of a formula, whereas the other segments shall be
+// specified either in terms of a formula, or by a sampled curve.
+
+
+// Read an embedded segmented curve
+static
+cmsToneCurve* ReadSegmentedCurve(struct _cms_typehandler_struct* self, cmsIOHANDLER* io)
+{
+ cmsCurveSegSignature ElementSig;
+ cmsUInt32Number i, j;
+ cmsUInt16Number nSegments;
+ cmsCurveSegment* Segments;
+ cmsToneCurve* Curve;
+ cmsFloat32Number PrevBreak = -1E22F; // - infinite
+
+ // Take signature and channels for each element.
+ if (!_cmsReadUInt32Number(io, (cmsUInt32Number*) &ElementSig)) return NULL;
+
+ // That should be a segmented curve
+ if (ElementSig != cmsSigSegmentedCurve) return NULL;
+
+ if (!_cmsReadUInt32Number(io, NULL)) return NULL;
+ if (!_cmsReadUInt16Number(io, &nSegments)) return NULL;
+ if (!_cmsReadUInt16Number(io, NULL)) return NULL;
+
+ if (nSegments < 1) return NULL;
+ Segments = (cmsCurveSegment*) _cmsCalloc(self ->ContextID, nSegments, sizeof(cmsCurveSegment));
+ if (Segments == NULL) return NULL;
+
+ // Read breakpoints
+ for (i=0; i < (cmsUInt32Number) nSegments - 1; i++) {
+
+ Segments[i].x0 = PrevBreak;
+ if (!_cmsReadFloat32Number(io, &Segments[i].x1)) goto Error;
+ PrevBreak = Segments[i].x1;
+ }
+
+ Segments[nSegments-1].x0 = PrevBreak;
+ Segments[nSegments-1].x1 = 1E22F; // A big cmsFloat32Number number
+
+ // Read segments
+ for (i=0; i < nSegments; i++) {
+
+ if (!_cmsReadUInt32Number(io, (cmsUInt32Number*) &ElementSig)) goto Error;
+ if (!_cmsReadUInt32Number(io, NULL)) goto Error;
+
+ switch (ElementSig) {
+
+ case cmsSigFormulaCurveSeg: {
+
+ cmsUInt16Number Type;
+ cmsUInt32Number ParamsByType[] = {4, 5, 5 };
+
+ if (!_cmsReadUInt16Number(io, &Type)) goto Error;
+ if (!_cmsReadUInt16Number(io, NULL)) goto Error;
+
+ Segments[i].Type = Type + 6;
+ if (Type > 2) goto Error;
+
+ for (j=0; j < ParamsByType[Type]; j++) {
+
+ cmsFloat32Number f;
+ if (!_cmsReadFloat32Number(io, &f)) goto Error;
+ Segments[i].Params[j] = f;
+ }
+ }
+ break;
+
+
+ case cmsSigSampledCurveSeg: {
+ cmsUInt32Number Count;
+
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+
+ Segments[i].nGridPoints = Count;
+ Segments[i].SampledPoints = (cmsFloat32Number*) _cmsCalloc(self ->ContextID, Count, sizeof(cmsFloat32Number));
+ if (Segments[i].SampledPoints == NULL) goto Error;
+
+ for (j=0; j < Count; j++) {
+ if (!_cmsReadFloat32Number(io, &Segments[i].SampledPoints[j])) goto Error;
+ }
+ }
+ break;
+
+ default:
+ {
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) ElementSig);
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown curve element type '%s' found.", String);
+ }
+ return NULL;
+
+ }
+ }
+
+ Curve = cmsBuildSegmentedToneCurve(self ->ContextID, nSegments, Segments);
+
+ for (i=0; i < nSegments; i++) {
+ if (Segments[i].SampledPoints) _cmsFree(self ->ContextID, Segments[i].SampledPoints);
+ }
+ _cmsFree(self ->ContextID, Segments);
+ return Curve;
+
+Error:
+ if (Segments) _cmsFree(self ->ContextID, Segments);
+ return NULL;
+}
+
+
+static
+cmsBool ReadMPECurve(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag)
+{
+ cmsToneCurve** GammaTables = ( cmsToneCurve**) Cargo;
+
+ GammaTables[n] = ReadSegmentedCurve(self, io);
+ return (GammaTables[n] != NULL);
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+void *Type_MPEcurve_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsStage* mpe = NULL;
+ cmsUInt16Number InputChans, OutputChans;
+ cmsUInt32Number i, BaseOffset;
+ cmsToneCurve** GammaTables;
+
+ *nItems = 0;
+
+ // Get actual position as a basis for element offsets
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ if (!_cmsReadUInt16Number(io, &InputChans)) return NULL;
+ if (!_cmsReadUInt16Number(io, &OutputChans)) return NULL;
+
+ if (InputChans != OutputChans) return NULL;
+
+ GammaTables = (cmsToneCurve**) _cmsCalloc(self ->ContextID, InputChans, sizeof(cmsToneCurve*));
+ if (GammaTables == NULL) return NULL;
+
+ if (ReadPositionTable(self, io, InputChans, BaseOffset, GammaTables, ReadMPECurve)) {
+
+ mpe = cmsStageAllocToneCurves(self ->ContextID, InputChans, GammaTables);
+ }
+ else {
+ mpe = NULL;
+ }
+
+ for (i=0; i < InputChans; i++) {
+ if (GammaTables[i]) cmsFreeToneCurve(GammaTables[i]);
+ }
+
+ _cmsFree(self ->ContextID, GammaTables);
+ *nItems = (mpe != NULL) ? 1 : 0;
+ return mpe;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+// Write a single segmented curve. NO CHECK IS PERFORMED ON VALIDITY
+static
+cmsBool WriteSegmentedCurve(cmsIOHANDLER* io, cmsToneCurve* g)
+{
+ cmsUInt32Number i, j;
+ cmsCurveSegment* Segments = g ->Segments;
+ cmsUInt32Number nSegments = g ->nSegments;
+
+ if (!_cmsWriteUInt32Number(io, cmsSigSegmentedCurve)) goto Error;
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error;
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) nSegments)) goto Error;
+ if (!_cmsWriteUInt16Number(io, 0)) goto Error;
+
+ // Write the break-points
+ for (i=0; i < nSegments - 1; i++) {
+ if (!_cmsWriteFloat32Number(io, Segments[i].x1)) goto Error;
+ }
+
+ // Write the segments
+ for (i=0; i < g ->nSegments; i++) {
+
+ cmsCurveSegment* ActualSeg = Segments + i;
+
+ if (ActualSeg -> Type == 0) {
+
+ // This is a sampled curve
+ if (!_cmsWriteUInt32Number(io, (cmsUInt32Number) cmsSigSampledCurveSeg)) goto Error;
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error;
+ if (!_cmsWriteUInt32Number(io, ActualSeg -> nGridPoints)) goto Error;
+
+ for (j=0; j < g ->Segments[i].nGridPoints; j++) {
+ if (!_cmsWriteFloat32Number(io, ActualSeg -> SampledPoints[j])) goto Error;
+ }
+
+ }
+ else {
+ int Type;
+ cmsUInt32Number ParamsByType[] = { 4, 5, 5 };
+
+ // This is a formula-based
+ if (!_cmsWriteUInt32Number(io, (cmsUInt32Number) cmsSigFormulaCurveSeg)) goto Error;
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error;
+
+ // We only allow 1, 2 and 3 as types
+ Type = ActualSeg ->Type - 6;
+ if (Type > 2 || Type < 0) goto Error;
+
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) Type)) goto Error;
+ if (!_cmsWriteUInt16Number(io, 0)) goto Error;
+
+ for (j=0; j < ParamsByType[Type]; j++) {
+ if (!_cmsWriteFloat32Number(io, (cmsFloat32Number) ActualSeg ->Params[j])) goto Error;
+ }
+ }
+
+ // It seems there is no need to align. Code is here, and for safety commented out
+ // if (!_cmsWriteAlignment(io)) goto Error;
+ }
+
+ return TRUE;
+
+Error:
+ return FALSE;
+}
+
+
+static
+cmsBool WriteMPECurve(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag)
+{
+ _cmsStageToneCurvesData* Curves = (_cmsStageToneCurvesData*) Cargo;
+
+ return WriteSegmentedCurve(io, Curves ->TheCurves[n]);
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+ cmsUNUSED_PARAMETER(self);
+}
+
+// Write a curve, checking first for validity
+static
+cmsBool Type_MPEcurve_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt32Number BaseOffset;
+ cmsStage* mpe = (cmsStage*) Ptr;
+ _cmsStageToneCurvesData* Curves = (_cmsStageToneCurvesData*) mpe ->Data;
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // Write the header. Since those are curves, input and output channels are same
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->InputChannels)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->InputChannels)) return FALSE;
+
+ if (!WritePositionTable(self, io, 0,
+ mpe ->InputChannels, BaseOffset, Curves, WriteMPECurve)) return FALSE;
+
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+
+// The matrix is organized as an array of PxQ+Q elements, where P is the number of input channels to the
+// matrix, and Q is the number of output channels. The matrix elements are each float32Numbers. The array
+// is organized as follows:
+// array = [e11, e12, ? e1P, e21, e22, ? e2P, ? eQ1, eQ2, ? eQP, e1, e2, ? eQ]
+
+static
+void *Type_MPEmatrix_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsStage* mpe;
+ cmsUInt16Number InputChans, OutputChans;
+ cmsUInt32Number nElems, i;
+ cmsFloat64Number* Matrix;
+ cmsFloat64Number* Offsets;
+
+ if (!_cmsReadUInt16Number(io, &InputChans)) return NULL;
+ if (!_cmsReadUInt16Number(io, &OutputChans)) return NULL;
+
+
+ nElems = InputChans * OutputChans;
+
+ // Input and output chans may be ANY (up to 0xffff)
+ Matrix = (cmsFloat64Number*) _cmsCalloc(self ->ContextID, nElems, sizeof(cmsFloat64Number));
+ if (Matrix == NULL) return NULL;
+
+ Offsets = (cmsFloat64Number*) _cmsCalloc(self ->ContextID, OutputChans, sizeof(cmsFloat64Number));
+ if (Offsets == NULL) {
+
+ _cmsFree(self ->ContextID, Matrix);
+ return NULL;
+ }
+
+ for (i=0; i < nElems; i++) {
+
+ cmsFloat32Number v;
+
+ if (!_cmsReadFloat32Number(io, &v)) return NULL;
+ Matrix[i] = v;
+ }
+
+
+ for (i=0; i < OutputChans; i++) {
+
+ cmsFloat32Number v;
+
+ if (!_cmsReadFloat32Number(io, &v)) return NULL;
+ Offsets[i] = v;
+ }
+
+
+ mpe = cmsStageAllocMatrix(self ->ContextID, OutputChans, InputChans, Matrix, Offsets);
+ _cmsFree(self ->ContextID, Matrix);
+ _cmsFree(self ->ContextID, Offsets);
+
+ *nItems = 1;
+
+ return mpe;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+static
+cmsBool Type_MPEmatrix_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt32Number i, nElems;
+ cmsStage* mpe = (cmsStage*) Ptr;
+ _cmsStageMatrixData* Matrix = (_cmsStageMatrixData*) mpe ->Data;
+
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->InputChannels)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->OutputChannels)) return FALSE;
+
+ nElems = mpe ->InputChannels * mpe ->OutputChannels;
+
+ for (i=0; i < nElems; i++) {
+ if (!_cmsWriteFloat32Number(io, (cmsFloat32Number) Matrix->Double[i])) return FALSE;
+ }
+
+
+ for (i=0; i < mpe ->OutputChannels; i++) {
+
+ if (Matrix ->Offset == NULL) {
+
+ if (!_cmsWriteFloat32Number(io, 0)) return FALSE;
+ }
+ else {
+ if (!_cmsWriteFloat32Number(io, (cmsFloat32Number) Matrix->Offset[i])) return FALSE;
+ }
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+
+static
+void *Type_MPEclut_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsStage* mpe = NULL;
+ cmsUInt16Number InputChans, OutputChans;
+ cmsUInt8Number Dimensions8[16];
+ cmsUInt32Number i, nMaxGrids, GridPoints[MAX_INPUT_DIMENSIONS];
+ _cmsStageCLutData* clut;
+
+ if (!_cmsReadUInt16Number(io, &InputChans)) return NULL;
+ if (!_cmsReadUInt16Number(io, &OutputChans)) return NULL;
+
+ if (InputChans == 0) goto Error;
+ if (OutputChans == 0) goto Error;
+
+ if (io ->Read(io, Dimensions8, sizeof(cmsUInt8Number), 16) != 16)
+ goto Error;
+
+ // Copy MAX_INPUT_DIMENSIONS at most. Expand to cmsUInt32Number
+ nMaxGrids = InputChans > MAX_INPUT_DIMENSIONS ? MAX_INPUT_DIMENSIONS : InputChans;
+ for (i=0; i < nMaxGrids; i++) GridPoints[i] = (cmsUInt32Number) Dimensions8[i];
+
+ // Allocate the true CLUT
+ mpe = cmsStageAllocCLutFloatGranular(self ->ContextID, GridPoints, InputChans, OutputChans, NULL);
+ if (mpe == NULL) goto Error;
+
+ // Read the data
+ clut = (_cmsStageCLutData*) mpe ->Data;
+ for (i=0; i < clut ->nEntries; i++) {
+
+ if (!_cmsReadFloat32Number(io, &clut ->Tab.TFloat[i])) goto Error;
+ }
+
+ *nItems = 1;
+ return mpe;
+
+Error:
+ *nItems = 0;
+ if (mpe != NULL) cmsStageFree(mpe);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+// Write a CLUT in floating point
+static
+cmsBool Type_MPEclut_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt8Number Dimensions8[16];
+ cmsUInt32Number i;
+ cmsStage* mpe = (cmsStage*) Ptr;
+ _cmsStageCLutData* clut = (_cmsStageCLutData*) mpe ->Data;
+
+ // Check for maximum number of channels
+ if (mpe -> InputChannels > 15) return FALSE;
+
+ // Only floats are supported in MPE
+ if (clut ->HasFloatValues == FALSE) return FALSE;
+
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->InputChannels)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) mpe ->OutputChannels)) return FALSE;
+
+ memset(Dimensions8, 0, sizeof(Dimensions8));
+
+ for (i=0; i < mpe ->InputChannels; i++)
+ Dimensions8[i] = (cmsUInt8Number) clut ->Params ->nSamples[i];
+
+ if (!io ->Write(io, 16, Dimensions8)) return FALSE;
+
+ for (i=0; i < clut ->nEntries; i++) {
+
+ if (!_cmsWriteFloat32Number(io, clut ->Tab.TFloat[i])) return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(nItems);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+
+// This is the list of built-in MPE types
+static _cmsTagTypeLinkedList SupportedMPEtypes[] = {
+
+{{ (cmsTagTypeSignature) cmsSigBAcsElemType, NULL, NULL, NULL, NULL, NULL, 0 }, &SupportedMPEtypes[1] }, // Ignore those elements for now
+{{ (cmsTagTypeSignature) cmsSigEAcsElemType, NULL, NULL, NULL, NULL, NULL, 0 }, &SupportedMPEtypes[2] }, // (That's what the spec says)
+
+{TYPE_MPE_HANDLER((cmsTagTypeSignature) cmsSigCurveSetElemType, MPEcurve), &SupportedMPEtypes[3] },
+{TYPE_MPE_HANDLER((cmsTagTypeSignature) cmsSigMatrixElemType, MPEmatrix), &SupportedMPEtypes[4] },
+{TYPE_MPE_HANDLER((cmsTagTypeSignature) cmsSigCLutElemType, MPEclut), NULL },
+};
+
+_cmsTagTypePluginChunkType _cmsMPETypePluginChunk = { NULL };
+
+static
+cmsBool ReadMPEElem(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ void* Cargo,
+ cmsUInt32Number n,
+ cmsUInt32Number SizeOfTag)
+{
+ cmsStageSignature ElementSig;
+ cmsTagTypeHandler* TypeHandler;
+ cmsUInt32Number nItems;
+ cmsPipeline *NewLUT = (cmsPipeline *) Cargo;
+ _cmsTagTypePluginChunkType* MPETypePluginChunk = ( _cmsTagTypePluginChunkType*) _cmsContextGetClientChunk(self->ContextID, MPEPlugin);
+
+
+ // Take signature and channels for each element.
+ if (!_cmsReadUInt32Number(io, (cmsUInt32Number*) &ElementSig)) return FALSE;
+
+ // The reserved placeholder
+ if (!_cmsReadUInt32Number(io, NULL)) return FALSE;
+
+ // Read diverse MPE types
+ TypeHandler = GetHandler((cmsTagTypeSignature) ElementSig, MPETypePluginChunk ->TagTypes, SupportedMPEtypes);
+ if (TypeHandler == NULL) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) ElementSig);
+
+ // An unknown element was found.
+ cmsSignalError(self ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown MPE type '%s' found.", String);
+ return FALSE;
+ }
+
+ // If no read method, just ignore the element (valid for cmsSigBAcsElemType and cmsSigEAcsElemType)
+ // Read the MPE. No size is given
+ if (TypeHandler ->ReadPtr != NULL) {
+
+ // This is a real element which should be read and processed
+ if (!cmsPipelineInsertStage(NewLUT, cmsAT_END, (cmsStage*) TypeHandler ->ReadPtr(self, io, &nItems, SizeOfTag)))
+ return FALSE;
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+ cmsUNUSED_PARAMETER(n);
+}
+
+
+// This is the main dispatcher for MPE
+static
+void *Type_MPE_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsUInt16Number InputChans, OutputChans;
+ cmsUInt32Number ElementCount;
+ cmsPipeline *NewLUT = NULL;
+ cmsUInt32Number BaseOffset;
+
+ // Get actual position as a basis for element offsets
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // Read channels and element count
+ if (!_cmsReadUInt16Number(io, &InputChans)) return NULL;
+ if (!_cmsReadUInt16Number(io, &OutputChans)) return NULL;
+
+ // Allocates an empty LUT
+ NewLUT = cmsPipelineAlloc(self ->ContextID, InputChans, OutputChans);
+ if (NewLUT == NULL) return NULL;
+
+ if (!_cmsReadUInt32Number(io, &ElementCount)) return NULL;
+
+ if (!ReadPositionTable(self, io, ElementCount, BaseOffset, NewLUT, ReadMPEElem)) {
+ if (NewLUT != NULL) cmsPipelineFree(NewLUT);
+ *nItems = 0;
+ return NULL;
+ }
+
+ // Success
+ *nItems = 1;
+ return NewLUT;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+
+// This one is a liitle bit more complex, so we don't use position tables this time.
+static
+cmsBool Type_MPE_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsUInt32Number i, BaseOffset, DirectoryPos, CurrentPos;
+ int inputChan, outputChan;
+ cmsUInt32Number ElemCount;
+ cmsUInt32Number *ElementOffsets = NULL, *ElementSizes = NULL, Before;
+ cmsStageSignature ElementSig;
+ cmsPipeline* Lut = (cmsPipeline*) Ptr;
+ cmsStage* Elem = Lut ->Elements;
+ cmsTagTypeHandler* TypeHandler;
+ _cmsTagTypePluginChunkType* MPETypePluginChunk = ( _cmsTagTypePluginChunkType*) _cmsContextGetClientChunk(self->ContextID, MPEPlugin);
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ inputChan = cmsPipelineInputChannels(Lut);
+ outputChan = cmsPipelineOutputChannels(Lut);
+ ElemCount = cmsPipelineStageCount(Lut);
+
+ ElementOffsets = (cmsUInt32Number *) _cmsCalloc(self ->ContextID, ElemCount, sizeof(cmsUInt32Number));
+ if (ElementOffsets == NULL) goto Error;
+
+ ElementSizes = (cmsUInt32Number *) _cmsCalloc(self ->ContextID, ElemCount, sizeof(cmsUInt32Number));
+ if (ElementSizes == NULL) goto Error;
+
+ // Write the head
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) inputChan)) goto Error;
+ if (!_cmsWriteUInt16Number(io, (cmsUInt16Number) outputChan)) goto Error;
+ if (!_cmsWriteUInt32Number(io, (cmsUInt16Number) ElemCount)) goto Error;
+
+ DirectoryPos = io ->Tell(io);
+
+ // Write a fake directory to be filled latter on
+ for (i=0; i < ElemCount; i++) {
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error; // Offset
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error; // size
+ }
+
+ // Write each single tag. Keep track of the size as well.
+ for (i=0; i < ElemCount; i++) {
+
+ ElementOffsets[i] = io ->Tell(io) - BaseOffset;
+
+ ElementSig = Elem ->Type;
+
+ TypeHandler = GetHandler((cmsTagTypeSignature) ElementSig, MPETypePluginChunk->TagTypes, SupportedMPEtypes);
+ if (TypeHandler == NULL) {
+
+ char String[5];
+
+ _cmsTagSignature2String(String, (cmsTagSignature) ElementSig);
+
+ // An unknow element was found.
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Found unknown MPE type '%s'", String);
+ goto Error;
+ }
+
+ if (!_cmsWriteUInt32Number(io, ElementSig)) goto Error;
+ if (!_cmsWriteUInt32Number(io, 0)) goto Error;
+ Before = io ->Tell(io);
+ if (!TypeHandler ->WritePtr(self, io, Elem, 1)) goto Error;
+ if (!_cmsWriteAlignment(io)) goto Error;
+
+ ElementSizes[i] = io ->Tell(io) - Before;
+
+ Elem = Elem ->Next;
+ }
+
+ // Write the directory
+ CurrentPos = io ->Tell(io);
+
+ if (!io ->Seek(io, DirectoryPos)) goto Error;
+
+ for (i=0; i < ElemCount; i++) {
+ if (!_cmsWriteUInt32Number(io, ElementOffsets[i])) goto Error;
+ if (!_cmsWriteUInt32Number(io, ElementSizes[i])) goto Error;
+ }
+
+ if (!io ->Seek(io, CurrentPos)) goto Error;
+
+ if (ElementOffsets != NULL) _cmsFree(self ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(self ->ContextID, ElementSizes);
+ return TRUE;
+
+Error:
+ if (ElementOffsets != NULL) _cmsFree(self ->ContextID, ElementOffsets);
+ if (ElementSizes != NULL) _cmsFree(self ->ContextID, ElementSizes);
+ return FALSE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_MPE_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsPipelineDup((cmsPipeline*) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+static
+void Type_MPE_Free(struct _cms_typehandler_struct* self, void *Ptr)
+{
+ cmsPipelineFree((cmsPipeline*) Ptr);
+ return;
+
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type cmsSigVcgtType
+// ********************************************************************************
+
+
+#define cmsVideoCardGammaTableType 0
+#define cmsVideoCardGammaFormulaType 1
+
+// Used internally
+typedef struct {
+ double Gamma;
+ double Min;
+ double Max;
+} _cmsVCGTGAMMA;
+
+
+static
+void *Type_vcgt_Read(struct _cms_typehandler_struct* self,
+ cmsIOHANDLER* io,
+ cmsUInt32Number* nItems,
+ cmsUInt32Number SizeOfTag)
+{
+ cmsUInt32Number TagType, n, i;
+ cmsToneCurve** Curves;
+
+ *nItems = 0;
+
+ // Read tag type
+ if (!_cmsReadUInt32Number(io, &TagType)) return NULL;
+
+ // Allocate space for the array
+ Curves = ( cmsToneCurve**) _cmsCalloc(self ->ContextID, 3, sizeof(cmsToneCurve*));
+ if (Curves == NULL) return NULL;
+
+ // There are two possible flavors
+ switch (TagType) {
+
+ // Gamma is stored as a table
+ case cmsVideoCardGammaTableType:
+ {
+ cmsUInt16Number nChannels, nElems, nBytes;
+
+ // Check channel count, which should be 3 (we don't support monochrome this time)
+ if (!_cmsReadUInt16Number(io, &nChannels)) goto Error;
+
+ if (nChannels != 3) {
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported number of channels for VCGT '%d'", nChannels);
+ goto Error;
+ }
+
+ // Get Table element count and bytes per element
+ if (!_cmsReadUInt16Number(io, &nElems)) goto Error;
+ if (!_cmsReadUInt16Number(io, &nBytes)) goto Error;
+
+ // Adobe's quirk fixup. Fixing broken profiles...
+ if (nElems == 256 && nBytes == 1 && SizeOfTag == 1576)
+ nBytes = 2;
+
+
+ // Populate tone curves
+ for (n=0; n < 3; n++) {
+
+ Curves[n] = cmsBuildTabulatedToneCurve16(self ->ContextID, nElems, NULL);
+ if (Curves[n] == NULL) goto Error;
+
+ // On depending on byte depth
+ switch (nBytes) {
+
+ // One byte, 0..255
+ case 1:
+ for (i=0; i < nElems; i++) {
+
+ cmsUInt8Number v;
+
+ if (!_cmsReadUInt8Number(io, &v)) goto Error;
+ Curves[n] ->Table16[i] = FROM_8_TO_16(v);
+ }
+ break;
+
+ // One word 0..65535
+ case 2:
+ if (!_cmsReadUInt16Array(io, nElems, Curves[n]->Table16)) goto Error;
+ break;
+
+ // Unsupported
+ default:
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported bit depth for VCGT '%d'", nBytes * 8);
+ goto Error;
+ }
+ } // For all 3 channels
+ }
+ break;
+
+ // In this case, gamma is stored as a formula
+ case cmsVideoCardGammaFormulaType:
+ {
+ _cmsVCGTGAMMA Colorant[3];
+
+ // Populate tone curves
+ for (n=0; n < 3; n++) {
+
+ double Params[10];
+
+ if (!_cmsRead15Fixed16Number(io, &Colorant[n].Gamma)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Colorant[n].Min)) goto Error;
+ if (!_cmsRead15Fixed16Number(io, &Colorant[n].Max)) goto Error;
+
+ // Parametric curve type 5 is:
+ // Y = (aX + b)^Gamma + e | X >= d
+ // Y = cX + f | X < d
+
+ // vcgt formula is:
+ // Y = (Max ?Min) * (X ^ Gamma) + Min
+
+ // So, the translation is
+ // a = (Max ?Min) ^ ( 1 / Gamma)
+ // e = Min
+ // b=c=d=f=0
+
+ Params[0] = Colorant[n].Gamma;
+ Params[1] = pow((Colorant[n].Max - Colorant[n].Min), (1.0 / Colorant[n].Gamma));
+ Params[2] = 0;
+ Params[3] = 0;
+ Params[4] = 0;
+ Params[5] = Colorant[n].Min;
+ Params[6] = 0;
+
+ Curves[n] = cmsBuildParametricToneCurve(self ->ContextID, 5, Params);
+ if (Curves[n] == NULL) goto Error;
+ }
+ }
+ break;
+
+ // Unsupported
+ default:
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag type for VCGT '%d'", TagType);
+ goto Error;
+ }
+
+ *nItems = 1;
+ return (void*) Curves;
+
+// Regret, free all resources
+Error:
+
+ cmsFreeToneCurveTriple(Curves);
+ _cmsFree(self ->ContextID, Curves);
+ return NULL;
+
+ cmsUNUSED_PARAMETER(SizeOfTag);
+}
+
+
+// We don't support all flavors, only 16bits tables and formula
+static
+cmsBool Type_vcgt_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsToneCurve** Curves = (cmsToneCurve**) Ptr;
+ cmsUInt32Number i, j;
+
+ if (cmsGetToneCurveParametricType(Curves[0]) == 5 &&
+ cmsGetToneCurveParametricType(Curves[1]) == 5 &&
+ cmsGetToneCurveParametricType(Curves[2]) == 5) {
+
+ if (!_cmsWriteUInt32Number(io, cmsVideoCardGammaFormulaType)) return FALSE;
+
+ // Save parameters
+ for (i=0; i < 3; i++) {
+
+ _cmsVCGTGAMMA v;
+
+ v.Gamma = Curves[i] ->Segments[0].Params[0];
+ v.Min = Curves[i] ->Segments[0].Params[5];
+ v.Max = pow(Curves[i] ->Segments[0].Params[1], v.Gamma) + v.Min;
+
+ if (!_cmsWrite15Fixed16Number(io, v.Gamma)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, v.Min)) return FALSE;
+ if (!_cmsWrite15Fixed16Number(io, v.Max)) return FALSE;
+ }
+ }
+
+ else {
+
+ // Always store as a table of 256 words
+ if (!_cmsWriteUInt32Number(io, cmsVideoCardGammaTableType)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 3)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 256)) return FALSE;
+ if (!_cmsWriteUInt16Number(io, 2)) return FALSE;
+
+ for (i=0; i < 3; i++) {
+ for (j=0; j < 256; j++) {
+
+ cmsFloat32Number v = cmsEvalToneCurveFloat(Curves[i], (cmsFloat32Number) (j / 255.0));
+ cmsUInt16Number n = _cmsQuickSaturateWord(v * 65535.0);
+
+ if (!_cmsWriteUInt16Number(io, n)) return FALSE;
+ }
+ }
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(self);
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+static
+void* Type_vcgt_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ cmsToneCurve** OldCurves = (cmsToneCurve**) Ptr;
+ cmsToneCurve** NewCurves;
+
+ NewCurves = ( cmsToneCurve**) _cmsCalloc(self ->ContextID, 3, sizeof(cmsToneCurve*));
+ if (NewCurves == NULL) return NULL;
+
+ NewCurves[0] = cmsDupToneCurve(OldCurves[0]);
+ NewCurves[1] = cmsDupToneCurve(OldCurves[1]);
+ NewCurves[2] = cmsDupToneCurve(OldCurves[2]);
+
+ return (void*) NewCurves;
+
+ cmsUNUSED_PARAMETER(n);
+}
+
+
+static
+void Type_vcgt_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsFreeToneCurveTriple((cmsToneCurve**) Ptr);
+ _cmsFree(self ->ContextID, Ptr);
+}
+
+
+// ********************************************************************************
+// Type cmsSigDictType
+// ********************************************************************************
+
+// Single column of the table can point to wchar or MLUC elements. Holds arrays of data
+typedef struct {
+ cmsContext ContextID;
+ cmsUInt32Number *Offsets;
+ cmsUInt32Number *Sizes;
+} _cmsDICelem;
+
+typedef struct {
+ _cmsDICelem Name, Value, DisplayName, DisplayValue;
+
+} _cmsDICarray;
+
+// Allocate an empty array element
+static
+cmsBool AllocElem(cmsContext ContextID, _cmsDICelem* e, cmsUInt32Number Count)
+{
+ e->Offsets = (cmsUInt32Number *) _cmsCalloc(ContextID, Count, sizeof(cmsUInt32Number));
+ if (e->Offsets == NULL) return FALSE;
+
+ e->Sizes = (cmsUInt32Number *) _cmsCalloc(ContextID, Count, sizeof(cmsUInt32Number));
+ if (e->Sizes == NULL) {
+
+ _cmsFree(ContextID, e -> Offsets);
+ return FALSE;
+ }
+
+ e ->ContextID = ContextID;
+ return TRUE;
+}
+
+// Free an array element
+static
+void FreeElem(_cmsDICelem* e)
+{
+ if (e ->Offsets != NULL) _cmsFree(e -> ContextID, e -> Offsets);
+ if (e ->Sizes != NULL) _cmsFree(e -> ContextID, e -> Sizes);
+ e->Offsets = e ->Sizes = NULL;
+}
+
+// Get rid of whole array
+static
+void FreeArray( _cmsDICarray* a)
+{
+ if (a ->Name.Offsets != NULL) FreeElem(&a->Name);
+ if (a ->Value.Offsets != NULL) FreeElem(&a ->Value);
+ if (a ->DisplayName.Offsets != NULL) FreeElem(&a->DisplayName);
+ if (a ->DisplayValue.Offsets != NULL) FreeElem(&a ->DisplayValue);
+}
+
+
+// Allocate whole array
+static
+cmsBool AllocArray(cmsContext ContextID, _cmsDICarray* a, cmsUInt32Number Count, cmsUInt32Number Length)
+{
+ // Empty values
+ memset(a, 0, sizeof(_cmsDICarray));
+
+ // On depending on record size, create column arrays
+ if (!AllocElem(ContextID, &a ->Name, Count)) goto Error;
+ if (!AllocElem(ContextID, &a ->Value, Count)) goto Error;
+
+ if (Length > 16) {
+ if (!AllocElem(ContextID, &a -> DisplayName, Count)) goto Error;
+
+ }
+ if (Length > 24) {
+ if (!AllocElem(ContextID, &a ->DisplayValue, Count)) goto Error;
+ }
+ return TRUE;
+
+Error:
+ FreeArray(a);
+ return FALSE;
+}
+
+// Read one element
+static
+cmsBool ReadOneElem(cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i, cmsUInt32Number BaseOffset)
+{
+ if (!_cmsReadUInt32Number(io, &e->Offsets[i])) return FALSE;
+ if (!_cmsReadUInt32Number(io, &e ->Sizes[i])) return FALSE;
+
+ // An offset of zero has special meaning and shal be preserved
+ if (e ->Offsets[i] > 0)
+ e ->Offsets[i] += BaseOffset;
+ return TRUE;
+}
+
+
+static
+cmsBool ReadOffsetArray(cmsIOHANDLER* io, _cmsDICarray* a, cmsUInt32Number Count, cmsUInt32Number Length, cmsUInt32Number BaseOffset)
+{
+ cmsUInt32Number i;
+
+ // Read column arrays
+ for (i=0; i < Count; i++) {
+
+ if (!ReadOneElem(io, &a -> Name, i, BaseOffset)) return FALSE;
+ if (!ReadOneElem(io, &a -> Value, i, BaseOffset)) return FALSE;
+
+ if (Length > 16) {
+
+ if (!ReadOneElem(io, &a ->DisplayName, i, BaseOffset)) return FALSE;
+
+ }
+
+ if (Length > 24) {
+
+ if (!ReadOneElem(io, & a -> DisplayValue, i, BaseOffset)) return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+
+// Write one element
+static
+cmsBool WriteOneElem(cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i)
+{
+ if (!_cmsWriteUInt32Number(io, e->Offsets[i])) return FALSE;
+ if (!_cmsWriteUInt32Number(io, e ->Sizes[i])) return FALSE;
+
+ return TRUE;
+}
+
+static
+cmsBool WriteOffsetArray(cmsIOHANDLER* io, _cmsDICarray* a, cmsUInt32Number Count, cmsUInt32Number Length)
+{
+ cmsUInt32Number i;
+
+ for (i=0; i < Count; i++) {
+
+ if (!WriteOneElem(io, &a -> Name, i)) return FALSE;
+ if (!WriteOneElem(io, &a -> Value, i)) return FALSE;
+
+ if (Length > 16) {
+
+ if (!WriteOneElem(io, &a -> DisplayName, i)) return FALSE;
+ }
+
+ if (Length > 24) {
+
+ if (!WriteOneElem(io, &a -> DisplayValue, i)) return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+static
+cmsBool ReadOneWChar(cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i, wchar_t ** wcstr)
+{
+
+ cmsUInt32Number nChars;
+
+ // Special case for undefined strings (see ICC Votable
+ // Proposal Submission, Dictionary Type and Metadata TAG Definition)
+ if (e -> Offsets[i] == 0) {
+
+ *wcstr = NULL;
+ return TRUE;
+ }
+
+ if (!io -> Seek(io, e -> Offsets[i])) return FALSE;
+
+ nChars = e ->Sizes[i] / sizeof(cmsUInt16Number);
+
+
+ *wcstr = (wchar_t*) _cmsMallocZero(e ->ContextID, (nChars + 1) * sizeof(wchar_t));
+ if (*wcstr == NULL) return FALSE;
+
+ if (!_cmsReadWCharArray(io, nChars, *wcstr)) {
+ _cmsFree(e ->ContextID, *wcstr);
+ return FALSE;
+ }
+
+ // End of string marker
+ (*wcstr)[nChars] = 0;
+ return TRUE;
+}
+
+static
+cmsUInt32Number mywcslen(const wchar_t *s)
+{
+ const wchar_t *p;
+
+ p = s;
+ while (*p)
+ p++;
+
+ return (cmsUInt32Number)(p - s);
+}
+
+static
+cmsBool WriteOneWChar(cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i, const wchar_t * wcstr, cmsUInt32Number BaseOffset)
+{
+ cmsUInt32Number Before = io ->Tell(io);
+ cmsUInt32Number n;
+
+ e ->Offsets[i] = Before - BaseOffset;
+
+ if (wcstr == NULL) {
+ e ->Sizes[i] = 0;
+ e ->Offsets[i] = 0;
+ return TRUE;
+ }
+
+ n = mywcslen(wcstr);
+ if (!_cmsWriteWCharArray(io, n, wcstr)) return FALSE;
+
+ e ->Sizes[i] = io ->Tell(io) - Before;
+ return TRUE;
+}
+
+static
+cmsBool ReadOneMLUC(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i, cmsMLU** mlu)
+{
+ cmsUInt32Number nItems = 0;
+
+ // A way to get null MLUCs
+ if (e -> Offsets[i] == 0 || e ->Sizes[i] == 0) {
+
+ *mlu = NULL;
+ return TRUE;
+ }
+
+ if (!io -> Seek(io, e -> Offsets[i])) return FALSE;
+
+ *mlu = (cmsMLU*) Type_MLU_Read(self, io, &nItems, e ->Sizes[i]);
+ return *mlu != NULL;
+}
+
+static
+cmsBool WriteOneMLUC(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, _cmsDICelem* e, cmsUInt32Number i, const cmsMLU* mlu, cmsUInt32Number BaseOffset)
+{
+ cmsUInt32Number Before;
+
+ // Special case for undefined strings (see ICC Votable
+ // Proposal Submission, Dictionary Type and Metadata TAG Definition)
+ if (mlu == NULL) {
+ e ->Sizes[i] = 0;
+ e ->Offsets[i] = 0;
+ return TRUE;
+ }
+
+ Before = io ->Tell(io);
+ e ->Offsets[i] = Before - BaseOffset;
+
+ if (!Type_MLU_Write(self, io, (void*) mlu, 1)) return FALSE;
+
+ e ->Sizes[i] = io ->Tell(io) - Before;
+ return TRUE;
+}
+
+
+static
+void *Type_Dictionary_Read(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, cmsUInt32Number* nItems, cmsUInt32Number SizeOfTag)
+{
+ cmsHANDLE hDict;
+ cmsUInt32Number i, Count, Length;
+ cmsUInt32Number BaseOffset;
+ _cmsDICarray a;
+ wchar_t *NameWCS = NULL, *ValueWCS = NULL;
+ cmsMLU *DisplayNameMLU = NULL, *DisplayValueMLU=NULL;
+ cmsBool rc;
+
+ *nItems = 0;
+
+ // Get actual position as a basis for element offsets
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // Get name-value record count
+ if (!_cmsReadUInt32Number(io, &Count)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ // Get rec length
+ if (!_cmsReadUInt32Number(io, &Length)) return NULL;
+ SizeOfTag -= sizeof(cmsUInt32Number);
+
+ // Check for valid lengths
+ if (Length != 16 && Length != 24 && Length != 32) {
+ cmsSignalError(self->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown record length in dictionary '%d'", Length);
+ return NULL;
+ }
+
+ // Creates an empty dictionary
+ hDict = cmsDictAlloc(self -> ContextID);
+ if (hDict == NULL) return NULL;
+
+ // On depending on record size, create column arrays
+ if (!AllocArray(self -> ContextID, &a, Count, Length)) goto Error;
+
+ // Read column arrays
+ if (!ReadOffsetArray(io, &a, Count, Length, BaseOffset)) goto Error;
+
+ // Seek to each element and read it
+ for (i=0; i < Count; i++) {
+
+ if (!ReadOneWChar(io, &a.Name, i, &NameWCS)) goto Error;
+ if (!ReadOneWChar(io, &a.Value, i, &ValueWCS)) goto Error;
+
+ if (Length > 16) {
+ if (!ReadOneMLUC(self, io, &a.DisplayName, i, &DisplayNameMLU)) goto Error;
+ }
+
+ if (Length > 24) {
+ if (!ReadOneMLUC(self, io, &a.DisplayValue, i, &DisplayValueMLU)) goto Error;
+ }
+
+ if (NameWCS == NULL || ValueWCS == NULL) {
+
+ cmsSignalError(self->ContextID, cmsERROR_CORRUPTION_DETECTED, "Bad dictionary Name/Value");
+ rc = FALSE;
+ }
+ else {
+
+ rc = cmsDictAddEntry(hDict, NameWCS, ValueWCS, DisplayNameMLU, DisplayValueMLU);
+ }
+
+ if (NameWCS != NULL) _cmsFree(self ->ContextID, NameWCS);
+ if (ValueWCS != NULL) _cmsFree(self ->ContextID, ValueWCS);
+ if (DisplayNameMLU != NULL) cmsMLUfree(DisplayNameMLU);
+ if (DisplayValueMLU != NULL) cmsMLUfree(DisplayValueMLU);
+
+ if (!rc) goto Error;
+ }
+
+ FreeArray(&a);
+ *nItems = 1;
+ return (void*) hDict;
+
+Error:
+ FreeArray(&a);
+ cmsDictFree(hDict);
+ return NULL;
+}
+
+
+static
+cmsBool Type_Dictionary_Write(struct _cms_typehandler_struct* self, cmsIOHANDLER* io, void* Ptr, cmsUInt32Number nItems)
+{
+ cmsHANDLE hDict = (cmsHANDLE) Ptr;
+ const cmsDICTentry* p;
+ cmsBool AnyName, AnyValue;
+ cmsUInt32Number i, Count, Length;
+ cmsUInt32Number DirectoryPos, CurrentPos, BaseOffset;
+ _cmsDICarray a;
+
+ if (hDict == NULL) return FALSE;
+
+ BaseOffset = io ->Tell(io) - sizeof(_cmsTagBase);
+
+ // Let's inspect the dictionary
+ Count = 0; AnyName = FALSE; AnyValue = FALSE;
+ for (p = cmsDictGetEntryList(hDict); p != NULL; p = cmsDictNextEntry(p)) {
+
+ if (p ->DisplayName != NULL) AnyName = TRUE;
+ if (p ->DisplayValue != NULL) AnyValue = TRUE;
+ Count++;
+ }
+
+ Length = 16;
+ if (AnyName) Length += 8;
+ if (AnyValue) Length += 8;
+
+ if (!_cmsWriteUInt32Number(io, Count)) return FALSE;
+ if (!_cmsWriteUInt32Number(io, Length)) return FALSE;
+
+ // Keep starting position of offsets table
+ DirectoryPos = io ->Tell(io);
+
+ // Allocate offsets array
+ if (!AllocArray(self ->ContextID, &a, Count, Length)) goto Error;
+
+ // Write a fake directory to be filled latter on
+ if (!WriteOffsetArray(io, &a, Count, Length)) goto Error;
+
+ // Write each element. Keep track of the size as well.
+ p = cmsDictGetEntryList(hDict);
+ for (i=0; i < Count; i++) {
+
+ if (!WriteOneWChar(io, &a.Name, i, p ->Name, BaseOffset)) goto Error;
+ if (!WriteOneWChar(io, &a.Value, i, p ->Value, BaseOffset)) goto Error;
+
+ if (p ->DisplayName != NULL) {
+ if (!WriteOneMLUC(self, io, &a.DisplayName, i, p ->DisplayName, BaseOffset)) goto Error;
+ }
+
+ if (p ->DisplayValue != NULL) {
+ if (!WriteOneMLUC(self, io, &a.DisplayValue, i, p ->DisplayValue, BaseOffset)) goto Error;
+ }
+
+ p = cmsDictNextEntry(p);
+ }
+
+ // Write the directory
+ CurrentPos = io ->Tell(io);
+ if (!io ->Seek(io, DirectoryPos)) goto Error;
+
+ if (!WriteOffsetArray(io, &a, Count, Length)) goto Error;
+
+ if (!io ->Seek(io, CurrentPos)) goto Error;
+
+ FreeArray(&a);
+ return TRUE;
+
+Error:
+ FreeArray(&a);
+ return FALSE;
+
+ cmsUNUSED_PARAMETER(nItems);
+}
+
+
+static
+void* Type_Dictionary_Dup(struct _cms_typehandler_struct* self, const void *Ptr, cmsUInt32Number n)
+{
+ return (void*) cmsDictDup((cmsHANDLE) Ptr);
+
+ cmsUNUSED_PARAMETER(n);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+static
+void Type_Dictionary_Free(struct _cms_typehandler_struct* self, void* Ptr)
+{
+ cmsDictFree((cmsHANDLE) Ptr);
+ cmsUNUSED_PARAMETER(self);
+}
+
+
+// ********************************************************************************
+// Type support main routines
+// ********************************************************************************
+
+
+// This is the list of built-in types
+static _cmsTagTypeLinkedList SupportedTagTypes[] = {
+
+{TYPE_HANDLER(cmsSigChromaticityType, Chromaticity), &SupportedTagTypes[1] },
+{TYPE_HANDLER(cmsSigColorantOrderType, ColorantOrderType), &SupportedTagTypes[2] },
+{TYPE_HANDLER(cmsSigS15Fixed16ArrayType, S15Fixed16), &SupportedTagTypes[3] },
+{TYPE_HANDLER(cmsSigU16Fixed16ArrayType, U16Fixed16), &SupportedTagTypes[4] },
+{TYPE_HANDLER(cmsSigTextType, Text), &SupportedTagTypes[5] },
+{TYPE_HANDLER(cmsSigTextDescriptionType, Text_Description), &SupportedTagTypes[6] },
+{TYPE_HANDLER(cmsSigCurveType, Curve), &SupportedTagTypes[7] },
+{TYPE_HANDLER(cmsSigParametricCurveType, ParametricCurve), &SupportedTagTypes[8] },
+{TYPE_HANDLER(cmsSigDateTimeType, DateTime), &SupportedTagTypes[9] },
+{TYPE_HANDLER(cmsSigLut8Type, LUT8), &SupportedTagTypes[10] },
+{TYPE_HANDLER(cmsSigLut16Type, LUT16), &SupportedTagTypes[11] },
+{TYPE_HANDLER(cmsSigColorantTableType, ColorantTable), &SupportedTagTypes[12] },
+{TYPE_HANDLER(cmsSigNamedColor2Type, NamedColor), &SupportedTagTypes[13] },
+{TYPE_HANDLER(cmsSigMultiLocalizedUnicodeType, MLU), &SupportedTagTypes[14] },
+{TYPE_HANDLER(cmsSigProfileSequenceDescType, ProfileSequenceDesc), &SupportedTagTypes[15] },
+{TYPE_HANDLER(cmsSigSignatureType, Signature), &SupportedTagTypes[16] },
+{TYPE_HANDLER(cmsSigMeasurementType, Measurement), &SupportedTagTypes[17] },
+{TYPE_HANDLER(cmsSigDataType, Data), &SupportedTagTypes[18] },
+{TYPE_HANDLER(cmsSigLutAtoBType, LUTA2B), &SupportedTagTypes[19] },
+{TYPE_HANDLER(cmsSigLutBtoAType, LUTB2A), &SupportedTagTypes[20] },
+{TYPE_HANDLER(cmsSigUcrBgType, UcrBg), &SupportedTagTypes[21] },
+{TYPE_HANDLER(cmsSigCrdInfoType, CrdInfo), &SupportedTagTypes[22] },
+{TYPE_HANDLER(cmsSigMultiProcessElementType, MPE), &SupportedTagTypes[23] },
+{TYPE_HANDLER(cmsSigScreeningType, Screening), &SupportedTagTypes[24] },
+{TYPE_HANDLER(cmsSigViewingConditionsType, ViewingConditions), &SupportedTagTypes[25] },
+{TYPE_HANDLER(cmsSigXYZType, XYZ), &SupportedTagTypes[26] },
+{TYPE_HANDLER(cmsCorbisBrokenXYZtype, XYZ), &SupportedTagTypes[27] },
+{TYPE_HANDLER(cmsMonacoBrokenCurveType, Curve), &SupportedTagTypes[28] },
+{TYPE_HANDLER(cmsSigProfileSequenceIdType, ProfileSequenceId), &SupportedTagTypes[29] },
+{TYPE_HANDLER(cmsSigDictType, Dictionary), &SupportedTagTypes[30] },
+{TYPE_HANDLER(cmsSigVcgtType, vcgt), NULL }
+};
+
+
+_cmsTagTypePluginChunkType _cmsTagTypePluginChunk = { NULL };
+
+
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupTagTypeList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src,
+ int loc)
+{
+ _cmsTagTypePluginChunkType newHead = { NULL };
+ _cmsTagTypeLinkedList* entry;
+ _cmsTagTypeLinkedList* Anterior = NULL;
+ _cmsTagTypePluginChunkType* head = (_cmsTagTypePluginChunkType*) src->chunks[loc];
+
+ // Walk the list copying all nodes
+ for (entry = head->TagTypes;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ _cmsTagTypeLinkedList *newEntry = ( _cmsTagTypeLinkedList *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsTagTypeLinkedList));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.TagTypes == NULL)
+ newHead.TagTypes = newEntry;
+ }
+
+ ctx ->chunks[loc] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsTagTypePluginChunkType));
+}
+
+
+void _cmsAllocTagTypePluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ // Duplicate the LIST
+ DupTagTypeList(ctx, src, TagTypePlugin);
+ }
+ else {
+ static _cmsTagTypePluginChunkType TagTypePluginChunk = { NULL };
+ ctx ->chunks[TagTypePlugin] = _cmsSubAllocDup(ctx ->MemPool, &TagTypePluginChunk, sizeof(_cmsTagTypePluginChunkType));
+ }
+}
+
+void _cmsAllocMPETypePluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ // Duplicate the LIST
+ DupTagTypeList(ctx, src, MPEPlugin);
+ }
+ else {
+ static _cmsTagTypePluginChunkType TagTypePluginChunk = { NULL };
+ ctx ->chunks[MPEPlugin] = _cmsSubAllocDup(ctx ->MemPool, &TagTypePluginChunk, sizeof(_cmsTagTypePluginChunkType));
+ }
+
+}
+
+
+// Both kind of plug-ins share same structure
+cmsBool _cmsRegisterTagTypePlugin(cmsContext id, cmsPluginBase* Data)
+{
+ return RegisterTypesPlugin(id, Data, TagTypePlugin);
+}
+
+cmsBool _cmsRegisterMultiProcessElementPlugin(cmsContext id, cmsPluginBase* Data)
+{
+ return RegisterTypesPlugin(id, Data,MPEPlugin);
+}
+
+
+// Wrapper for tag types
+cmsTagTypeHandler* _cmsGetTagTypeHandler(cmsContext ContextID, cmsTagTypeSignature sig)
+{
+ _cmsTagTypePluginChunkType* ctx = ( _cmsTagTypePluginChunkType*) _cmsContextGetClientChunk(ContextID, TagTypePlugin);
+
+ return GetHandler(sig, ctx->TagTypes, SupportedTagTypes);
+}
+
+// ********************************************************************************
+// Tag support main routines
+// ********************************************************************************
+
+typedef struct _cmsTagLinkedList_st {
+
+ cmsTagSignature Signature;
+ cmsTagDescriptor Descriptor;
+ struct _cmsTagLinkedList_st* Next;
+
+} _cmsTagLinkedList;
+
+// This is the list of built-in tags
+static _cmsTagLinkedList SupportedTags[] = {
+
+ { cmsSigAToB0Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutAtoBType, cmsSigLut8Type}, DecideLUTtypeA2B}, &SupportedTags[1]},
+ { cmsSigAToB1Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutAtoBType, cmsSigLut8Type}, DecideLUTtypeA2B}, &SupportedTags[2]},
+ { cmsSigAToB2Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutAtoBType, cmsSigLut8Type}, DecideLUTtypeA2B}, &SupportedTags[3]},
+ { cmsSigBToA0Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type}, DecideLUTtypeB2A}, &SupportedTags[4]},
+ { cmsSigBToA1Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type}, DecideLUTtypeB2A}, &SupportedTags[5]},
+ { cmsSigBToA2Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type}, DecideLUTtypeB2A}, &SupportedTags[6]},
+
+ // Allow corbis and its broken XYZ type
+ { cmsSigRedColorantTag, { 1, 2, { cmsSigXYZType, cmsCorbisBrokenXYZtype }, DecideXYZtype}, &SupportedTags[7]},
+ { cmsSigGreenColorantTag, { 1, 2, { cmsSigXYZType, cmsCorbisBrokenXYZtype }, DecideXYZtype}, &SupportedTags[8]},
+ { cmsSigBlueColorantTag, { 1, 2, { cmsSigXYZType, cmsCorbisBrokenXYZtype }, DecideXYZtype}, &SupportedTags[9]},
+
+ { cmsSigRedTRCTag, { 1, 3, { cmsSigCurveType, cmsSigParametricCurveType, cmsMonacoBrokenCurveType }, DecideCurveType}, &SupportedTags[10]},
+ { cmsSigGreenTRCTag, { 1, 3, { cmsSigCurveType, cmsSigParametricCurveType, cmsMonacoBrokenCurveType }, DecideCurveType}, &SupportedTags[11]},
+ { cmsSigBlueTRCTag, { 1, 3, { cmsSigCurveType, cmsSigParametricCurveType, cmsMonacoBrokenCurveType }, DecideCurveType}, &SupportedTags[12]},
+
+ { cmsSigCalibrationDateTimeTag, { 1, 1, { cmsSigDateTimeType }, NULL}, &SupportedTags[13]},
+ { cmsSigCharTargetTag, { 1, 1, { cmsSigTextType }, NULL}, &SupportedTags[14]},
+
+ { cmsSigChromaticAdaptationTag, { 9, 1, { cmsSigS15Fixed16ArrayType }, NULL}, &SupportedTags[15]},
+ { cmsSigChromaticityTag, { 1, 1, { cmsSigChromaticityType }, NULL}, &SupportedTags[16]},
+ { cmsSigColorantOrderTag, { 1, 1, { cmsSigColorantOrderType }, NULL}, &SupportedTags[17]},
+ { cmsSigColorantTableTag, { 1, 1, { cmsSigColorantTableType }, NULL}, &SupportedTags[18]},
+ { cmsSigColorantTableOutTag, { 1, 1, { cmsSigColorantTableType }, NULL}, &SupportedTags[19]},
+
+ { cmsSigCopyrightTag, { 1, 3, { cmsSigTextType, cmsSigMultiLocalizedUnicodeType, cmsSigTextDescriptionType}, DecideTextType}, &SupportedTags[20]},
+ { cmsSigDateTimeTag, { 1, 1, { cmsSigDateTimeType }, NULL}, &SupportedTags[21]},
+
+ { cmsSigDeviceMfgDescTag, { 1, 3, { cmsSigTextDescriptionType, cmsSigMultiLocalizedUnicodeType, cmsSigTextType}, DecideTextDescType}, &SupportedTags[22]},
+ { cmsSigDeviceModelDescTag, { 1, 3, { cmsSigTextDescriptionType, cmsSigMultiLocalizedUnicodeType, cmsSigTextType}, DecideTextDescType}, &SupportedTags[23]},
+
+ { cmsSigGamutTag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type }, DecideLUTtypeB2A}, &SupportedTags[24]},
+
+ { cmsSigGrayTRCTag, { 1, 2, { cmsSigCurveType, cmsSigParametricCurveType }, DecideCurveType}, &SupportedTags[25]},
+ { cmsSigLuminanceTag, { 1, 1, { cmsSigXYZType }, NULL}, &SupportedTags[26]},
+
+ { cmsSigMediaBlackPointTag, { 1, 2, { cmsSigXYZType, cmsCorbisBrokenXYZtype }, NULL}, &SupportedTags[27]},
+ { cmsSigMediaWhitePointTag, { 1, 2, { cmsSigXYZType, cmsCorbisBrokenXYZtype }, NULL}, &SupportedTags[28]},
+
+ { cmsSigNamedColor2Tag, { 1, 1, { cmsSigNamedColor2Type }, NULL}, &SupportedTags[29]},
+
+ { cmsSigPreview0Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type }, DecideLUTtypeB2A}, &SupportedTags[30]},
+ { cmsSigPreview1Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type }, DecideLUTtypeB2A}, &SupportedTags[31]},
+ { cmsSigPreview2Tag, { 1, 3, { cmsSigLut16Type, cmsSigLutBtoAType, cmsSigLut8Type }, DecideLUTtypeB2A}, &SupportedTags[32]},
+
+ { cmsSigProfileDescriptionTag, { 1, 3, { cmsSigTextDescriptionType, cmsSigMultiLocalizedUnicodeType, cmsSigTextType}, DecideTextDescType}, &SupportedTags[33]},
+ { cmsSigProfileSequenceDescTag, { 1, 1, { cmsSigProfileSequenceDescType }, NULL}, &SupportedTags[34]},
+ { cmsSigTechnologyTag, { 1, 1, { cmsSigSignatureType }, NULL}, &SupportedTags[35]},
+
+ { cmsSigColorimetricIntentImageStateTag, { 1, 1, { cmsSigSignatureType }, NULL}, &SupportedTags[36]},
+ { cmsSigPerceptualRenderingIntentGamutTag, { 1, 1, { cmsSigSignatureType }, NULL}, &SupportedTags[37]},
+ { cmsSigSaturationRenderingIntentGamutTag, { 1, 1, { cmsSigSignatureType }, NULL}, &SupportedTags[38]},
+
+ { cmsSigMeasurementTag, { 1, 1, { cmsSigMeasurementType }, NULL}, &SupportedTags[39]},
+
+ { cmsSigPs2CRD0Tag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[40]},
+ { cmsSigPs2CRD1Tag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[41]},
+ { cmsSigPs2CRD2Tag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[42]},
+ { cmsSigPs2CRD3Tag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[43]},
+ { cmsSigPs2CSATag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[44]},
+ { cmsSigPs2RenderingIntentTag, { 1, 1, { cmsSigDataType }, NULL}, &SupportedTags[45]},
+
+ { cmsSigViewingCondDescTag, { 1, 3, { cmsSigTextDescriptionType, cmsSigMultiLocalizedUnicodeType, cmsSigTextType}, DecideTextDescType}, &SupportedTags[46]},
+
+ { cmsSigUcrBgTag, { 1, 1, { cmsSigUcrBgType}, NULL}, &SupportedTags[47]},
+ { cmsSigCrdInfoTag, { 1, 1, { cmsSigCrdInfoType}, NULL}, &SupportedTags[48]},
+
+ { cmsSigDToB0Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[49]},
+ { cmsSigDToB1Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[50]},
+ { cmsSigDToB2Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[51]},
+ { cmsSigDToB3Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[52]},
+ { cmsSigBToD0Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[53]},
+ { cmsSigBToD1Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[54]},
+ { cmsSigBToD2Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[55]},
+ { cmsSigBToD3Tag, { 1, 1, { cmsSigMultiProcessElementType}, NULL}, &SupportedTags[56]},
+
+ { cmsSigScreeningDescTag, { 1, 1, { cmsSigTextDescriptionType }, NULL}, &SupportedTags[57]},
+ { cmsSigViewingConditionsTag, { 1, 1, { cmsSigViewingConditionsType }, NULL}, &SupportedTags[58]},
+
+ { cmsSigScreeningTag, { 1, 1, { cmsSigScreeningType}, NULL }, &SupportedTags[59]},
+ { cmsSigVcgtTag, { 1, 1, { cmsSigVcgtType}, NULL }, &SupportedTags[60]},
+ { cmsSigMetaTag, { 1, 1, { cmsSigDictType}, NULL }, &SupportedTags[61]},
+ { cmsSigProfileSequenceIdTag, { 1, 1, { cmsSigProfileSequenceIdType}, NULL }, &SupportedTags[62]},
+ { cmsSigProfileDescriptionMLTag,{ 1, 1, { cmsSigMultiLocalizedUnicodeType}, NULL}, NULL}
+
+
+};
+
+/*
+ Not supported Why
+ ======================= =========================================
+ cmsSigOutputResponseTag ==> WARNING, POSSIBLE PATENT ON THIS SUBJECT!
+ cmsSigNamedColorTag ==> Deprecated
+ cmsSigDataTag ==> Ancient, unused
+ cmsSigDeviceSettingsTag ==> Deprecated, useless
+*/
+
+
+_cmsTagPluginChunkType _cmsTagPluginChunk = { NULL };
+
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupTagList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsTagPluginChunkType newHead = { NULL };
+ _cmsTagLinkedList* entry;
+ _cmsTagLinkedList* Anterior = NULL;
+ _cmsTagPluginChunkType* head = (_cmsTagPluginChunkType*) src->chunks[TagPlugin];
+
+ // Walk the list copying all nodes
+ for (entry = head->Tag;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ _cmsTagLinkedList *newEntry = ( _cmsTagLinkedList *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsTagLinkedList));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.Tag == NULL)
+ newHead.Tag = newEntry;
+ }
+
+ ctx ->chunks[TagPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsTagPluginChunkType));
+}
+
+void _cmsAllocTagPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ DupTagList(ctx, src);
+ }
+ else {
+ static _cmsTagPluginChunkType TagPluginChunk = { NULL };
+ ctx ->chunks[TagPlugin] = _cmsSubAllocDup(ctx ->MemPool, &TagPluginChunk, sizeof(_cmsTagPluginChunkType));
+ }
+
+}
+
+cmsBool _cmsRegisterTagPlugin(cmsContext id, cmsPluginBase* Data)
+{
+ cmsPluginTag* Plugin = (cmsPluginTag*) Data;
+ _cmsTagLinkedList *pt;
+ _cmsTagPluginChunkType* TagPluginChunk = ( _cmsTagPluginChunkType*) _cmsContextGetClientChunk(id, TagPlugin);
+
+ if (Data == NULL) {
+
+ TagPluginChunk->Tag = NULL;
+ return TRUE;
+ }
+
+ pt = (_cmsTagLinkedList*) _cmsPluginMalloc(id, sizeof(_cmsTagLinkedList));
+ if (pt == NULL) return FALSE;
+
+ pt ->Signature = Plugin ->Signature;
+ pt ->Descriptor = Plugin ->Descriptor;
+ pt ->Next = TagPluginChunk ->Tag;
+
+ TagPluginChunk ->Tag = pt;
+
+ return TRUE;
+}
+
+// Return a descriptor for a given tag or NULL
+cmsTagDescriptor* _cmsGetTagDescriptor(cmsContext ContextID, cmsTagSignature sig)
+{
+ _cmsTagLinkedList* pt;
+ _cmsTagPluginChunkType* TagPluginChunk = ( _cmsTagPluginChunkType*) _cmsContextGetClientChunk(ContextID, TagPlugin);
+
+ for (pt = TagPluginChunk->Tag;
+ pt != NULL;
+ pt = pt ->Next) {
+
+ if (sig == pt -> Signature) return &pt ->Descriptor;
+ }
+
+ for (pt = SupportedTags;
+ pt != NULL;
+ pt = pt ->Next) {
+
+ if (sig == pt -> Signature) return &pt ->Descriptor;
+ }
+
+ return NULL;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsvirt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsvirt.c
new file mode 100644
index 0000000000..b324c9902a
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsvirt.c
@@ -0,0 +1,1194 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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"
+
+// Virtual (built-in) profiles
+// -----------------------------------------------------------------------------------
+
+static
+cmsBool SetTextTags(cmsHPROFILE hProfile, const wchar_t* Description)
+{
+ cmsMLU *DescriptionMLU, *CopyrightMLU;
+ cmsBool rc = FALSE;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ DescriptionMLU = cmsMLUalloc(ContextID, 1);
+ CopyrightMLU = cmsMLUalloc(ContextID, 1);
+
+ if (DescriptionMLU == NULL || CopyrightMLU == NULL) goto Error;
+
+ if (!cmsMLUsetWide(DescriptionMLU, "en", "US", Description)) goto Error;
+ if (!cmsMLUsetWide(CopyrightMLU, "en", "US", L"No copyright, use freely")) goto Error;
+
+ if (!cmsWriteTag(hProfile, cmsSigProfileDescriptionTag, DescriptionMLU)) goto Error;
+ if (!cmsWriteTag(hProfile, cmsSigCopyrightTag, CopyrightMLU)) goto Error;
+
+ rc = TRUE;
+
+Error:
+
+ if (DescriptionMLU)
+ cmsMLUfree(DescriptionMLU);
+ if (CopyrightMLU)
+ cmsMLUfree(CopyrightMLU);
+ return rc;
+}
+
+
+static
+cmsBool SetSeqDescTag(cmsHPROFILE hProfile, const char* Model)
+{
+ cmsBool rc = FALSE;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ cmsSEQ* Seq = cmsAllocProfileSequenceDescription(ContextID, 1);
+
+ if (Seq == NULL) return FALSE;
+
+ Seq->seq[0].deviceMfg = (cmsSignature) 0;
+ Seq->seq[0].deviceModel = (cmsSignature) 0;
+
+#ifdef CMS_DONT_USE_INT64
+ Seq->seq[0].attributes[0] = 0;
+ Seq->seq[0].attributes[1] = 0;
+#else
+ Seq->seq[0].attributes = 0;
+#endif
+
+ Seq->seq[0].technology = (cmsTechnologySignature) 0;
+
+ cmsMLUsetASCII( Seq->seq[0].Manufacturer, cmsNoLanguage, cmsNoCountry, "Little CMS");
+ cmsMLUsetASCII( Seq->seq[0].Model, cmsNoLanguage, cmsNoCountry, Model);
+
+ if (!_cmsWriteProfileSequence(hProfile, Seq)) goto Error;
+
+ rc = TRUE;
+
+Error:
+ if (Seq)
+ cmsFreeProfileSequenceDescription(Seq);
+
+ return rc;
+}
+
+
+
+// This function creates a profile based on White point, primaries and
+// transfer functions.
+cmsHPROFILE CMSEXPORT cmsCreateRGBProfileTHR(cmsContext ContextID,
+ const cmsCIExyY* WhitePoint,
+ const cmsCIExyYTRIPLE* Primaries,
+ cmsToneCurve* const TransferFunction[3])
+{
+ cmsHPROFILE hICC;
+ cmsMAT3 MColorants;
+ cmsCIEXYZTRIPLE Colorants;
+ cmsCIExyY MaxWhite;
+ cmsMAT3 CHAD;
+ cmsCIEXYZ WhitePointXYZ;
+
+ hICC = cmsCreateProfilePlaceholder(ContextID);
+ if (!hICC) // can't allocate
+ return NULL;
+
+ cmsSetProfileVersion(hICC, 4.3);
+
+ cmsSetDeviceClass(hICC, cmsSigDisplayClass);
+ cmsSetColorSpace(hICC, cmsSigRgbData);
+ cmsSetPCS(hICC, cmsSigXYZData);
+
+ cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+
+ // Implement profile using following tags:
+ //
+ // 1 cmsSigProfileDescriptionTag
+ // 2 cmsSigMediaWhitePointTag
+ // 3 cmsSigRedColorantTag
+ // 4 cmsSigGreenColorantTag
+ // 5 cmsSigBlueColorantTag
+ // 6 cmsSigRedTRCTag
+ // 7 cmsSigGreenTRCTag
+ // 8 cmsSigBlueTRCTag
+ // 9 Chromatic adaptation Tag
+ // This conforms a standard RGB DisplayProfile as says ICC, and then I add (As per addendum II)
+ // 10 cmsSigChromaticityTag
+
+
+ if (!SetTextTags(hICC, L"RGB built-in")) goto Error;
+
+ if (WhitePoint) {
+
+ if (!cmsWriteTag(hICC, cmsSigMediaWhitePointTag, cmsD50_XYZ())) goto Error;
+
+ cmsxyY2XYZ(&WhitePointXYZ, WhitePoint);
+ _cmsAdaptationMatrix(&CHAD, NULL, &WhitePointXYZ, cmsD50_XYZ());
+
+ // This is a V4 tag, but many CMM does read and understand it no matter which version
+ if (!cmsWriteTag(hICC, cmsSigChromaticAdaptationTag, (void*) &CHAD)) goto Error;
+ }
+
+ if (WhitePoint && Primaries) {
+
+ MaxWhite.x = WhitePoint -> x;
+ MaxWhite.y = WhitePoint -> y;
+ MaxWhite.Y = 1.0;
+
+ if (!_cmsBuildRGB2XYZtransferMatrix(&MColorants, &MaxWhite, Primaries)) goto Error;
+
+ Colorants.Red.X = MColorants.v[0].n[0];
+ Colorants.Red.Y = MColorants.v[1].n[0];
+ Colorants.Red.Z = MColorants.v[2].n[0];
+
+ Colorants.Green.X = MColorants.v[0].n[1];
+ Colorants.Green.Y = MColorants.v[1].n[1];
+ Colorants.Green.Z = MColorants.v[2].n[1];
+
+ Colorants.Blue.X = MColorants.v[0].n[2];
+ Colorants.Blue.Y = MColorants.v[1].n[2];
+ Colorants.Blue.Z = MColorants.v[2].n[2];
+
+ if (!cmsWriteTag(hICC, cmsSigRedColorantTag, (void*) &Colorants.Red)) goto Error;
+ if (!cmsWriteTag(hICC, cmsSigBlueColorantTag, (void*) &Colorants.Blue)) goto Error;
+ if (!cmsWriteTag(hICC, cmsSigGreenColorantTag, (void*) &Colorants.Green)) goto Error;
+ }
+
+
+ if (TransferFunction) {
+
+ // Tries to minimize space. Thanks to Richard Hughes for this nice idea
+ if (!cmsWriteTag(hICC, cmsSigRedTRCTag, (void*) TransferFunction[0])) goto Error;
+
+ if (TransferFunction[1] == TransferFunction[0]) {
+
+ if (!cmsLinkTag (hICC, cmsSigGreenTRCTag, cmsSigRedTRCTag)) goto Error;
+
+ } else {
+
+ if (!cmsWriteTag(hICC, cmsSigGreenTRCTag, (void*) TransferFunction[1])) goto Error;
+ }
+
+ if (TransferFunction[2] == TransferFunction[0]) {
+
+ if (!cmsLinkTag (hICC, cmsSigBlueTRCTag, cmsSigRedTRCTag)) goto Error;
+
+ } else {
+
+ if (!cmsWriteTag(hICC, cmsSigBlueTRCTag, (void*) TransferFunction[2])) goto Error;
+ }
+ }
+
+ if (Primaries) {
+ if (!cmsWriteTag(hICC, cmsSigChromaticityTag, (void*) Primaries)) goto Error;
+ }
+
+
+ return hICC;
+
+Error:
+ if (hICC)
+ cmsCloseProfile(hICC);
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreateRGBProfile(const cmsCIExyY* WhitePoint,
+ const cmsCIExyYTRIPLE* Primaries,
+ cmsToneCurve* const TransferFunction[3])
+{
+ return cmsCreateRGBProfileTHR(NULL, WhitePoint, Primaries, TransferFunction);
+}
+
+
+
+// This function creates a profile based on White point and transfer function.
+cmsHPROFILE CMSEXPORT cmsCreateGrayProfileTHR(cmsContext ContextID,
+ const cmsCIExyY* WhitePoint,
+ const cmsToneCurve* TransferFunction)
+{
+ cmsHPROFILE hICC;
+ cmsCIEXYZ tmp;
+
+ hICC = cmsCreateProfilePlaceholder(ContextID);
+ if (!hICC) // can't allocate
+ return NULL;
+
+ cmsSetProfileVersion(hICC, 4.3);
+
+ cmsSetDeviceClass(hICC, cmsSigDisplayClass);
+ cmsSetColorSpace(hICC, cmsSigGrayData);
+ cmsSetPCS(hICC, cmsSigXYZData);
+ cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+
+ // Implement profile using following tags:
+ //
+ // 1 cmsSigProfileDescriptionTag
+ // 2 cmsSigMediaWhitePointTag
+ // 3 cmsSigGrayTRCTag
+
+ // This conforms a standard Gray DisplayProfile
+
+ // Fill-in the tags
+
+ if (!SetTextTags(hICC, L"gray built-in")) goto Error;
+
+
+ if (WhitePoint) {
+
+ cmsxyY2XYZ(&tmp, WhitePoint);
+ if (!cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void*) &tmp)) goto Error;
+ }
+
+ if (TransferFunction) {
+
+ if (!cmsWriteTag(hICC, cmsSigGrayTRCTag, (void*) TransferFunction)) goto Error;
+ }
+
+ return hICC;
+
+Error:
+ if (hICC)
+ cmsCloseProfile(hICC);
+ return NULL;
+}
+
+
+
+cmsHPROFILE CMSEXPORT cmsCreateGrayProfile(const cmsCIExyY* WhitePoint,
+ const cmsToneCurve* TransferFunction)
+{
+ return cmsCreateGrayProfileTHR(NULL, WhitePoint, TransferFunction);
+}
+
+// This is a devicelink operating in the target colorspace with as many transfer functions as components
+
+cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLinkTHR(cmsContext ContextID,
+ cmsColorSpaceSignature ColorSpace,
+ cmsToneCurve* const TransferFunctions[])
+{
+ cmsHPROFILE hICC;
+ cmsPipeline* Pipeline;
+ int nChannels;
+
+ hICC = cmsCreateProfilePlaceholder(ContextID);
+ if (!hICC)
+ return NULL;
+
+ cmsSetProfileVersion(hICC, 4.3);
+
+ cmsSetDeviceClass(hICC, cmsSigLinkClass);
+ cmsSetColorSpace(hICC, ColorSpace);
+ cmsSetPCS(hICC, ColorSpace);
+
+ cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+ // Set up channels
+ nChannels = cmsChannelsOf(ColorSpace);
+
+ // Creates a Pipeline with prelinearization step only
+ Pipeline = cmsPipelineAlloc(ContextID, nChannels, nChannels);
+ if (Pipeline == NULL) goto Error;
+
+
+ // Copy tables to Pipeline
+ if (!cmsPipelineInsertStage(Pipeline, cmsAT_BEGIN, cmsStageAllocToneCurves(ContextID, nChannels, TransferFunctions)))
+ goto Error;
+
+ // Create tags
+ if (!SetTextTags(hICC, L"Linearization built-in")) goto Error;
+ if (!cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) Pipeline)) goto Error;
+ if (!SetSeqDescTag(hICC, "Linearization built-in")) goto Error;
+
+ // Pipeline is already on virtual profile
+ cmsPipelineFree(Pipeline);
+
+ // Ok, done
+ return hICC;
+
+Error:
+ cmsPipelineFree(Pipeline);
+ if (hICC)
+ cmsCloseProfile(hICC);
+
+
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreateLinearizationDeviceLink(cmsColorSpaceSignature ColorSpace,
+ cmsToneCurve* const TransferFunctions[])
+{
+ return cmsCreateLinearizationDeviceLinkTHR(NULL, ColorSpace, TransferFunctions);
+}
+
+// Ink-limiting algorithm
+//
+// Sum = C + M + Y + K
+// If Sum > InkLimit
+// Ratio= 1 - (Sum - InkLimit) / (C + M + Y)
+// if Ratio <0
+// Ratio=0
+// endif
+// Else
+// Ratio=1
+// endif
+//
+// C = Ratio * C
+// M = Ratio * M
+// Y = Ratio * Y
+// K: Does not change
+
+static
+int InkLimitingSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ cmsFloat64Number InkLimit = *(cmsFloat64Number *) Cargo;
+ cmsFloat64Number SumCMY, SumCMYK, Ratio;
+
+ InkLimit = (InkLimit * 655.35);
+
+ SumCMY = In[0] + In[1] + In[2];
+ SumCMYK = SumCMY + In[3];
+
+ if (SumCMYK > InkLimit) {
+
+ Ratio = 1 - ((SumCMYK - InkLimit) / SumCMY);
+ if (Ratio < 0)
+ Ratio = 0;
+ }
+ else Ratio = 1;
+
+ Out[0] = _cmsQuickSaturateWord(In[0] * Ratio); // C
+ Out[1] = _cmsQuickSaturateWord(In[1] * Ratio); // M
+ Out[2] = _cmsQuickSaturateWord(In[2] * Ratio); // Y
+
+ Out[3] = In[3]; // K (untouched)
+
+ return TRUE;
+}
+
+// This is a devicelink operating in CMYK for ink-limiting
+
+cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLinkTHR(cmsContext ContextID,
+ cmsColorSpaceSignature ColorSpace,
+ cmsFloat64Number Limit)
+{
+ cmsHPROFILE hICC;
+ cmsPipeline* LUT;
+ cmsStage* CLUT;
+ int nChannels;
+
+ if (ColorSpace != cmsSigCmykData) {
+ cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "InkLimiting: Only CMYK currently supported");
+ return NULL;
+ }
+
+ if (Limit < 0.0 || Limit > 400) {
+
+ cmsSignalError(ContextID, cmsERROR_RANGE, "InkLimiting: Limit should be between 0..400");
+ if (Limit < 0) Limit = 0;
+ if (Limit > 400) Limit = 400;
+
+ }
+
+ hICC = cmsCreateProfilePlaceholder(ContextID);
+ if (!hICC) // can't allocate
+ return NULL;
+
+ cmsSetProfileVersion(hICC, 4.3);
+
+ cmsSetDeviceClass(hICC, cmsSigLinkClass);
+ cmsSetColorSpace(hICC, ColorSpace);
+ cmsSetPCS(hICC, ColorSpace);
+
+ cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+
+ // Creates a Pipeline with 3D grid only
+ LUT = cmsPipelineAlloc(ContextID, 4, 4);
+ if (LUT == NULL) goto Error;
+
+
+ nChannels = cmsChannelsOf(ColorSpace);
+
+ CLUT = cmsStageAllocCLut16bit(ContextID, 17, nChannels, nChannels, NULL);
+ if (CLUT == NULL) goto Error;
+
+ if (!cmsStageSampleCLut16bit(CLUT, InkLimitingSampler, (void*) &Limit, 0)) goto Error;
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, nChannels)) ||
+ !cmsPipelineInsertStage(LUT, cmsAT_END, CLUT) ||
+ !cmsPipelineInsertStage(LUT, cmsAT_END, _cmsStageAllocIdentityCurves(ContextID, nChannels)))
+ goto Error;
+
+ // Create tags
+ if (!SetTextTags(hICC, L"ink-limiting built-in")) goto Error;
+
+ if (!cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) LUT)) goto Error;
+ if (!SetSeqDescTag(hICC, "ink-limiting built-in")) goto Error;
+
+ // cmsPipeline is already on virtual profile
+ cmsPipelineFree(LUT);
+
+ // Ok, done
+ return hICC;
+
+Error:
+ if (LUT != NULL)
+ cmsPipelineFree(LUT);
+
+ if (hICC != NULL)
+ cmsCloseProfile(hICC);
+
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreateInkLimitingDeviceLink(cmsColorSpaceSignature ColorSpace, cmsFloat64Number Limit)
+{
+ return cmsCreateInkLimitingDeviceLinkTHR(NULL, ColorSpace, Limit);
+}
+
+
+// Creates a fake Lab identity.
+cmsHPROFILE CMSEXPORT cmsCreateLab2ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint)
+{
+ cmsHPROFILE hProfile;
+ cmsPipeline* LUT = NULL;
+
+ hProfile = cmsCreateRGBProfileTHR(ContextID, WhitePoint == NULL ? cmsD50_xyY() : WhitePoint, NULL, NULL);
+ if (hProfile == NULL) return NULL;
+
+ cmsSetProfileVersion(hProfile, 2.1);
+
+ cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
+ cmsSetColorSpace(hProfile, cmsSigLabData);
+ cmsSetPCS(hProfile, cmsSigLabData);
+
+ if (!SetTextTags(hProfile, L"Lab identity built-in")) return NULL;
+
+ // An identity LUT is all we need
+ LUT = cmsPipelineAlloc(ContextID, 3, 3);
+ if (LUT == NULL) goto Error;
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCLut(ContextID, 3)))
+ goto Error;
+
+ if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
+ cmsPipelineFree(LUT);
+
+ return hProfile;
+
+Error:
+
+ if (LUT != NULL)
+ cmsPipelineFree(LUT);
+
+ if (hProfile != NULL)
+ cmsCloseProfile(hProfile);
+
+ return NULL;
+}
+
+
+cmsHPROFILE CMSEXPORT cmsCreateLab2Profile(const cmsCIExyY* WhitePoint)
+{
+ return cmsCreateLab2ProfileTHR(NULL, WhitePoint);
+}
+
+
+// Creates a fake Lab V4 identity.
+cmsHPROFILE CMSEXPORT cmsCreateLab4ProfileTHR(cmsContext ContextID, const cmsCIExyY* WhitePoint)
+{
+ cmsHPROFILE hProfile;
+ cmsPipeline* LUT = NULL;
+
+ hProfile = cmsCreateRGBProfileTHR(ContextID, WhitePoint == NULL ? cmsD50_xyY() : WhitePoint, NULL, NULL);
+ if (hProfile == NULL) return NULL;
+
+ cmsSetProfileVersion(hProfile, 4.3);
+
+ cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
+ cmsSetColorSpace(hProfile, cmsSigLabData);
+ cmsSetPCS(hProfile, cmsSigLabData);
+
+ if (!SetTextTags(hProfile, L"Lab identity built-in")) goto Error;
+
+ // An empty LUTs is all we need
+ LUT = cmsPipelineAlloc(ContextID, 3, 3);
+ if (LUT == NULL) goto Error;
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, 3)))
+ goto Error;
+
+ if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
+ cmsPipelineFree(LUT);
+
+ return hProfile;
+
+Error:
+
+ if (LUT != NULL)
+ cmsPipelineFree(LUT);
+
+ if (hProfile != NULL)
+ cmsCloseProfile(hProfile);
+
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreateLab4Profile(const cmsCIExyY* WhitePoint)
+{
+ return cmsCreateLab4ProfileTHR(NULL, WhitePoint);
+}
+
+
+// Creates a fake XYZ identity
+cmsHPROFILE CMSEXPORT cmsCreateXYZProfileTHR(cmsContext ContextID)
+{
+ cmsHPROFILE hProfile;
+ cmsPipeline* LUT = NULL;
+
+ hProfile = cmsCreateRGBProfileTHR(ContextID, cmsD50_xyY(), NULL, NULL);
+ if (hProfile == NULL) return NULL;
+
+ cmsSetProfileVersion(hProfile, 4.3);
+
+ cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
+ cmsSetColorSpace(hProfile, cmsSigXYZData);
+ cmsSetPCS(hProfile, cmsSigXYZData);
+
+ if (!SetTextTags(hProfile, L"XYZ identity built-in")) goto Error;
+
+ // An identity LUT is all we need
+ LUT = cmsPipelineAlloc(ContextID, 3, 3);
+ if (LUT == NULL) goto Error;
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, 3)))
+ goto Error;
+
+ if (!cmsWriteTag(hProfile, cmsSigAToB0Tag, LUT)) goto Error;
+ cmsPipelineFree(LUT);
+
+ return hProfile;
+
+Error:
+
+ if (LUT != NULL)
+ cmsPipelineFree(LUT);
+
+ if (hProfile != NULL)
+ cmsCloseProfile(hProfile);
+
+ return NULL;
+}
+
+
+cmsHPROFILE CMSEXPORT cmsCreateXYZProfile(void)
+{
+ return cmsCreateXYZProfileTHR(NULL);
+}
+
+
+//sRGB Curves are defined by:
+//
+//If R’sRGB,G’sRGB, B’sRGB < 0.04045
+//
+// R = R’sRGB / 12.92
+// G = G’sRGB / 12.92
+// B = B’sRGB / 12.92
+//
+//
+//else if R’sRGB,G’sRGB, B’sRGB >= 0.04045
+//
+// R = ((R’sRGB + 0.055) / 1.055)^2.4
+// G = ((G’sRGB + 0.055) / 1.055)^2.4
+// B = ((B’sRGB + 0.055) / 1.055)^2.4
+
+static
+cmsToneCurve* Build_sRGBGamma(cmsContext ContextID)
+{
+ cmsFloat64Number Parameters[5];
+
+ Parameters[0] = 2.4;
+ Parameters[1] = 1. / 1.055;
+ Parameters[2] = 0.055 / 1.055;
+ Parameters[3] = 1. / 12.92;
+ Parameters[4] = 0.04045;
+
+ return cmsBuildParametricToneCurve(ContextID, 4, Parameters);
+}
+
+// Create the ICC virtual profile for sRGB space
+cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfileTHR(cmsContext ContextID)
+{
+ cmsCIExyY D65;
+ cmsCIExyYTRIPLE Rec709Primaries = {
+ {0.6400, 0.3300, 1.0},
+ {0.3000, 0.6000, 1.0},
+ {0.1500, 0.0600, 1.0}
+ };
+ cmsToneCurve* Gamma22[3];
+ cmsHPROFILE hsRGB;
+
+ cmsWhitePointFromTemp(&D65, 6504);
+ Gamma22[0] = Gamma22[1] = Gamma22[2] = Build_sRGBGamma(ContextID);
+ if (Gamma22[0] == NULL) return NULL;
+
+ hsRGB = cmsCreateRGBProfileTHR(ContextID, &D65, &Rec709Primaries, Gamma22);
+ cmsFreeToneCurve(Gamma22[0]);
+ if (hsRGB == NULL) return NULL;
+
+ if (!SetTextTags(hsRGB, L"sRGB built-in")) {
+ cmsCloseProfile(hsRGB);
+ return NULL;
+ }
+
+ return hsRGB;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreate_sRGBProfile(void)
+{
+ return cmsCreate_sRGBProfileTHR(NULL);
+}
+
+
+
+typedef struct {
+ cmsFloat64Number Brightness;
+ cmsFloat64Number Contrast;
+ cmsFloat64Number Hue;
+ cmsFloat64Number Saturation;
+ cmsCIEXYZ WPsrc, WPdest;
+
+} BCHSWADJUSTS, *LPBCHSWADJUSTS;
+
+
+static
+int bchswSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ cmsCIELab LabIn, LabOut;
+ cmsCIELCh LChIn, LChOut;
+ cmsCIEXYZ XYZ;
+ LPBCHSWADJUSTS bchsw = (LPBCHSWADJUSTS) Cargo;
+
+
+ cmsLabEncoded2Float(&LabIn, In);
+
+
+ cmsLab2LCh(&LChIn, &LabIn);
+
+ // Do some adjusts on LCh
+
+ LChOut.L = LChIn.L * bchsw ->Contrast + bchsw ->Brightness;
+ LChOut.C = LChIn.C + bchsw -> Saturation;
+ LChOut.h = LChIn.h + bchsw -> Hue;
+
+
+ cmsLCh2Lab(&LabOut, &LChOut);
+
+ // Move white point in Lab
+
+ cmsLab2XYZ(&bchsw ->WPsrc, &XYZ, &LabOut);
+ cmsXYZ2Lab(&bchsw ->WPdest, &LabOut, &XYZ);
+
+ // Back to encoded
+
+ cmsFloat2LabEncoded(Out, &LabOut);
+
+ return TRUE;
+}
+
+
+// Creates an abstract profile operating in Lab space for Brightness,
+// contrast, Saturation and white point displacement
+
+cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfileTHR(cmsContext ContextID,
+ int nLUTPoints,
+ cmsFloat64Number Bright,
+ cmsFloat64Number Contrast,
+ cmsFloat64Number Hue,
+ cmsFloat64Number Saturation,
+ int TempSrc,
+ int TempDest)
+{
+ cmsHPROFILE hICC;
+ cmsPipeline* Pipeline;
+ BCHSWADJUSTS bchsw;
+ cmsCIExyY WhitePnt;
+ cmsStage* CLUT;
+ cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+ int i;
+
+ bchsw.Brightness = Bright;
+ bchsw.Contrast = Contrast;
+ bchsw.Hue = Hue;
+ bchsw.Saturation = Saturation;
+
+ cmsWhitePointFromTemp(&WhitePnt, TempSrc );
+ cmsxyY2XYZ(&bchsw.WPsrc, &WhitePnt);
+
+ cmsWhitePointFromTemp(&WhitePnt, TempDest);
+ cmsxyY2XYZ(&bchsw.WPdest, &WhitePnt);
+
+ hICC = cmsCreateProfilePlaceholder(ContextID);
+ if (!hICC) // can't allocate
+ return NULL;
+
+
+ cmsSetDeviceClass(hICC, cmsSigAbstractClass);
+ cmsSetColorSpace(hICC, cmsSigLabData);
+ cmsSetPCS(hICC, cmsSigLabData);
+
+ cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+ // Creates a Pipeline with 3D grid only
+ Pipeline = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Pipeline == NULL) {
+ cmsCloseProfile(hICC);
+ return NULL;
+ }
+
+ for (i=0; i < MAX_INPUT_DIMENSIONS; i++) Dimensions[i] = nLUTPoints;
+ CLUT = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, 3, 3, NULL);
+ if (CLUT == NULL) return NULL;
+
+
+ if (!cmsStageSampleCLut16bit(CLUT, bchswSampler, (void*) &bchsw, 0)) {
+
+ // Shouldn't reach here
+ goto Error;
+ }
+
+ if (!cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT)) {
+ goto Error;
+ }
+
+ // Create tags
+ if (!SetTextTags(hICC, L"BCHS built-in")) return NULL;
+
+ cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void*) cmsD50_XYZ());
+
+ cmsWriteTag(hICC, cmsSigAToB0Tag, (void*) Pipeline);
+
+ // Pipeline is already on virtual profile
+ cmsPipelineFree(Pipeline);
+
+ // Ok, done
+ return hICC;
+
+Error:
+ cmsPipelineFree(Pipeline);
+ cmsCloseProfile(hICC);
+ return NULL;
+}
+
+
+CMSAPI cmsHPROFILE CMSEXPORT cmsCreateBCHSWabstractProfile(int nLUTPoints,
+ cmsFloat64Number Bright,
+ cmsFloat64Number Contrast,
+ cmsFloat64Number Hue,
+ cmsFloat64Number Saturation,
+ int TempSrc,
+ int TempDest)
+{
+ return cmsCreateBCHSWabstractProfileTHR(NULL, nLUTPoints, Bright, Contrast, Hue, Saturation, TempSrc, TempDest);
+}
+
+
+// Creates a fake NULL profile. This profile return 1 channel as always 0.
+// Is useful only for gamut checking tricks
+cmsHPROFILE CMSEXPORT cmsCreateNULLProfileTHR(cmsContext ContextID)
+{
+ cmsHPROFILE hProfile;
+ cmsPipeline* LUT = NULL;
+ cmsStage* PostLin;
+ cmsToneCurve* EmptyTab;
+ cmsUInt16Number Zero[2] = { 0, 0 };
+
+ hProfile = cmsCreateProfilePlaceholder(ContextID);
+ if (!hProfile) // can't allocate
+ return NULL;
+
+ cmsSetProfileVersion(hProfile, 4.3);
+
+ if (!SetTextTags(hProfile, L"NULL profile built-in")) goto Error;
+
+
+
+ cmsSetDeviceClass(hProfile, cmsSigOutputClass);
+ cmsSetColorSpace(hProfile, cmsSigGrayData);
+ cmsSetPCS(hProfile, cmsSigLabData);
+
+ // An empty LUTs is all we need
+ LUT = cmsPipelineAlloc(ContextID, 1, 1);
+ if (LUT == NULL) goto Error;
+
+ EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
+ PostLin = cmsStageAllocToneCurves(ContextID, 1, &EmptyTab);
+ cmsFreeToneCurve(EmptyTab);
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_END, PostLin))
+ goto Error;
+
+ if (!cmsWriteTag(hProfile, cmsSigBToA0Tag, (void*) LUT)) goto Error;
+ if (!cmsWriteTag(hProfile, cmsSigMediaWhitePointTag, cmsD50_XYZ())) goto Error;
+
+ cmsPipelineFree(LUT);
+ return hProfile;
+
+Error:
+
+ if (LUT != NULL)
+ cmsPipelineFree(LUT);
+
+ if (hProfile != NULL)
+ cmsCloseProfile(hProfile);
+
+ return NULL;
+}
+
+cmsHPROFILE CMSEXPORT cmsCreateNULLProfile(void)
+{
+ return cmsCreateNULLProfileTHR(NULL);
+}
+
+
+static
+int IsPCS(cmsColorSpaceSignature ColorSpace)
+{
+ return (ColorSpace == cmsSigXYZData ||
+ ColorSpace == cmsSigLabData);
+}
+
+
+static
+void FixColorSpaces(cmsHPROFILE hProfile,
+ cmsColorSpaceSignature ColorSpace,
+ cmsColorSpaceSignature PCS,
+ cmsUInt32Number dwFlags)
+{
+ if (dwFlags & cmsFLAGS_GUESSDEVICECLASS) {
+
+ if (IsPCS(ColorSpace) && IsPCS(PCS)) {
+
+ cmsSetDeviceClass(hProfile, cmsSigAbstractClass);
+ cmsSetColorSpace(hProfile, ColorSpace);
+ cmsSetPCS(hProfile, PCS);
+ return;
+ }
+
+ if (IsPCS(ColorSpace) && !IsPCS(PCS)) {
+
+ cmsSetDeviceClass(hProfile, cmsSigOutputClass);
+ cmsSetPCS(hProfile, ColorSpace);
+ cmsSetColorSpace(hProfile, PCS);
+ return;
+ }
+
+ if (IsPCS(PCS) && !IsPCS(ColorSpace)) {
+
+ cmsSetDeviceClass(hProfile, cmsSigInputClass);
+ cmsSetColorSpace(hProfile, ColorSpace);
+ cmsSetPCS(hProfile, PCS);
+ return;
+ }
+ }
+
+ cmsSetDeviceClass(hProfile, cmsSigLinkClass);
+ cmsSetColorSpace(hProfile, ColorSpace);
+ cmsSetPCS(hProfile, PCS);
+}
+
+
+
+// This function creates a named color profile dumping all the contents of transform to a single profile
+// In this way, LittleCMS may be used to "group" several named color databases into a single profile.
+// It has, however, several minor limitations. PCS is always Lab, which is not very critic since this
+// is the normal PCS for named color profiles.
+static
+cmsHPROFILE CreateNamedColorDevicelink(cmsHTRANSFORM xform)
+{
+ _cmsTRANSFORM* v = (_cmsTRANSFORM*) xform;
+ cmsHPROFILE hICC = NULL;
+ int i, nColors;
+ cmsNAMEDCOLORLIST *nc2 = NULL, *Original = NULL;
+
+ // Create an empty placeholder
+ hICC = cmsCreateProfilePlaceholder(v->ContextID);
+ if (hICC == NULL) return NULL;
+
+ // Critical information
+ cmsSetDeviceClass(hICC, cmsSigNamedColorClass);
+ cmsSetColorSpace(hICC, v ->ExitColorSpace);
+ cmsSetPCS(hICC, cmsSigLabData);
+
+ // Tag profile with information
+ if (!SetTextTags(hICC, L"Named color devicelink")) goto Error;
+
+ Original = cmsGetNamedColorList(xform);
+ if (Original == NULL) goto Error;
+
+ nColors = cmsNamedColorCount(Original);
+ nc2 = cmsDupNamedColorList(Original);
+ if (nc2 == NULL) goto Error;
+
+ // Colorant count now depends on the output space
+ nc2 ->ColorantCount = cmsPipelineOutputChannels(v ->Lut);
+
+ // Make sure we have proper formatters
+ cmsChangeBuffersFormat(xform, TYPE_NAMED_COLOR_INDEX,
+ FLOAT_SH(0) | COLORSPACE_SH(_cmsLCMScolorSpace(v ->ExitColorSpace))
+ | BYTES_SH(2) | CHANNELS_SH(cmsChannelsOf(v ->ExitColorSpace)));
+
+ // Apply the transfor to colorants.
+ for (i=0; i < nColors; i++) {
+ cmsDoTransform(xform, &i, nc2 ->List[i].DeviceColorant, 1);
+ }
+
+ if (!cmsWriteTag(hICC, cmsSigNamedColor2Tag, (void*) nc2)) goto Error;
+ cmsFreeNamedColorList(nc2);
+
+ return hICC;
+
+Error:
+ if (hICC != NULL) cmsCloseProfile(hICC);
+ return NULL;
+}
+
+
+// This structure holds information about which MPU can be stored on a profile based on the version
+
+typedef struct {
+ cmsBool IsV4; // Is a V4 tag?
+ cmsTagSignature RequiredTag; // Set to 0 for both types
+ cmsTagTypeSignature LutType; // The LUT type
+ int nTypes; // Number of types (up to 5)
+ cmsStageSignature MpeTypes[5]; // 5 is the maximum number
+
+} cmsAllowedLUT;
+
+static const cmsAllowedLUT AllowedLUTTypes[] = {
+
+ { FALSE, 0, cmsSigLut16Type, 4, { cmsSigMatrixElemType, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType}},
+ { FALSE, 0, cmsSigLut16Type, 3, { cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType}},
+ { FALSE, 0, cmsSigLut16Type, 2, { cmsSigCurveSetElemType, cmsSigCLutElemType}},
+ { TRUE , 0, cmsSigLutAtoBType, 1, { cmsSigCurveSetElemType }},
+ { TRUE , cmsSigAToB0Tag, cmsSigLutAtoBType, 3, { cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType } },
+ { TRUE , cmsSigAToB0Tag, cmsSigLutAtoBType, 3, { cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType } },
+ { TRUE , cmsSigAToB0Tag, cmsSigLutAtoBType, 5, { cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType }},
+ { TRUE , cmsSigBToA0Tag, cmsSigLutBtoAType, 1, { cmsSigCurveSetElemType }},
+ { TRUE , cmsSigBToA0Tag, cmsSigLutBtoAType, 3, { cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType }},
+ { TRUE , cmsSigBToA0Tag, cmsSigLutBtoAType, 3, { cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType }},
+ { TRUE , cmsSigBToA0Tag, cmsSigLutBtoAType, 5, { cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType }}
+};
+
+#define SIZE_OF_ALLOWED_LUT (sizeof(AllowedLUTTypes)/sizeof(cmsAllowedLUT))
+
+// Check a single entry
+static
+cmsBool CheckOne(const cmsAllowedLUT* Tab, const cmsPipeline* Lut)
+{
+ cmsStage* mpe;
+ int n;
+
+ for (n=0, mpe = Lut ->Elements; mpe != NULL; mpe = mpe ->Next, n++) {
+
+ if (n > Tab ->nTypes) return FALSE;
+ if (cmsStageType(mpe) != Tab ->MpeTypes[n]) return FALSE;
+ }
+
+ return (n == Tab ->nTypes);
+}
+
+
+static
+const cmsAllowedLUT* FindCombination(const cmsPipeline* Lut, cmsBool IsV4, cmsTagSignature DestinationTag)
+{
+ cmsUInt32Number n;
+
+ for (n=0; n < SIZE_OF_ALLOWED_LUT; n++) {
+
+ const cmsAllowedLUT* Tab = AllowedLUTTypes + n;
+
+ if (IsV4 ^ Tab -> IsV4) continue;
+ if ((Tab ->RequiredTag != 0) && (Tab ->RequiredTag != DestinationTag)) continue;
+
+ if (CheckOne(Tab, Lut)) return Tab;
+ }
+
+ return NULL;
+}
+
+
+// Does convert a transform into a device link profile
+cmsHPROFILE CMSEXPORT cmsTransform2DeviceLink(cmsHTRANSFORM hTransform, cmsFloat64Number Version, cmsUInt32Number dwFlags)
+{
+ cmsHPROFILE hProfile = NULL;
+ cmsUInt32Number FrmIn, FrmOut, ChansIn, ChansOut;
+ cmsUInt32Number ColorSpaceBitsIn, ColorSpaceBitsOut;
+ _cmsTRANSFORM* xform = (_cmsTRANSFORM*) hTransform;
+ cmsPipeline* LUT = NULL;
+ cmsStage* mpe;
+ cmsContext ContextID = cmsGetTransformContextID(hTransform);
+ const cmsAllowedLUT* AllowedLUT;
+ cmsTagSignature DestinationTag;
+ cmsProfileClassSignature deviceClass;
+
+ _cmsAssert(hTransform != NULL);
+
+ // Get the first mpe to check for named color
+ mpe = cmsPipelineGetPtrToFirstStage(xform ->Lut);
+
+ // Check if is a named color transform
+ if (mpe != NULL) {
+
+ if (cmsStageType(mpe) == cmsSigNamedColorElemType) {
+ return CreateNamedColorDevicelink(hTransform);
+ }
+ }
+
+ // First thing to do is to get a copy of the transformation
+ LUT = cmsPipelineDup(xform ->Lut);
+ if (LUT == NULL) return NULL;
+
+ // Time to fix the Lab2/Lab4 issue.
+ if ((xform ->EntryColorSpace == cmsSigLabData) && (Version < 4.0)) {
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocLabV2ToV4curves(ContextID)))
+ goto Error;
+ }
+
+ // On the output side too
+ if ((xform ->ExitColorSpace) == cmsSigLabData && (Version < 4.0)) {
+
+ if (!cmsPipelineInsertStage(LUT, cmsAT_END, _cmsStageAllocLabV4ToV2(ContextID)))
+ goto Error;
+ }
+
+
+ hProfile = cmsCreateProfilePlaceholder(ContextID);
+ if (!hProfile) goto Error; // can't allocate
+
+ cmsSetProfileVersion(hProfile, Version);
+
+ FixColorSpaces(hProfile, xform -> EntryColorSpace, xform -> ExitColorSpace, dwFlags);
+
+ // Optimize the LUT and precalculate a devicelink
+
+ ChansIn = cmsChannelsOf(xform -> EntryColorSpace);
+ ChansOut = cmsChannelsOf(xform -> ExitColorSpace);
+
+ ColorSpaceBitsIn = _cmsLCMScolorSpace(xform -> EntryColorSpace);
+ ColorSpaceBitsOut = _cmsLCMScolorSpace(xform -> ExitColorSpace);
+
+ FrmIn = COLORSPACE_SH(ColorSpaceBitsIn) | CHANNELS_SH(ChansIn)|BYTES_SH(2);
+ FrmOut = COLORSPACE_SH(ColorSpaceBitsOut) | CHANNELS_SH(ChansOut)|BYTES_SH(2);
+
+ deviceClass = cmsGetDeviceClass(hProfile);
+
+ if (deviceClass == cmsSigOutputClass)
+ DestinationTag = cmsSigBToA0Tag;
+ else
+ DestinationTag = cmsSigAToB0Tag;
+
+ // Check if the profile/version can store the result
+ if (dwFlags & cmsFLAGS_FORCE_CLUT)
+ AllowedLUT = NULL;
+ else
+ AllowedLUT = FindCombination(LUT, Version >= 4.0, DestinationTag);
+
+ if (AllowedLUT == NULL) {
+
+ // Try to optimize
+ _cmsOptimizePipeline(ContextID, &LUT, xform ->RenderingIntent, &FrmIn, &FrmOut, &dwFlags);
+ AllowedLUT = FindCombination(LUT, Version >= 4.0, DestinationTag);
+
+ }
+
+ // If no way, then force CLUT that for sure can be written
+ if (AllowedLUT == NULL) {
+
+ dwFlags |= cmsFLAGS_FORCE_CLUT;
+ _cmsOptimizePipeline(ContextID, &LUT, xform ->RenderingIntent, &FrmIn, &FrmOut, &dwFlags);
+
+ // Put identity curves if needed
+ if (cmsPipelineGetPtrToFirstStage(LUT) ->Type != cmsSigCurveSetElemType)
+ if (!cmsPipelineInsertStage(LUT, cmsAT_BEGIN, _cmsStageAllocIdentityCurves(ContextID, ChansIn)))
+ goto Error;
+
+ if (cmsPipelineGetPtrToLastStage(LUT) ->Type != cmsSigCurveSetElemType)
+ if (!cmsPipelineInsertStage(LUT, cmsAT_END, _cmsStageAllocIdentityCurves(ContextID, ChansOut)))
+ goto Error;
+
+ AllowedLUT = FindCombination(LUT, Version >= 4.0, DestinationTag);
+ }
+
+ // Somethings is wrong...
+ if (AllowedLUT == NULL) {
+ goto Error;
+ }
+
+
+ if (dwFlags & cmsFLAGS_8BITS_DEVICELINK)
+ cmsPipelineSetSaveAs8bitsFlag(LUT, TRUE);
+
+ // Tag profile with information
+ if (!SetTextTags(hProfile, L"devicelink")) goto Error;
+
+ // Store result
+ if (!cmsWriteTag(hProfile, DestinationTag, LUT)) goto Error;
+
+
+ if (xform -> InputColorant != NULL) {
+ if (!cmsWriteTag(hProfile, cmsSigColorantTableTag, xform->InputColorant)) goto Error;
+ }
+
+ if (xform -> OutputColorant != NULL) {
+ if (!cmsWriteTag(hProfile, cmsSigColorantTableOutTag, xform->OutputColorant)) goto Error;
+ }
+
+ if ((deviceClass == cmsSigLinkClass) && (xform ->Sequence != NULL)) {
+ if (!_cmsWriteProfileSequence(hProfile, xform ->Sequence)) goto Error;
+ }
+
+ // Set the white point
+ if (deviceClass == cmsSigInputClass) {
+ if (!cmsWriteTag(hProfile, cmsSigMediaWhitePointTag, &xform ->EntryWhitePoint)) goto Error;
+ }
+ else {
+ if (!cmsWriteTag(hProfile, cmsSigMediaWhitePointTag, &xform ->ExitWhitePoint)) goto Error;
+ }
+
+
+ // Per 7.2.15 in spec 4.3
+ cmsSetHeaderRenderingIntent(hProfile, xform ->RenderingIntent);
+
+ cmsPipelineFree(LUT);
+ return hProfile;
+
+Error:
+ if (LUT != NULL) cmsPipelineFree(LUT);
+ cmsCloseProfile(hProfile);
+ return NULL;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmswtpnt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmswtpnt.c
new file mode 100644
index 0000000000..903fdd7497
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmswtpnt.c
@@ -0,0 +1,349 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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"
+
+
+// D50 - Widely used
+const cmsCIEXYZ* CMSEXPORT cmsD50_XYZ(void)
+{
+ static cmsCIEXYZ D50XYZ = {cmsD50X, cmsD50Y, cmsD50Z};
+
+ return &D50XYZ;
+}
+
+const cmsCIExyY* CMSEXPORT cmsD50_xyY(void)
+{
+ static cmsCIExyY D50xyY;
+
+ cmsXYZ2xyY(&D50xyY, cmsD50_XYZ());
+
+ return &D50xyY;
+}
+
+// Obtains WhitePoint from Temperature
+cmsBool CMSEXPORT cmsWhitePointFromTemp(cmsCIExyY* WhitePoint, cmsFloat64Number TempK)
+{
+ cmsFloat64Number x, y;
+ cmsFloat64Number T, T2, T3;
+ // cmsFloat64Number M1, M2;
+
+ _cmsAssert(WhitePoint != NULL);
+
+ T = TempK;
+ T2 = T*T; // Square
+ T3 = T2*T; // Cube
+
+ // For correlated color temperature (T) between 4000K and 7000K:
+
+ if (T >= 4000. && T <= 7000.)
+ {
+ x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
+ }
+ else
+ // or for correlated color temperature (T) between 7000K and 25000K:
+
+ if (T > 7000.0 && T <= 25000.0)
+ {
+ x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
+ }
+ else {
+ cmsSignalError(0, cmsERROR_RANGE, "cmsWhitePointFromTemp: invalid temp");
+ return FALSE;
+ }
+
+ // Obtain y(x)
+
+ y = -3.000*(x*x) + 2.870*x - 0.275;
+
+ // wave factors (not used, but here for futures extensions)
+
+ // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
+ // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
+
+ WhitePoint -> x = x;
+ WhitePoint -> y = y;
+ WhitePoint -> Y = 1.0;
+
+ return TRUE;
+}
+
+
+
+typedef struct {
+
+ cmsFloat64Number mirek; // temp (in microreciprocal kelvin)
+ cmsFloat64Number ut; // u coord of intersection w/ blackbody locus
+ cmsFloat64Number vt; // v coord of intersection w/ blackbody locus
+ cmsFloat64Number tt; // slope of ISOTEMPERATURE. line
+
+ } ISOTEMPERATURE;
+
+static ISOTEMPERATURE isotempdata[] = {
+// {Mirek, Ut, Vt, Tt }
+ {0, 0.18006, 0.26352, -0.24341},
+ {10, 0.18066, 0.26589, -0.25479},
+ {20, 0.18133, 0.26846, -0.26876},
+ {30, 0.18208, 0.27119, -0.28539},
+ {40, 0.18293, 0.27407, -0.30470},
+ {50, 0.18388, 0.27709, -0.32675},
+ {60, 0.18494, 0.28021, -0.35156},
+ {70, 0.18611, 0.28342, -0.37915},
+ {80, 0.18740, 0.28668, -0.40955},
+ {90, 0.18880, 0.28997, -0.44278},
+ {100, 0.19032, 0.29326, -0.47888},
+ {125, 0.19462, 0.30141, -0.58204},
+ {150, 0.19962, 0.30921, -0.70471},
+ {175, 0.20525, 0.31647, -0.84901},
+ {200, 0.21142, 0.32312, -1.0182 },
+ {225, 0.21807, 0.32909, -1.2168 },
+ {250, 0.22511, 0.33439, -1.4512 },
+ {275, 0.23247, 0.33904, -1.7298 },
+ {300, 0.24010, 0.34308, -2.0637 },
+ {325, 0.24702, 0.34655, -2.4681 },
+ {350, 0.25591, 0.34951, -2.9641 },
+ {375, 0.26400, 0.35200, -3.5814 },
+ {400, 0.27218, 0.35407, -4.3633 },
+ {425, 0.28039, 0.35577, -5.3762 },
+ {450, 0.28863, 0.35714, -6.7262 },
+ {475, 0.29685, 0.35823, -8.5955 },
+ {500, 0.30505, 0.35907, -11.324 },
+ {525, 0.31320, 0.35968, -15.628 },
+ {550, 0.32129, 0.36011, -23.325 },
+ {575, 0.32931, 0.36038, -40.770 },
+ {600, 0.33724, 0.36051, -116.45 }
+};
+
+#define NISO sizeof(isotempdata)/sizeof(ISOTEMPERATURE)
+
+
+// Robertson's method
+cmsBool CMSEXPORT cmsTempFromWhitePoint(cmsFloat64Number* TempK, const cmsCIExyY* WhitePoint)
+{
+ cmsUInt32Number j;
+ cmsFloat64Number us,vs;
+ cmsFloat64Number uj,vj,tj,di,dj,mi,mj;
+ cmsFloat64Number xs, ys;
+
+ _cmsAssert(WhitePoint != NULL);
+ _cmsAssert(TempK != NULL);
+
+ di = mi = 0;
+ xs = WhitePoint -> x;
+ ys = WhitePoint -> y;
+
+ // convert (x,y) to CIE 1960 (u,WhitePoint)
+
+ us = (2*xs) / (-xs + 6*ys + 1.5);
+ vs = (3*ys) / (-xs + 6*ys + 1.5);
+
+
+ for (j=0; j < NISO; j++) {
+
+ uj = isotempdata[j].ut;
+ vj = isotempdata[j].vt;
+ tj = isotempdata[j].tt;
+ mj = isotempdata[j].mirek;
+
+ dj = ((vs - vj) - tj * (us - uj)) / sqrt(1.0 + tj * tj);
+
+ if ((j != 0) && (di/dj < 0.0)) {
+
+ // Found a match
+ *TempK = 1000000.0 / (mi + (di / (di - dj)) * (mj - mi));
+ return TRUE;
+ }
+
+ di = dj;
+ mi = mj;
+ }
+
+ // Not found
+ return FALSE;
+}
+
+
+// Compute chromatic adaptation matrix using Chad as cone matrix
+
+static
+cmsBool ComputeChromaticAdaptation(cmsMAT3* Conversion,
+ const cmsCIEXYZ* SourceWhitePoint,
+ const cmsCIEXYZ* DestWhitePoint,
+ const cmsMAT3* Chad)
+
+{
+
+ cmsMAT3 Chad_Inv;
+ cmsVEC3 ConeSourceXYZ, ConeSourceRGB;
+ cmsVEC3 ConeDestXYZ, ConeDestRGB;
+ cmsMAT3 Cone, Tmp;
+
+
+ Tmp = *Chad;
+ if (!_cmsMAT3inverse(&Tmp, &Chad_Inv)) return FALSE;
+
+ _cmsVEC3init(&ConeSourceXYZ, SourceWhitePoint -> X,
+ SourceWhitePoint -> Y,
+ SourceWhitePoint -> Z);
+
+ _cmsVEC3init(&ConeDestXYZ, DestWhitePoint -> X,
+ DestWhitePoint -> Y,
+ DestWhitePoint -> Z);
+
+ _cmsMAT3eval(&ConeSourceRGB, Chad, &ConeSourceXYZ);
+ _cmsMAT3eval(&ConeDestRGB, Chad, &ConeDestXYZ);
+
+ // Build matrix
+ _cmsVEC3init(&Cone.v[0], ConeDestRGB.n[0]/ConeSourceRGB.n[0], 0.0, 0.0);
+ _cmsVEC3init(&Cone.v[1], 0.0, ConeDestRGB.n[1]/ConeSourceRGB.n[1], 0.0);
+ _cmsVEC3init(&Cone.v[2], 0.0, 0.0, ConeDestRGB.n[2]/ConeSourceRGB.n[2]);
+
+
+ // Normalize
+ _cmsMAT3per(&Tmp, &Cone, Chad);
+ _cmsMAT3per(Conversion, &Chad_Inv, &Tmp);
+
+ return TRUE;
+}
+
+// Returns the final chrmatic adaptation from illuminant FromIll to Illuminant ToIll
+// The cone matrix can be specified in ConeMatrix. If NULL, Bradford is assumed
+cmsBool _cmsAdaptationMatrix(cmsMAT3* r, const cmsMAT3* ConeMatrix, const cmsCIEXYZ* FromIll, const cmsCIEXYZ* ToIll)
+{
+ cmsMAT3 LamRigg = {{ // Bradford matrix
+ {{ 0.8951, 0.2664, -0.1614 }},
+ {{ -0.7502, 1.7135, 0.0367 }},
+ {{ 0.0389, -0.0685, 1.0296 }}
+ }};
+
+ if (ConeMatrix == NULL)
+ ConeMatrix = &LamRigg;
+
+ return ComputeChromaticAdaptation(r, FromIll, ToIll, ConeMatrix);
+}
+
+// Same as anterior, but assuming D50 destination. White point is given in xyY
+static
+cmsBool _cmsAdaptMatrixToD50(cmsMAT3* r, const cmsCIExyY* SourceWhitePt)
+{
+ cmsCIEXYZ Dn;
+ cmsMAT3 Bradford;
+ cmsMAT3 Tmp;
+
+ cmsxyY2XYZ(&Dn, SourceWhitePt);
+
+ if (!_cmsAdaptationMatrix(&Bradford, NULL, &Dn, cmsD50_XYZ())) return FALSE;
+
+ Tmp = *r;
+ _cmsMAT3per(r, &Bradford, &Tmp);
+
+ return TRUE;
+}
+
+// Build a White point, primary chromas transfer matrix from RGB to CIE XYZ
+// This is just an approximation, I am not handling all the non-linear
+// aspects of the RGB to XYZ process, and assumming that the gamma correction
+// has transitive property in the tranformation chain.
+//
+// the alghoritm:
+//
+// - First I build the absolute conversion matrix using
+// primaries in XYZ. This matrix is next inverted
+// - Then I eval the source white point across this matrix
+// obtaining the coeficients of the transformation
+// - Then, I apply these coeficients to the original matrix
+//
+cmsBool _cmsBuildRGB2XYZtransferMatrix(cmsMAT3* r, const cmsCIExyY* WhitePt, const cmsCIExyYTRIPLE* Primrs)
+{
+ cmsVEC3 WhitePoint, Coef;
+ cmsMAT3 Result, Primaries;
+ cmsFloat64Number xn, yn;
+ cmsFloat64Number xr, yr;
+ cmsFloat64Number xg, yg;
+ cmsFloat64Number xb, yb;
+
+ xn = WhitePt -> x;
+ yn = WhitePt -> y;
+ xr = Primrs -> Red.x;
+ yr = Primrs -> Red.y;
+ xg = Primrs -> Green.x;
+ yg = Primrs -> Green.y;
+ xb = Primrs -> Blue.x;
+ yb = Primrs -> Blue.y;
+
+ // Build Primaries matrix
+ _cmsVEC3init(&Primaries.v[0], xr, xg, xb);
+ _cmsVEC3init(&Primaries.v[1], yr, yg, yb);
+ _cmsVEC3init(&Primaries.v[2], (1-xr-yr), (1-xg-yg), (1-xb-yb));
+
+
+ // Result = Primaries ^ (-1) inverse matrix
+ if (!_cmsMAT3inverse(&Primaries, &Result))
+ return FALSE;
+
+
+ _cmsVEC3init(&WhitePoint, xn/yn, 1.0, (1.0-xn-yn)/yn);
+
+ // Across inverse primaries ...
+ _cmsMAT3eval(&Coef, &Result, &WhitePoint);
+
+ // Give us the Coefs, then I build transformation matrix
+ _cmsVEC3init(&r -> v[0], Coef.n[VX]*xr, Coef.n[VY]*xg, Coef.n[VZ]*xb);
+ _cmsVEC3init(&r -> v[1], Coef.n[VX]*yr, Coef.n[VY]*yg, Coef.n[VZ]*yb);
+ _cmsVEC3init(&r -> v[2], Coef.n[VX]*(1.0-xr-yr), Coef.n[VY]*(1.0-xg-yg), Coef.n[VZ]*(1.0-xb-yb));
+
+
+ return _cmsAdaptMatrixToD50(r, WhitePt);
+
+}
+
+
+// Adapts a color to a given illuminant. Original color is expected to have
+// a SourceWhitePt white point.
+cmsBool CMSEXPORT cmsAdaptToIlluminant(cmsCIEXYZ* Result,
+ const cmsCIEXYZ* SourceWhitePt,
+ const cmsCIEXYZ* Illuminant,
+ const cmsCIEXYZ* Value)
+{
+ cmsMAT3 Bradford;
+ cmsVEC3 In, Out;
+
+ _cmsAssert(Result != NULL);
+ _cmsAssert(SourceWhitePt != NULL);
+ _cmsAssert(Illuminant != NULL);
+ _cmsAssert(Value != NULL);
+
+ if (!_cmsAdaptationMatrix(&Bradford, NULL, SourceWhitePt, Illuminant)) return FALSE;
+
+ _cmsVEC3init(&In, Value -> X, Value -> Y, Value -> Z);
+ _cmsMAT3eval(&Out, &Bradford, &In);
+
+ Result -> X = Out.n[0];
+ Result -> Y = Out.n[1];
+ Result -> Z = Out.n[2];
+
+ return TRUE;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsxform.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsxform.c
new file mode 100644
index 0000000000..eddb9bd02c
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsxform.c
@@ -0,0 +1,1132 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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"
+
+// Transformations stuff
+// -----------------------------------------------------------------------
+
+#define DEFAULT_OBSERVER_ADAPTATION_STATE 1.0
+
+// The Context0 observer adaptation state.
+_cmsAdaptationStateChunkType _cmsAdaptationStateChunk = { DEFAULT_OBSERVER_ADAPTATION_STATE };
+
+// Init and duplicate observer adaptation state
+void _cmsAllocAdaptationStateChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ static _cmsAdaptationStateChunkType AdaptationStateChunk = { DEFAULT_OBSERVER_ADAPTATION_STATE };
+ void* from;
+
+ if (src != NULL) {
+ from = src ->chunks[AdaptationStateContext];
+ }
+ else {
+ from = &AdaptationStateChunk;
+ }
+
+ ctx ->chunks[AdaptationStateContext] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsAdaptationStateChunkType));
+}
+
+
+// Sets adaptation state for absolute colorimetric intent in the given context. Adaptation state applies on all
+// but cmsCreateExtendedTransformTHR(). Little CMS can handle incomplete adaptation states.
+cmsFloat64Number CMSEXPORT cmsSetAdaptationStateTHR(cmsContext ContextID, cmsFloat64Number d)
+{
+ cmsFloat64Number prev;
+ _cmsAdaptationStateChunkType* ptr = (_cmsAdaptationStateChunkType*) _cmsContextGetClientChunk(ContextID, AdaptationStateContext);
+
+ // Get previous value for return
+ prev = ptr ->AdaptationState;
+
+ // Set the value if d is positive or zero
+ if (d >= 0.0) {
+
+ ptr ->AdaptationState = d;
+ }
+
+ // Always return previous value
+ return prev;
+}
+
+
+// The adaptation state may be defaulted by this function. If you don't like it, use the extended transform routine
+cmsFloat64Number CMSEXPORT cmsSetAdaptationState(cmsFloat64Number d)
+{
+ return cmsSetAdaptationStateTHR(NULL, d);
+}
+
+// -----------------------------------------------------------------------
+
+// Alarm codes for 16-bit transformations, because the fixed range of containers there are
+// no values left to mark out of gamut.
+
+#define DEFAULT_ALARM_CODES_VALUE {0x7F00, 0x7F00, 0x7F00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
+
+_cmsAlarmCodesChunkType _cmsAlarmCodesChunk = { DEFAULT_ALARM_CODES_VALUE };
+
+// Sets the codes used to mark out-out-gamut on Proofing transforms for a given context. Values are meant to be
+// encoded in 16 bits.
+void CMSEXPORT cmsSetAlarmCodesTHR(cmsContext ContextID, const cmsUInt16Number AlarmCodesP[cmsMAXCHANNELS])
+{
+ _cmsAlarmCodesChunkType* ContextAlarmCodes = (_cmsAlarmCodesChunkType*) _cmsContextGetClientChunk(ContextID, AlarmCodesContext);
+
+ _cmsAssert(ContextAlarmCodes != NULL); // Can't happen
+
+ memcpy(ContextAlarmCodes->AlarmCodes, AlarmCodesP, sizeof(ContextAlarmCodes->AlarmCodes));
+}
+
+// Gets the current codes used to mark out-out-gamut on Proofing transforms for the given context.
+// Values are meant to be encoded in 16 bits.
+void CMSEXPORT cmsGetAlarmCodesTHR(cmsContext ContextID, cmsUInt16Number AlarmCodesP[cmsMAXCHANNELS])
+{
+ _cmsAlarmCodesChunkType* ContextAlarmCodes = (_cmsAlarmCodesChunkType*) _cmsContextGetClientChunk(ContextID, AlarmCodesContext);
+
+ _cmsAssert(ContextAlarmCodes != NULL); // Can't happen
+
+ memcpy(AlarmCodesP, ContextAlarmCodes->AlarmCodes, sizeof(ContextAlarmCodes->AlarmCodes));
+}
+
+void CMSEXPORT cmsSetAlarmCodes(const cmsUInt16Number NewAlarm[cmsMAXCHANNELS])
+{
+ _cmsAssert(NewAlarm != NULL);
+
+ cmsSetAlarmCodesTHR(NULL, NewAlarm);
+}
+
+void CMSEXPORT cmsGetAlarmCodes(cmsUInt16Number OldAlarm[cmsMAXCHANNELS])
+{
+ _cmsAssert(OldAlarm != NULL);
+ cmsGetAlarmCodesTHR(NULL, OldAlarm);
+}
+
+
+// Init and duplicate alarm codes
+void _cmsAllocAlarmCodesChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ static _cmsAlarmCodesChunkType AlarmCodesChunk = { DEFAULT_ALARM_CODES_VALUE };
+ void* from;
+
+ if (src != NULL) {
+ from = src ->chunks[AlarmCodesContext];
+ }
+ else {
+ from = &AlarmCodesChunk;
+ }
+
+ ctx ->chunks[AlarmCodesContext] = _cmsSubAllocDup(ctx ->MemPool, from, sizeof(_cmsAlarmCodesChunkType));
+}
+
+// -----------------------------------------------------------------------
+
+// Get rid of transform resources
+void CMSEXPORT cmsDeleteTransform(cmsHTRANSFORM hTransform)
+{
+ _cmsTRANSFORM* p = (_cmsTRANSFORM*) hTransform;
+
+ _cmsAssert(p != NULL);
+
+ if (p -> GamutCheck)
+ cmsPipelineFree(p -> GamutCheck);
+
+ if (p -> Lut)
+ cmsPipelineFree(p -> Lut);
+
+ if (p ->InputColorant)
+ cmsFreeNamedColorList(p ->InputColorant);
+
+ if (p -> OutputColorant)
+ cmsFreeNamedColorList(p ->OutputColorant);
+
+ if (p ->Sequence)
+ cmsFreeProfileSequenceDescription(p ->Sequence);
+
+ if (p ->UserData)
+ p ->FreeUserData(p ->ContextID, p ->UserData);
+
+ _cmsFree(p ->ContextID, (void *) p);
+}
+
+// Apply transform.
+void CMSEXPORT cmsDoTransform(cmsHTRANSFORM Transform,
+ const void* InputBuffer,
+ void* OutputBuffer,
+ cmsUInt32Number Size)
+
+{
+ _cmsTRANSFORM* p = (_cmsTRANSFORM*) Transform;
+
+ p -> xform(p, InputBuffer, OutputBuffer, Size, Size);
+}
+
+
+// Apply transform.
+void CMSEXPORT cmsDoTransformStride(cmsHTRANSFORM Transform,
+ const void* InputBuffer,
+ void* OutputBuffer,
+ cmsUInt32Number Size, cmsUInt32Number Stride)
+
+{
+ _cmsTRANSFORM* p = (_cmsTRANSFORM*) Transform;
+
+ p -> xform(p, InputBuffer, OutputBuffer, Size, Stride);
+}
+
+
+// Transform routines ----------------------------------------------------------------------------------------------------------
+
+// Float xform converts floats. Since there are no performance issues, one routine does all job, including gamut check.
+// Note that because extended range, we can use a -1.0 value for out of gamut in this case.
+static
+void FloatXFORM(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size, cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsFloat32Number fIn[cmsMAXCHANNELS], fOut[cmsMAXCHANNELS];
+ cmsFloat32Number OutOfGamut;
+ cmsUInt32Number i, j;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+
+ for (i=0; i < Size; i++) {
+
+ accum = p -> FromInputFloat(p, fIn, accum, Stride);
+
+ // Any gamut chack to do?
+ if (p ->GamutCheck != NULL) {
+
+ // Evaluate gamut marker.
+ cmsPipelineEvalFloat( fIn, &OutOfGamut, p ->GamutCheck);
+
+ // Is current color out of gamut?
+ if (OutOfGamut > 0.0) {
+
+ // Certainly, out of gamut
+ for (j=0; j < cmsMAXCHANNELS; j++)
+ fOut[j] = -1.0;
+
+ }
+ else {
+ // No, proceed normally
+ cmsPipelineEvalFloat(fIn, fOut, p -> Lut);
+ }
+ }
+ else {
+
+ // No gamut check at all
+ cmsPipelineEvalFloat(fIn, fOut, p -> Lut);
+ }
+
+ // Back to asked representation
+ output = p -> ToOutputFloat(p, fOut, output, Stride);
+ }
+}
+
+
+static
+void NullFloatXFORM(_cmsTRANSFORM* p,
+ const void* in,
+ void* out,
+ cmsUInt32Number Size,
+ cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsFloat32Number fIn[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size;
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInputFloat(p, fIn, accum, Stride);
+ output = p -> ToOutputFloat(p, fIn, output, Stride);
+ }
+}
+
+// 16 bit precision -----------------------------------------------------------------------------------------------------------
+
+// Null transformation, only applies formatters. No cach?static
+void NullXFORM(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size,
+ cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsUInt16Number wIn[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size; // Buffer len
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInput(p, wIn, accum, Stride);
+ output = p -> ToOutput(p, wIn, output, Stride);
+ }
+}
+
+
+// No gamut check, no cache, 16 bits
+static
+void PrecalculatedXFORM(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size, cmsUInt32Number Stride)
+{
+ register cmsUInt8Number* accum;
+ register cmsUInt8Number* output;
+ cmsUInt16Number wIn[cmsMAXCHANNELS], wOut[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size;
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInput(p, wIn, accum, Stride);
+ p ->Lut ->Eval16Fn(wIn, wOut, p -> Lut->Data);
+ output = p -> ToOutput(p, wOut, output, Stride);
+ }
+}
+
+
+// Auxiliar: Handle precalculated gamut check. The retrieval of context may be alittle bit slow, but this function is not critical.
+static
+void TransformOnePixelWithGamutCheck(_cmsTRANSFORM* p,
+ const cmsUInt16Number wIn[],
+ cmsUInt16Number wOut[])
+{
+ cmsUInt16Number wOutOfGamut;
+
+ p ->GamutCheck ->Eval16Fn(wIn, &wOutOfGamut, p ->GamutCheck ->Data);
+ if (wOutOfGamut >= 1) {
+
+ cmsUInt16Number i;
+ _cmsAlarmCodesChunkType* ContextAlarmCodes = (_cmsAlarmCodesChunkType*) _cmsContextGetClientChunk(p->ContextID, AlarmCodesContext);
+
+ for (i=0; i < p ->Lut->OutputChannels; i++) {
+
+ wOut[i] = ContextAlarmCodes ->AlarmCodes[i];
+ }
+ }
+ else
+ p ->Lut ->Eval16Fn(wIn, wOut, p -> Lut->Data);
+}
+
+// Gamut check, No cach? 16 bits.
+static
+void PrecalculatedXFORMGamutCheck(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size, cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsUInt16Number wIn[cmsMAXCHANNELS], wOut[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size; // Buffer len
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInput(p, wIn, accum, Stride);
+ TransformOnePixelWithGamutCheck(p, wIn, wOut);
+ output = p -> ToOutput(p, wOut, output, Stride);
+ }
+}
+
+
+// No gamut check, Cach? 16 bits,
+static
+void CachedXFORM(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size, cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsUInt16Number wIn[cmsMAXCHANNELS], wOut[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+ _cmsCACHE Cache;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size; // Buffer len
+
+ // Empty buffers for quick memcmp
+ memset(wIn, 0, sizeof(wIn));
+ memset(wOut, 0, sizeof(wOut));
+
+ // Get copy of zero cache
+ memcpy(&Cache, &p ->Cache, sizeof(Cache));
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInput(p, wIn, accum, Stride);
+
+ if (memcmp(wIn, Cache.CacheIn, sizeof(Cache.CacheIn)) == 0) {
+
+ memcpy(wOut, Cache.CacheOut, sizeof(Cache.CacheOut));
+ }
+ else {
+
+ p ->Lut ->Eval16Fn(wIn, wOut, p -> Lut->Data);
+
+ memcpy(Cache.CacheIn, wIn, sizeof(Cache.CacheIn));
+ memcpy(Cache.CacheOut, wOut, sizeof(Cache.CacheOut));
+ }
+
+ output = p -> ToOutput(p, wOut, output, Stride);
+ }
+
+}
+
+
+// All those nice features together
+static
+void CachedXFORMGamutCheck(_cmsTRANSFORM* p,
+ const void* in,
+ void* out, cmsUInt32Number Size, cmsUInt32Number Stride)
+{
+ cmsUInt8Number* accum;
+ cmsUInt8Number* output;
+ cmsUInt16Number wIn[cmsMAXCHANNELS], wOut[cmsMAXCHANNELS];
+ cmsUInt32Number i, n;
+ _cmsCACHE Cache;
+
+ accum = (cmsUInt8Number*) in;
+ output = (cmsUInt8Number*) out;
+ n = Size; // Buffer len
+
+ // Empty buffers for quick memcmp
+ memset(wIn, 0, sizeof(cmsUInt16Number) * cmsMAXCHANNELS);
+ memset(wOut, 0, sizeof(cmsUInt16Number) * cmsMAXCHANNELS);
+
+ // Get copy of zero cache
+ memcpy(&Cache, &p ->Cache, sizeof(Cache));
+
+ for (i=0; i < n; i++) {
+
+ accum = p -> FromInput(p, wIn, accum, Stride);
+
+ if (memcmp(wIn, Cache.CacheIn, sizeof(Cache.CacheIn)) == 0) {
+ memcpy(wOut, Cache.CacheOut, sizeof(Cache.CacheOut));
+ }
+ else {
+ TransformOnePixelWithGamutCheck(p, wIn, wOut);
+ memcpy(Cache.CacheIn, wIn, sizeof(Cache.CacheIn));
+ memcpy(Cache.CacheOut, wOut, sizeof(Cache.CacheOut));
+ }
+
+ output = p -> ToOutput(p, wOut, output, Stride);
+ }
+
+}
+
+// -------------------------------------------------------------------------------------------------------------
+
+// List of used-defined transform factories
+typedef struct _cmsTransformCollection_st {
+
+ _cmsTransformFactory Factory;
+ struct _cmsTransformCollection_st *Next;
+
+} _cmsTransformCollection;
+
+// The linked list head
+_cmsTransformPluginChunkType _cmsTransformPluginChunk = { NULL };
+
+
+// Duplicates the zone of memory used by the plug-in in the new context
+static
+void DupPluginTransformList(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ _cmsTransformPluginChunkType newHead = { NULL };
+ _cmsTransformCollection* entry;
+ _cmsTransformCollection* Anterior = NULL;
+ _cmsTransformPluginChunkType* head = (_cmsTransformPluginChunkType*) src->chunks[TransformPlugin];
+
+ // Walk the list copying all nodes
+ for (entry = head->TransformCollection;
+ entry != NULL;
+ entry = entry ->Next) {
+
+ _cmsTransformCollection *newEntry = ( _cmsTransformCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsTransformCollection));
+
+ if (newEntry == NULL)
+ return;
+
+ // We want to keep the linked list order, so this is a little bit tricky
+ newEntry -> Next = NULL;
+ if (Anterior)
+ Anterior -> Next = newEntry;
+
+ Anterior = newEntry;
+
+ if (newHead.TransformCollection == NULL)
+ newHead.TransformCollection = newEntry;
+ }
+
+ ctx ->chunks[TransformPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsTransformPluginChunkType));
+}
+
+void _cmsAllocTransformPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src)
+{
+ if (src != NULL) {
+
+ // Copy all linked list
+ DupPluginTransformList(ctx, src);
+ }
+ else {
+ static _cmsTransformPluginChunkType TransformPluginChunkType = { NULL };
+ ctx ->chunks[TransformPlugin] = _cmsSubAllocDup(ctx ->MemPool, &TransformPluginChunkType, sizeof(_cmsTransformPluginChunkType));
+ }
+}
+
+
+
+// Register new ways to transform
+cmsBool _cmsRegisterTransformPlugin(cmsContext ContextID, cmsPluginBase* Data)
+{
+ cmsPluginTransform* Plugin = (cmsPluginTransform*) Data;
+ _cmsTransformCollection* fl;
+ _cmsTransformPluginChunkType* ctx = ( _cmsTransformPluginChunkType*) _cmsContextGetClientChunk(ContextID,TransformPlugin);
+
+ if (Data == NULL) {
+
+ // Free the chain. Memory is safely freed at exit
+ ctx->TransformCollection = NULL;
+ return TRUE;
+ }
+
+ // Factory callback is required
+ if (Plugin ->Factory == NULL) return FALSE;
+
+
+ fl = (_cmsTransformCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsTransformCollection));
+ if (fl == NULL) return FALSE;
+
+ // Copy the parameters
+ fl ->Factory = Plugin ->Factory;
+
+ // Keep linked list
+ fl ->Next = ctx->TransformCollection;
+ ctx->TransformCollection = fl;
+
+ // All is ok
+ return TRUE;
+}
+
+
+void CMSEXPORT _cmsSetTransformUserData(struct _cmstransform_struct *CMMcargo, void* ptr, _cmsFreeUserDataFn FreePrivateDataFn)
+{
+ _cmsAssert(CMMcargo != NULL);
+ CMMcargo ->UserData = ptr;
+ CMMcargo ->FreeUserData = FreePrivateDataFn;
+}
+
+// returns the pointer defined by the plug-in to store private data
+void * CMSEXPORT _cmsGetTransformUserData(struct _cmstransform_struct *CMMcargo)
+{
+ _cmsAssert(CMMcargo != NULL);
+ return CMMcargo ->UserData;
+}
+
+// returns the current formatters
+void CMSEXPORT _cmsGetTransformFormatters16(struct _cmstransform_struct *CMMcargo, cmsFormatter16* FromInput, cmsFormatter16* ToOutput)
+{
+ _cmsAssert(CMMcargo != NULL);
+ if (FromInput) *FromInput = CMMcargo ->FromInput;
+ if (ToOutput) *ToOutput = CMMcargo ->ToOutput;
+}
+
+void CMSEXPORT _cmsGetTransformFormattersFloat(struct _cmstransform_struct *CMMcargo, cmsFormatterFloat* FromInput, cmsFormatterFloat* ToOutput)
+{
+ _cmsAssert(CMMcargo != NULL);
+ if (FromInput) *FromInput = CMMcargo ->FromInputFloat;
+ if (ToOutput) *ToOutput = CMMcargo ->ToOutputFloat;
+}
+
+
+// Allocate transform struct and set it to defaults. Ask the optimization plug-in about if those formats are proper
+// for separated transforms. If this is the case,
+static
+_cmsTRANSFORM* AllocEmptyTransform(cmsContext ContextID, cmsPipeline* lut,
+ cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
+{
+ _cmsTransformPluginChunkType* ctx = ( _cmsTransformPluginChunkType*) _cmsContextGetClientChunk(ContextID, TransformPlugin);
+ _cmsTransformCollection* Plugin;
+
+ // Allocate needed memory
+ _cmsTRANSFORM* p = (_cmsTRANSFORM*) _cmsMallocZero(ContextID, sizeof(_cmsTRANSFORM));
+ if (!p) return NULL;
+
+ // Store the proposed pipeline
+ p ->Lut = lut;
+
+ // Let's see if any plug-in want to do the transform by itself
+ for (Plugin = ctx ->TransformCollection;
+ Plugin != NULL;
+ Plugin = Plugin ->Next) {
+
+ if (Plugin ->Factory(&p->xform, &p->UserData, &p ->FreeUserData, &p ->Lut, InputFormat, OutputFormat, dwFlags)) {
+
+ // Last plugin in the declaration order takes control. We just keep
+ // the original parameters as a logging.
+ // Note that cmsFLAGS_CAN_CHANGE_FORMATTER is not set, so by default
+ // an optimized transform is not reusable. The plug-in can, however, change
+ // the flags and make it suitable.
+
+ p ->ContextID = ContextID;
+ p ->InputFormat = *InputFormat;
+ p ->OutputFormat = *OutputFormat;
+ p ->dwOriginalFlags = *dwFlags;
+
+ // Fill the formatters just in case the optimized routine is interested.
+ // No error is thrown if the formatter doesn't exist. It is up to the optimization
+ // factory to decide what to do in those cases.
+ p ->FromInput = _cmsGetFormatter(ContextID, *InputFormat, cmsFormatterInput, CMS_PACK_FLAGS_16BITS).Fmt16;
+ p ->ToOutput = _cmsGetFormatter(ContextID, *OutputFormat, cmsFormatterOutput, CMS_PACK_FLAGS_16BITS).Fmt16;
+ p ->FromInputFloat = _cmsGetFormatter(ContextID, *InputFormat, cmsFormatterInput, CMS_PACK_FLAGS_FLOAT).FmtFloat;
+ p ->ToOutputFloat = _cmsGetFormatter(ContextID, *OutputFormat, cmsFormatterOutput, CMS_PACK_FLAGS_FLOAT).FmtFloat;
+
+ return p;
+ }
+ }
+
+ // Not suitable for the transform plug-in, let's check the pipeline plug-in
+ if (p ->Lut != NULL)
+ _cmsOptimizePipeline(ContextID, &p->Lut, Intent, InputFormat, OutputFormat, dwFlags);
+
+ // Check whatever this is a true floating point transform
+ if (_cmsFormatterIsFloat(*InputFormat) && _cmsFormatterIsFloat(*OutputFormat)) {
+
+ // Get formatter function always return a valid union, but the contents of this union may be NULL.
+ p ->FromInputFloat = _cmsGetFormatter(ContextID, *InputFormat, cmsFormatterInput, CMS_PACK_FLAGS_FLOAT).FmtFloat;
+ p ->ToOutputFloat = _cmsGetFormatter(ContextID, *OutputFormat, cmsFormatterOutput, CMS_PACK_FLAGS_FLOAT).FmtFloat;
+ *dwFlags |= cmsFLAGS_CAN_CHANGE_FORMATTER;
+
+ if (p ->FromInputFloat == NULL || p ->ToOutputFloat == NULL) {
+
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported raster format");
+ _cmsFree(ContextID, p);
+ return NULL;
+ }
+
+ if (*dwFlags & cmsFLAGS_NULLTRANSFORM) {
+
+ p ->xform = NullFloatXFORM;
+ }
+ else {
+ // Float transforms don't use cach? always are non-NULL
+ p ->xform = FloatXFORM;
+ }
+
+ }
+ else {
+
+ if (*InputFormat == 0 && *OutputFormat == 0) {
+ p ->FromInput = p ->ToOutput = NULL;
+ *dwFlags |= cmsFLAGS_CAN_CHANGE_FORMATTER;
+ }
+ else {
+
+ int BytesPerPixelInput;
+
+ p ->FromInput = _cmsGetFormatter(ContextID, *InputFormat, cmsFormatterInput, CMS_PACK_FLAGS_16BITS).Fmt16;
+ p ->ToOutput = _cmsGetFormatter(ContextID, *OutputFormat, cmsFormatterOutput, CMS_PACK_FLAGS_16BITS).Fmt16;
+
+ if (p ->FromInput == NULL || p ->ToOutput == NULL) {
+
+ cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported raster format");
+ _cmsFree(ContextID, p);
+ return NULL;
+ }
+
+ BytesPerPixelInput = T_BYTES(p ->InputFormat);
+ if (BytesPerPixelInput == 0 || BytesPerPixelInput >= 2)
+ *dwFlags |= cmsFLAGS_CAN_CHANGE_FORMATTER;
+
+ }
+
+ if (*dwFlags & cmsFLAGS_NULLTRANSFORM) {
+
+ p ->xform = NullXFORM;
+ }
+ else {
+ if (*dwFlags & cmsFLAGS_NOCACHE) {
+
+ if (*dwFlags & cmsFLAGS_GAMUTCHECK)
+ p ->xform = PrecalculatedXFORMGamutCheck; // Gamut check, no cach? else
+ p ->xform = PrecalculatedXFORM; // No cach? no gamut check
+ }
+ else {
+
+ if (*dwFlags & cmsFLAGS_GAMUTCHECK)
+ p ->xform = CachedXFORMGamutCheck; // Gamut check, cach? else
+ p ->xform = CachedXFORM; // No gamut check, cach?
+ }
+ }
+ }
+
+ p ->InputFormat = *InputFormat;
+ p ->OutputFormat = *OutputFormat;
+ p ->dwOriginalFlags = *dwFlags;
+ p ->ContextID = ContextID;
+ p ->UserData = NULL;
+ return p;
+}
+
+static
+cmsBool GetXFormColorSpaces(int nProfiles, cmsHPROFILE hProfiles[], cmsColorSpaceSignature* Input, cmsColorSpaceSignature* Output)
+{
+ cmsColorSpaceSignature ColorSpaceIn, ColorSpaceOut;
+ cmsColorSpaceSignature PostColorSpace;
+ int i;
+
+ if (nProfiles <= 0) return FALSE;
+ if (hProfiles[0] == NULL) return FALSE;
+
+ *Input = PostColorSpace = cmsGetColorSpace(hProfiles[0]);
+
+ for (i=0; i < nProfiles; i++) {
+
+ cmsProfileClassSignature cls;
+ cmsHPROFILE hProfile = hProfiles[i];
+
+ int lIsInput = (PostColorSpace != cmsSigXYZData) &&
+ (PostColorSpace != cmsSigLabData);
+
+ if (hProfile == NULL) return FALSE;
+
+ cls = cmsGetDeviceClass(hProfile);
+
+ if (cls == cmsSigNamedColorClass) {
+
+ ColorSpaceIn = cmsSig1colorData;
+ ColorSpaceOut = (nProfiles > 1) ? cmsGetPCS(hProfile) : cmsGetColorSpace(hProfile);
+ }
+ else
+ if (lIsInput || (cls == cmsSigLinkClass)) {
+
+ ColorSpaceIn = cmsGetColorSpace(hProfile);
+ ColorSpaceOut = cmsGetPCS(hProfile);
+ }
+ else
+ {
+ ColorSpaceIn = cmsGetPCS(hProfile);
+ ColorSpaceOut = cmsGetColorSpace(hProfile);
+ }
+
+ if (i==0)
+ *Input = ColorSpaceIn;
+
+ PostColorSpace = ColorSpaceOut;
+ }
+
+ *Output = PostColorSpace;
+
+ return TRUE;
+}
+
+// Check colorspace
+static
+cmsBool IsProperColorSpace(cmsColorSpaceSignature Check, cmsUInt32Number dwFormat)
+{
+ int Space1 = T_COLORSPACE(dwFormat);
+ int Space2 = _cmsLCMScolorSpace(Check);
+
+ if (Space1 == PT_ANY) return TRUE;
+ if (Space1 == Space2) return TRUE;
+
+ if (Space1 == PT_LabV2 && Space2 == PT_Lab) return TRUE;
+ if (Space1 == PT_Lab && Space2 == PT_LabV2) return TRUE;
+
+ return FALSE;
+}
+
+// ----------------------------------------------------------------------------------------------------------------
+
+static
+void SetWhitePoint(cmsCIEXYZ* wtPt, const cmsCIEXYZ* src)
+{
+ if (src == NULL) {
+ wtPt ->X = cmsD50X;
+ wtPt ->Y = cmsD50Y;
+ wtPt ->Z = cmsD50Z;
+ }
+ else {
+ wtPt ->X = src->X;
+ wtPt ->Y = src->Y;
+ wtPt ->Z = src->Z;
+ }
+
+}
+
+// New to lcms 2.0 -- have all parameters available.
+cmsHTRANSFORM CMSEXPORT cmsCreateExtendedTransform(cmsContext ContextID,
+ cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsUInt32Number Intents[],
+ cmsFloat64Number AdaptationStates[],
+ cmsHPROFILE hGamutProfile,
+ cmsUInt32Number nGamutPCSposition,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number dwFlags)
+{
+ _cmsTRANSFORM* xform;
+ cmsColorSpaceSignature EntryColorSpace;
+ cmsColorSpaceSignature ExitColorSpace;
+ cmsPipeline* Lut;
+ cmsUInt32Number LastIntent = Intents[nProfiles-1];
+
+ // If it is a fake transform
+ if (dwFlags & cmsFLAGS_NULLTRANSFORM)
+ {
+ return AllocEmptyTransform(ContextID, NULL, INTENT_PERCEPTUAL, &InputFormat, &OutputFormat, &dwFlags);
+ }
+
+ // If gamut check is requested, make sure we have a gamut profile
+ if (dwFlags & cmsFLAGS_GAMUTCHECK) {
+ if (hGamutProfile == NULL) dwFlags &= ~cmsFLAGS_GAMUTCHECK;
+ }
+
+ // On floating point transforms, inhibit cache
+ if (_cmsFormatterIsFloat(InputFormat) || _cmsFormatterIsFloat(OutputFormat))
+ dwFlags |= cmsFLAGS_NOCACHE;
+
+ // Mark entry/exit spaces
+ if (!GetXFormColorSpaces(nProfiles, hProfiles, &EntryColorSpace, &ExitColorSpace)) {
+ cmsSignalError(ContextID, cmsERROR_NULL, "NULL input profiles on transform");
+ return NULL;
+ }
+
+ // Check if proper colorspaces
+ if (!IsProperColorSpace(EntryColorSpace, InputFormat)) {
+ cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "Wrong input color space on transform");
+ return NULL;
+ }
+
+ if (!IsProperColorSpace(ExitColorSpace, OutputFormat)) {
+ cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "Wrong output color space on transform");
+ return NULL;
+ }
+
+ // Create a pipeline with all transformations
+ Lut = _cmsLinkProfiles(ContextID, nProfiles, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
+ if (Lut == NULL) {
+ cmsSignalError(ContextID, cmsERROR_NOT_SUITABLE, "Couldn't link the profiles");
+ return NULL;
+ }
+
+ // Check channel count
+ if ((cmsChannelsOf(EntryColorSpace) != cmsPipelineInputChannels(Lut)) ||
+ (cmsChannelsOf(ExitColorSpace) != cmsPipelineOutputChannels(Lut))) {
+ cmsPipelineFree(Lut);
+ cmsSignalError(ContextID, cmsERROR_NOT_SUITABLE, "Channel count doesn't match. Profile is corrupted");
+ return NULL;
+ }
+
+
+ // All seems ok
+ xform = AllocEmptyTransform(ContextID, Lut, LastIntent, &InputFormat, &OutputFormat, &dwFlags);
+ if (xform == NULL) {
+ return NULL;
+ }
+
+ // Keep values
+ xform ->EntryColorSpace = EntryColorSpace;
+ xform ->ExitColorSpace = ExitColorSpace;
+ xform ->RenderingIntent = Intents[nProfiles-1];
+
+ // Take white points
+ SetWhitePoint(&xform->EntryWhitePoint, (cmsCIEXYZ*) cmsReadTag(hProfiles[0], cmsSigMediaWhitePointTag));
+ SetWhitePoint(&xform->ExitWhitePoint, (cmsCIEXYZ*) cmsReadTag(hProfiles[nProfiles-1], cmsSigMediaWhitePointTag));
+
+
+ // Create a gamut check LUT if requested
+ if (hGamutProfile != NULL && (dwFlags & cmsFLAGS_GAMUTCHECK))
+ xform ->GamutCheck = _cmsCreateGamutCheckPipeline(ContextID, hProfiles,
+ BPC, Intents,
+ AdaptationStates,
+ nGamutPCSposition,
+ hGamutProfile);
+
+
+ // Try to read input and output colorant table
+ if (cmsIsTag(hProfiles[0], cmsSigColorantTableTag)) {
+
+ // Input table can only come in this way.
+ xform ->InputColorant = cmsDupNamedColorList((cmsNAMEDCOLORLIST*) cmsReadTag(hProfiles[0], cmsSigColorantTableTag));
+ }
+
+ // Output is a little bit more complex.
+ if (cmsGetDeviceClass(hProfiles[nProfiles-1]) == cmsSigLinkClass) {
+
+ // This tag may exist only on devicelink profiles.
+ if (cmsIsTag(hProfiles[nProfiles-1], cmsSigColorantTableOutTag)) {
+
+ // It may be NULL if error
+ xform ->OutputColorant = cmsDupNamedColorList((cmsNAMEDCOLORLIST*) cmsReadTag(hProfiles[nProfiles-1], cmsSigColorantTableOutTag));
+ }
+
+ } else {
+
+ if (cmsIsTag(hProfiles[nProfiles-1], cmsSigColorantTableTag)) {
+
+ xform -> OutputColorant = cmsDupNamedColorList((cmsNAMEDCOLORLIST*) cmsReadTag(hProfiles[nProfiles-1], cmsSigColorantTableTag));
+ }
+ }
+
+ // Store the sequence of profiles
+ if (dwFlags & cmsFLAGS_KEEP_SEQUENCE) {
+ xform ->Sequence = _cmsCompileProfileSequence(ContextID, nProfiles, hProfiles);
+ }
+ else
+ xform ->Sequence = NULL;
+
+ // If this is a cached transform, init first value, which is zero (16 bits only)
+ if (!(dwFlags & cmsFLAGS_NOCACHE)) {
+
+ memset(&xform ->Cache.CacheIn, 0, sizeof(xform ->Cache.CacheIn));
+
+ if (xform ->GamutCheck != NULL) {
+ TransformOnePixelWithGamutCheck(xform, xform ->Cache.CacheIn, xform->Cache.CacheOut);
+ }
+ else {
+
+ xform ->Lut ->Eval16Fn(xform ->Cache.CacheIn, xform->Cache.CacheOut, xform -> Lut->Data);
+ }
+
+ }
+
+ return (cmsHTRANSFORM) xform;
+}
+
+// Multiprofile transforms: Gamut check is not available here, as it is unclear from which profile the gamut comes.
+cmsHTRANSFORM CMSEXPORT cmsCreateMultiprofileTransformTHR(cmsContext ContextID,
+ cmsHPROFILE hProfiles[],
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags)
+{
+ cmsUInt32Number i;
+ cmsBool BPC[256];
+ cmsUInt32Number Intents[256];
+ cmsFloat64Number AdaptationStates[256];
+
+ if (nProfiles <= 0 || nProfiles > 255) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong number of profiles. 1..255 expected, %d found.", nProfiles);
+ return NULL;
+ }
+
+ for (i=0; i < nProfiles; i++) {
+ BPC[i] = dwFlags & cmsFLAGS_BLACKPOINTCOMPENSATION ? TRUE : FALSE;
+ Intents[i] = Intent;
+ AdaptationStates[i] = cmsSetAdaptationStateTHR(ContextID, -1);
+ }
+
+
+ return cmsCreateExtendedTransform(ContextID, nProfiles, hProfiles, BPC, Intents, AdaptationStates, NULL, 0, InputFormat, OutputFormat, dwFlags);
+}
+
+
+
+cmsHTRANSFORM CMSEXPORT cmsCreateMultiprofileTransform(cmsHPROFILE hProfiles[],
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags)
+{
+
+ if (nProfiles <= 0 || nProfiles > 255) {
+ cmsSignalError(NULL, cmsERROR_RANGE, "Wrong number of profiles. 1..255 expected, %d found.", nProfiles);
+ return NULL;
+ }
+
+ return cmsCreateMultiprofileTransformTHR(cmsGetProfileContextID(hProfiles[0]),
+ hProfiles,
+ nProfiles,
+ InputFormat,
+ OutputFormat,
+ Intent,
+ dwFlags);
+}
+
+cmsHTRANSFORM CMSEXPORT cmsCreateTransformTHR(cmsContext ContextID,
+ cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags)
+{
+
+ cmsHPROFILE hArray[2];
+
+ hArray[0] = Input;
+ hArray[1] = Output;
+
+ return cmsCreateMultiprofileTransformTHR(ContextID, hArray, Output == NULL ? 1 : 2, InputFormat, OutputFormat, Intent, dwFlags);
+}
+
+CMSAPI cmsHTRANSFORM CMSEXPORT cmsCreateTransform(cmsHPROFILE Input,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE Output,
+ cmsUInt32Number OutputFormat,
+ cmsUInt32Number Intent,
+ cmsUInt32Number dwFlags)
+{
+ return cmsCreateTransformTHR(cmsGetProfileContextID(Input), Input, InputFormat, Output, OutputFormat, Intent, dwFlags);
+}
+
+
+cmsHTRANSFORM CMSEXPORT cmsCreateProofingTransformTHR(cmsContext ContextID,
+ cmsHPROFILE InputProfile,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE OutputProfile,
+ cmsUInt32Number OutputFormat,
+ cmsHPROFILE ProofingProfile,
+ cmsUInt32Number nIntent,
+ cmsUInt32Number ProofingIntent,
+ cmsUInt32Number dwFlags)
+{
+ cmsHPROFILE hArray[4];
+ cmsUInt32Number Intents[4];
+ cmsBool BPC[4];
+ cmsFloat64Number Adaptation[4];
+ cmsBool DoBPC = (dwFlags & cmsFLAGS_BLACKPOINTCOMPENSATION) ? TRUE : FALSE;
+
+
+ hArray[0] = InputProfile; hArray[1] = ProofingProfile; hArray[2] = ProofingProfile; hArray[3] = OutputProfile;
+ Intents[0] = nIntent; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = ProofingIntent;
+ BPC[0] = DoBPC; BPC[1] = DoBPC; BPC[2] = 0; BPC[3] = 0;
+
+ Adaptation[0] = Adaptation[1] = Adaptation[2] = Adaptation[3] = cmsSetAdaptationStateTHR(ContextID, -1);
+
+ if (!(dwFlags & (cmsFLAGS_SOFTPROOFING|cmsFLAGS_GAMUTCHECK)))
+ return cmsCreateTransformTHR(ContextID, InputProfile, InputFormat, OutputProfile, OutputFormat, nIntent, dwFlags);
+
+ return cmsCreateExtendedTransform(ContextID, 4, hArray, BPC, Intents, Adaptation,
+ ProofingProfile, 1, InputFormat, OutputFormat, dwFlags);
+
+}
+
+
+cmsHTRANSFORM CMSEXPORT cmsCreateProofingTransform(cmsHPROFILE InputProfile,
+ cmsUInt32Number InputFormat,
+ cmsHPROFILE OutputProfile,
+ cmsUInt32Number OutputFormat,
+ cmsHPROFILE ProofingProfile,
+ cmsUInt32Number nIntent,
+ cmsUInt32Number ProofingIntent,
+ cmsUInt32Number dwFlags)
+{
+ return cmsCreateProofingTransformTHR(cmsGetProfileContextID(InputProfile),
+ InputProfile,
+ InputFormat,
+ OutputProfile,
+ OutputFormat,
+ ProofingProfile,
+ nIntent,
+ ProofingIntent,
+ dwFlags);
+}
+
+
+// Grab the ContextID from an open transform. Returns NULL if a NULL transform is passed
+cmsContext CMSEXPORT cmsGetTransformContextID(cmsHTRANSFORM hTransform)
+{
+ _cmsTRANSFORM* xform = (_cmsTRANSFORM*) hTransform;
+
+ if (xform == NULL) return NULL;
+ return xform -> ContextID;
+}
+
+// Grab the input/output formats
+cmsUInt32Number CMSEXPORT cmsGetTransformInputFormat(cmsHTRANSFORM hTransform)
+{
+ _cmsTRANSFORM* xform = (_cmsTRANSFORM*) hTransform;
+
+ if (xform == NULL) return 0;
+ return xform->InputFormat;
+}
+
+cmsUInt32Number CMSEXPORT cmsGetTransformOutputFormat(cmsHTRANSFORM hTransform)
+{
+ _cmsTRANSFORM* xform = (_cmsTRANSFORM*) hTransform;
+
+ if (xform == NULL) return 0;
+ return xform->OutputFormat;
+}
+
+// For backwards compatibility
+cmsBool CMSEXPORT cmsChangeBuffersFormat(cmsHTRANSFORM hTransform,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat)
+{
+
+ _cmsTRANSFORM* xform = (_cmsTRANSFORM*) hTransform;
+ cmsFormatter16 FromInput, ToOutput;
+
+
+ // We only can afford to change formatters if previous transform is at least 16 bits
+ if (!(xform ->dwOriginalFlags & cmsFLAGS_CAN_CHANGE_FORMATTER)) {
+
+ cmsSignalError(xform ->ContextID, cmsERROR_NOT_SUITABLE, "cmsChangeBuffersFormat works only on transforms created originally with at least 16 bits of precision");
+ return FALSE;
+ }
+
+ FromInput = _cmsGetFormatter(xform->ContextID, InputFormat, cmsFormatterInput, CMS_PACK_FLAGS_16BITS).Fmt16;
+ ToOutput = _cmsGetFormatter(xform->ContextID, OutputFormat, cmsFormatterOutput, CMS_PACK_FLAGS_16BITS).Fmt16;
+
+ if (FromInput == NULL || ToOutput == NULL) {
+
+ cmsSignalError(xform -> ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported raster format");
+ return FALSE;
+ }
+
+ xform ->InputFormat = InputFormat;
+ xform ->OutputFormat = OutputFormat;
+ xform ->FromInput = FromInput;
+ xform ->ToOutput = ToOutput;
+ return TRUE;
+}
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/lcms2_internal.h b/core/src/fxcodec/lcms2/lcms2-2.6/src/lcms2_internal.h
new file mode 100644
index 0000000000..5119218ceb
--- /dev/null
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/lcms2_internal.h
@@ -0,0 +1,1032 @@
+//<<<+++OPENSOURCE
+//<<<+++OPENSOURCE_MUST_BEGIN COMMENT==TRUE
+//
+// Little Color Management System
+// Copyright (c) 1998-2014 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.
+//
+//---------------------------------------------------------------------------------
+//
+
+#ifndef _lcms_internal_H
+
+// Include plug-in foundation
+#ifndef _lcms_plugin_H
+# include "../include/lcms2_plugin.h"
+#endif
+
+// ctype is part of C99 as per 7.1.2
+#include <ctype.h>
+
+// assert macro is part of C99 as per 7.2
+#include <assert.h>
+
+// Some needed constants
+#ifndef M_PI
+# define M_PI 3.14159265358979323846
+#endif
+
+#ifndef M_LOG10E
+# define M_LOG10E 0.434294481903251827651
+#endif
+
+// BorlandC 5.5, VC2003 are broken on that
+#if defined(__BORLANDC__) || (_MSC_VER < 1400) // 1400 == VC++ 8.0
+#define sinf(x) (float)sin((float)x)
+#define sqrtf(x) (float)sqrt((float)x)
+#endif
+
+
+// Alignment of ICC file format uses 4 bytes (cmsUInt32Number)
+#define _cmsALIGNLONG(x) (((x)+(sizeof(cmsUInt32Number)-1)) & ~(sizeof(cmsUInt32Number)-1))
+
+// Alignment to memory pointer
+#define _cmsALIGNMEM(x) (((x)+(sizeof(void *) - 1)) & ~(sizeof(void *) - 1))
+
+// Maximum encodeable values in floating point
+#define MAX_ENCODEABLE_XYZ (1.0 + 32767.0/32768.0)
+#define MIN_ENCODEABLE_ab2 (-128.0)
+#define MAX_ENCODEABLE_ab2 ((65535.0/256.0) - 128.0)
+#define MIN_ENCODEABLE_ab4 (-128.0)
+#define MAX_ENCODEABLE_ab4 (127.0)
+
+// Maximum of channels for internal pipeline evaluation
+#define MAX_STAGE_CHANNELS 128
+
+// Unused parameter warning supression
+#define cmsUNUSED_PARAMETER(x) ((void)x)
+
+// The specification for "inline" is section 6.7.4 of the C99 standard (ISO/IEC 9899:1999).
+// unfortunately VisualC++ does not conform that
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+# define cmsINLINE __inline
+#else
+# define cmsINLINE static inline
+#endif
+
+// Other replacement functions
+#ifdef _MSC_VER
+# ifndef snprintf
+# define snprintf _snprintf
+# endif
+# ifndef vsnprintf
+# define vsnprintf _vsnprintf
+# endif
+#endif
+
+
+// A fast way to convert from/to 16 <-> 8 bits
+#define FROM_8_TO_16(rgb) (cmsUInt16Number) ((((cmsUInt16Number) (rgb)) << 8)|(rgb))
+#define FROM_16_TO_8(rgb) (cmsUInt8Number) ((((rgb) * 65281 + 8388608) >> 24) & 0xFF)
+
+// Code analysis is broken on asserts
+#ifdef _MSC_VER
+# if (_MSC_VER >= 1500)
+# define _cmsAssert(a) { assert((a)); __analysis_assume((a)); }
+# else
+# define _cmsAssert(a) assert((a))
+# endif
+#else
+# define _cmsAssert(a) assert((a))
+#endif
+
+//---------------------------------------------------------------------------------
+
+// Determinant lower than that are assumed zero (used on matrix invert)
+#define MATRIX_DET_TOLERANCE 0.0001
+
+//---------------------------------------------------------------------------------
+
+// Fixed point
+#define FIXED_TO_INT(x) ((x)>>16)
+#define FIXED_REST_TO_INT(x) ((x)&0xFFFFU)
+#define ROUND_FIXED_TO_INT(x) (((x)+0x8000)>>16)
+
+cmsINLINE cmsS15Fixed16Number _cmsToFixedDomain(int a) { return a + ((a + 0x7fff) / 0xffff); }
+cmsINLINE int _cmsFromFixedDomain(cmsS15Fixed16Number a) { return a - ((a + 0x7fff) >> 16); }
+
+// -----------------------------------------------------------------------------------------------------------
+
+// Fast floor conversion logic. Thanks to Sree Kotay and Stuart Nixon
+// note than this only works in the range ..-32767...+32767 because
+// mantissa is interpreted as 15.16 fixed point.
+// The union is to avoid pointer aliasing overoptimization.
+cmsINLINE int _cmsQuickFloor(cmsFloat64Number val)
+{
+#ifdef CMS_DONT_USE_FAST_FLOOR
+ return (int) floor(val);
+#else
+ const cmsFloat64Number _lcms_double2fixmagic = 68719476736.0 * 1.5; // 2^36 * 1.5, (52-16=36) uses limited precision to floor
+ union {
+ cmsFloat64Number val;
+ int halves[2];
+ } temp;
+
+ temp.val = val + _lcms_double2fixmagic;
+
+#ifdef CMS_USE_BIG_ENDIAN
+ return temp.halves[1] >> 16;
+#else
+ return temp.halves[0] >> 16;
+#endif
+#endif
+}
+
+// Fast floor restricted to 0..65535.0
+cmsINLINE cmsUInt16Number _cmsQuickFloorWord(cmsFloat64Number d)
+{
+ return (cmsUInt16Number) _cmsQuickFloor(d - 32767.0) + 32767U;
+}
+
+// Floor to word, taking care of saturation
+cmsINLINE cmsUInt16Number _cmsQuickSaturateWord(cmsFloat64Number d)
+{
+ d += 0.5;
+ if (d <= 0) return 0;
+ if (d >= 65535.0) return 0xffff;
+
+ return _cmsQuickFloorWord(d);
+}
+
+
+// Pthread support --------------------------------------------------------------------
+#ifndef CMS_NO_PTHREADS
+
+// This is the threading support. Unfortunately, it has to be platform-dependent because
+// windows does not support pthreads.
+
+#ifdef CMS_IS_WINDOWS_
+
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>
+
+
+// From: http://locklessinc.com/articles/pthreads_on_windows/
+// The pthreads API has an initialization macro that has no correspondence to anything in
+// the windows API. By investigating the internal definition of the critical section type,
+// one may work out how to initialize one without calling InitializeCriticalSection().
+// The trick here is that InitializeCriticalSection() is not allowed to fail. It tries
+// to allocate a critical section debug object, but if no memory is available, it sets
+// the pointer to a specific value. (One would expect that value to be NULL, but it is
+// actually (void *)-1 for some reason.) Thus we can use this special value for that
+// pointer, and the critical section code will work.
+
+// The other important part of the critical section type to initialize is the number
+// of waiters. This controls whether or not the mutex is locked. Fortunately, this
+// part of the critical section is unlikely to change. Apparently, many programs
+// already test critical sections to see if they are locked using this value, so
+// Microsoft felt that it was necessary to keep it set at -1 for an unlocked critical
+// section, even when they changed the underlying algorithm to be more scalable.
+// The final parts of the critical section object are unimportant, and can be set
+// to zero for their defaults. This yields an initialization macro:
+
+typedef CRITICAL_SECTION _cmsMutex;
+
+#define CMS_MUTEX_INITIALIZER {(void*) -1,-1,0,0,0,0}
+
+cmsINLINE int _cmsLockPrimitive(_cmsMutex *m)
+{
+ EnterCriticalSection(m);
+ return 0;
+}
+
+cmsINLINE int _cmsUnlockPrimitive(_cmsMutex *m)
+{
+ LeaveCriticalSection(m);
+ return 0;
+}
+
+cmsINLINE int _cmsInitMutexPrimitive(_cmsMutex *m)
+{
+ InitializeCriticalSection(m);
+ return 0;
+}
+
+cmsINLINE int _cmsDestroyMutexPrimitive(_cmsMutex *m)
+{
+ DeleteCriticalSection(m);
+ return 0;
+}
+
+cmsINLINE int _cmsEnterCriticalSectionPrimitive(_cmsMutex *m)
+{
+ EnterCriticalSection(m);
+ return 0;
+}
+
+cmsINLINE int _cmsLeaveCriticalSectionPrimitive(_cmsMutex *m)
+{
+ LeaveCriticalSection(m);
+ return 0;
+}
+
+#else
+
+// Rest of the wide world
+#include <pthread.h>
+
+#define CMS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+typedef pthread_mutex_t _cmsMutex;
+
+
+cmsINLINE int _cmsLockPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_lock(m);
+}
+
+cmsINLINE int _cmsUnlockPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_unlock(m);
+}
+
+cmsINLINE int _cmsInitMutexPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_init(m, NULL);
+}
+
+cmsINLINE int _cmsDestroyMutexPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_destroy(m);
+}
+
+cmsINLINE int _cmsEnterCriticalSectionPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_lock(m);
+}
+
+cmsINLINE int _cmsLeaveCriticalSectionPrimitive(_cmsMutex *m)
+{
+ return pthread_mutex_unlock(m);
+}
+
+#endif
+#else
+
+#define CMS_MUTEX_INITIALIZER 0
+typedef int _cmsMutex;
+
+
+cmsINLINE int _cmsLockPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+
+cmsINLINE int _cmsUnlockPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+
+cmsINLINE int _cmsInitMutexPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+
+cmsINLINE int _cmsDestroyMutexPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+
+cmsINLINE int _cmsEnterCriticalSectionPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+
+cmsINLINE int _cmsLeaveCriticalSectionPrimitive(_cmsMutex *m)
+{
+ return 0;
+ cmsUNUSED_PARAMETER(m);
+}
+#endif
+
+// Plug-In registration ---------------------------------------------------------------
+
+// Specialized function for plug-in memory management. No pairing free() since whole pool is freed at once.
+void* _cmsPluginMalloc(cmsContext ContextID, cmsUInt32Number size);
+
+// Memory management
+cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Interpolation
+cmsBool _cmsRegisterInterpPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Parametric curves
+cmsBool _cmsRegisterParametricCurvesPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Formatters management
+cmsBool _cmsRegisterFormattersPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Tag type management
+cmsBool _cmsRegisterTagTypePlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Tag management
+cmsBool _cmsRegisterTagPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Intent management
+cmsBool _cmsRegisterRenderingIntentPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Multi Process elements
+cmsBool _cmsRegisterMultiProcessElementPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Optimization
+cmsBool _cmsRegisterOptimizationPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Transform
+cmsBool _cmsRegisterTransformPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// Mutex
+cmsBool _cmsRegisterMutexPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
+
+// ---------------------------------------------------------------------------------------------------------
+
+// Suballocators.
+typedef struct _cmsSubAllocator_chunk_st {
+
+ cmsUInt8Number* Block;
+ cmsUInt32Number BlockSize;
+ cmsUInt32Number Used;
+
+ struct _cmsSubAllocator_chunk_st* next;
+
+} _cmsSubAllocator_chunk;
+
+
+typedef struct {
+
+ cmsContext ContextID;
+ _cmsSubAllocator_chunk* h;
+
+} _cmsSubAllocator;
+
+
+_cmsSubAllocator* _cmsCreateSubAlloc(cmsContext ContextID, cmsUInt32Number Initial);
+void _cmsSubAllocDestroy(_cmsSubAllocator* s);
+void* _cmsSubAlloc(_cmsSubAllocator* s, cmsUInt32Number size);
+void* _cmsSubAllocDup(_cmsSubAllocator* s, const void *ptr, cmsUInt32Number size);
+
+// ----------------------------------------------------------------------------------
+
+// The context clients.
+typedef enum {
+
+ UserPtr, // User-defined pointer
+ Logger,
+ AlarmCodesContext,
+ AdaptationStateContext,
+ MemPlugin,
+ InterpPlugin,
+ CurvesPlugin,
+ FormattersPlugin,
+ TagTypePlugin,
+ TagPlugin,
+ IntentPlugin,
+ MPEPlugin,
+ OptimizationPlugin,
+ TransformPlugin,
+ MutexPlugin,
+
+ // Last in list
+ MemoryClientMax
+
+} _cmsMemoryClient;
+
+
+// Container for memory management plug-in.
+typedef struct {
+
+ _cmsMallocFnPtrType MallocPtr;
+ _cmsMalloZerocFnPtrType MallocZeroPtr;
+ _cmsFreeFnPtrType FreePtr;
+ _cmsReallocFnPtrType ReallocPtr;
+ _cmsCallocFnPtrType CallocPtr;
+ _cmsDupFnPtrType DupPtr;
+
+} _cmsMemPluginChunkType;
+
+// Copy memory management function pointers from plug-in to chunk, taking care of missing routines
+void _cmsInstallAllocFunctions(cmsPluginMemHandler* Plugin, _cmsMemPluginChunkType* ptr);
+
+// Internal structure for context
+struct _cmsContext_struct {
+
+ struct _cmsContext_struct* Next; // Points to next context in the new style
+ _cmsSubAllocator* MemPool; // The memory pool that stores context data
+
+ void* chunks[MemoryClientMax]; // array of pointers to client chunks. Memory itself is hold in the suballocator.
+ // If NULL, then it reverts to global Context0
+
+ _cmsMemPluginChunkType DefaultMemoryManager; // The allocators used for creating the context itself. Cannot be overriden
+};
+
+// Returns a pointer to a valid context structure, including the global one if id is zero.
+// Verifies the magic number.
+struct _cmsContext_struct* _cmsGetContext(cmsContext ContextID);
+
+// Returns the block assigned to the specific zone.
+void* _cmsContextGetClientChunk(cmsContext id, _cmsMemoryClient mc);
+
+
+// Chunks of context memory by plug-in client -------------------------------------------------------
+
+// Those structures encapsulates all variables needed by the several context clients (mostly plug-ins)
+
+// Container for error logger -- not a plug-in
+typedef struct {
+
+ cmsLogErrorHandlerFunction LogErrorHandler; // Set to NULL for Context0 fallback
+
+} _cmsLogErrorChunkType;
+
+// The global Context0 storage for error logger
+extern _cmsLogErrorChunkType _cmsLogErrorChunk;
+
+// Allocate and init error logger container.
+void _cmsAllocLogErrorChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for alarm codes -- not a plug-in
+typedef struct {
+
+ cmsUInt16Number AlarmCodes[cmsMAXCHANNELS];
+
+} _cmsAlarmCodesChunkType;
+
+// The global Context0 storage for alarm codes
+extern _cmsAlarmCodesChunkType _cmsAlarmCodesChunk;
+
+// Allocate and init alarm codes container.
+void _cmsAllocAlarmCodesChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for adaptation state -- not a plug-in
+typedef struct {
+
+ cmsFloat64Number AdaptationState;
+
+} _cmsAdaptationStateChunkType;
+
+// The global Context0 storage for adaptation state
+extern _cmsAdaptationStateChunkType _cmsAdaptationStateChunk;
+
+// Allocate and init adaptation state container.
+void _cmsAllocAdaptationStateChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+
+// The global Context0 storage for memory management
+extern _cmsMemPluginChunkType _cmsMemPluginChunk;
+
+// Allocate and init memory management container.
+void _cmsAllocMemPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for interpolation plug-in
+typedef struct {
+
+ cmsInterpFnFactory Interpolators;
+
+} _cmsInterpPluginChunkType;
+
+// The global Context0 storage for interpolation plug-in
+extern _cmsInterpPluginChunkType _cmsInterpPluginChunk;
+
+// Allocate and init interpolation container.
+void _cmsAllocInterpPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for parametric curves plug-in
+typedef struct {
+
+ struct _cmsParametricCurvesCollection_st* ParametricCurves;
+
+} _cmsCurvesPluginChunkType;
+
+// The global Context0 storage for tone curves plug-in
+extern _cmsCurvesPluginChunkType _cmsCurvesPluginChunk;
+
+// Allocate and init parametric curves container.
+void _cmsAllocCurvesPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for formatters plug-in
+typedef struct {
+
+ struct _cms_formatters_factory_list* FactoryList;
+
+} _cmsFormattersPluginChunkType;
+
+// The global Context0 storage for formatters plug-in
+extern _cmsFormattersPluginChunkType _cmsFormattersPluginChunk;
+
+// Allocate and init formatters container.
+void _cmsAllocFormattersPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// This chunk type is shared by TagType plug-in and MPE Plug-in
+typedef struct {
+
+ struct _cmsTagTypeLinkedList_st* TagTypes;
+
+} _cmsTagTypePluginChunkType;
+
+
+// The global Context0 storage for tag types plug-in
+extern _cmsTagTypePluginChunkType _cmsTagTypePluginChunk;
+
+
+// The global Context0 storage for mult process elements plug-in
+extern _cmsTagTypePluginChunkType _cmsMPETypePluginChunk;
+
+// Allocate and init Tag types container.
+void _cmsAllocTagTypePluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+// Allocate and init MPE container.
+void _cmsAllocMPETypePluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+// Container for tag plug-in
+typedef struct {
+
+ struct _cmsTagLinkedList_st* Tag;
+
+} _cmsTagPluginChunkType;
+
+
+// The global Context0 storage for tag plug-in
+extern _cmsTagPluginChunkType _cmsTagPluginChunk;
+
+// Allocate and init Tag container.
+void _cmsAllocTagPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for intents plug-in
+typedef struct {
+
+ struct _cms_intents_list* Intents;
+
+} _cmsIntentsPluginChunkType;
+
+
+// The global Context0 storage for intents plug-in
+extern _cmsIntentsPluginChunkType _cmsIntentsPluginChunk;
+
+// Allocate and init intents container.
+void _cmsAllocIntentsPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for optimization plug-in
+typedef struct {
+
+ struct _cmsOptimizationCollection_st* OptimizationCollection;
+
+} _cmsOptimizationPluginChunkType;
+
+
+// The global Context0 storage for optimizers plug-in
+extern _cmsOptimizationPluginChunkType _cmsOptimizationPluginChunk;
+
+// Allocate and init optimizers container.
+void _cmsAllocOptimizationPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for transform plug-in
+typedef struct {
+
+ struct _cmsTransformCollection_st* TransformCollection;
+
+} _cmsTransformPluginChunkType;
+
+// The global Context0 storage for full-transform replacement plug-in
+extern _cmsTransformPluginChunkType _cmsTransformPluginChunk;
+
+// Allocate and init transform container.
+void _cmsAllocTransformPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// Container for mutex plug-in
+typedef struct {
+
+ _cmsCreateMutexFnPtrType CreateMutexPtr;
+ _cmsDestroyMutexFnPtrType DestroyMutexPtr;
+ _cmsLockMutexFnPtrType LockMutexPtr;
+ _cmsUnlockMutexFnPtrType UnlockMutexPtr;
+
+} _cmsMutexPluginChunkType;
+
+// The global Context0 storage for mutex plug-in
+extern _cmsMutexPluginChunkType _cmsMutexPluginChunk;
+
+// Allocate and init mutex container.
+void _cmsAllocMutexPluginChunk(struct _cmsContext_struct* ctx,
+ const struct _cmsContext_struct* src);
+
+// ----------------------------------------------------------------------------------
+// MLU internal representation
+typedef struct {
+
+ cmsUInt16Number Language;
+ cmsUInt16Number Country;
+
+ cmsUInt32Number StrW; // Offset to current unicode string
+ cmsUInt32Number Len; // Length in bytes
+
+} _cmsMLUentry;
+
+struct _cms_MLU_struct {
+
+ cmsContext ContextID;
+
+ // The directory
+ int AllocatedEntries;
+ int UsedEntries;
+ _cmsMLUentry* Entries; // Array of pointers to strings allocated in MemPool
+
+ // The Pool
+ cmsUInt32Number PoolSize; // The maximum allocated size
+ cmsUInt32Number PoolUsed; // The used size
+ void* MemPool; // Pointer to begin of memory pool
+};
+
+// Named color list internal representation
+typedef struct {
+
+ char Name[cmsMAX_PATH];
+ cmsUInt16Number PCS[3];
+ cmsUInt16Number DeviceColorant[cmsMAXCHANNELS];
+
+} _cmsNAMEDCOLOR;
+
+struct _cms_NAMEDCOLORLIST_struct {
+
+ cmsUInt32Number nColors;
+ cmsUInt32Number Allocated;
+ cmsUInt32Number ColorantCount;
+
+ char Prefix[33]; // Prefix and suffix are defined to be 32 characters at most
+ char Suffix[33];
+
+ _cmsNAMEDCOLOR* List;
+
+ cmsContext ContextID;
+};
+
+
+// ----------------------------------------------------------------------------------
+
+// This is the internal struct holding profile details.
+
+// Maximum supported tags in a profile
+#define MAX_TABLE_TAG 100
+
+typedef struct _cms_iccprofile_struct {
+
+ // I/O handler
+ cmsIOHANDLER* IOhandler;
+
+ // The thread ID
+ cmsContext ContextID;
+
+ // Creation time
+ struct tm Created;
+
+ // Only most important items found in ICC profiles
+ cmsUInt32Number Version;
+ cmsProfileClassSignature DeviceClass;
+ cmsColorSpaceSignature ColorSpace;
+ cmsColorSpaceSignature PCS;
+ cmsUInt32Number RenderingIntent;
+
+ cmsUInt32Number flags;
+ cmsUInt32Number manufacturer, model;
+ cmsUInt64Number attributes;
+ cmsUInt32Number creator;
+
+ cmsProfileID ProfileID;
+
+ // Dictionary
+ cmsUInt32Number TagCount;
+ cmsTagSignature TagNames[MAX_TABLE_TAG];
+ cmsTagSignature TagLinked[MAX_TABLE_TAG]; // The tag to wich is linked (0=none)
+ cmsUInt32Number TagSizes[MAX_TABLE_TAG]; // Size on disk
+ cmsUInt32Number TagOffsets[MAX_TABLE_TAG];
+ cmsBool TagSaveAsRaw[MAX_TABLE_TAG]; // True to write uncooked
+ void * TagPtrs[MAX_TABLE_TAG];
+ cmsTagTypeHandler* TagTypeHandlers[MAX_TABLE_TAG]; // Same structure may be serialized on different types
+ // depending on profile version, so we keep track of the
+ // type handler for each tag in the list.
+ // Special
+ cmsBool IsWrite;
+
+ // Keep a mutex for cmsReadTag -- Note that this only works if the user includes a mutex plugin
+ void * UsrMutex;
+
+} _cmsICCPROFILE;
+
+// IO helpers for profiles
+cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc);
+cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace);
+int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks);
+
+// Tag types
+cmsTagTypeHandler* _cmsGetTagTypeHandler(cmsContext ContextID, cmsTagTypeSignature sig);
+cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig);
+cmsTagDescriptor* _cmsGetTagDescriptor(cmsContext ContextID, cmsTagSignature sig);
+
+// Error logging ---------------------------------------------------------------------------------------------------------
+
+void _cmsTagSignature2String(char String[5], cmsTagSignature sig);
+
+// Interpolation ---------------------------------------------------------------------------------------------------------
+
+cmsInterpParams* _cmsComputeInterpParams(cmsContext ContextID, int nSamples, int InputChan, int OutputChan, const void* Table, cmsUInt32Number dwFlags);
+cmsInterpParams* _cmsComputeInterpParamsEx(cmsContext ContextID, const cmsUInt32Number nSamples[], int InputChan, int OutputChan, const void* Table, cmsUInt32Number dwFlags);
+void _cmsFreeInterpParams(cmsInterpParams* p);
+cmsBool _cmsSetInterpolationRoutine(cmsContext ContextID, cmsInterpParams* p);
+
+// Curves ----------------------------------------------------------------------------------------------------------------
+
+// This struct holds information about a segment, plus a pointer to the function that implements the evaluation.
+// In the case of table-based, Eval pointer is set to NULL
+
+// The gamma function main structure
+struct _cms_curve_struct {
+
+ cmsInterpParams* InterpParams; // Private optimizations for interpolation
+
+ cmsUInt32Number nSegments; // Number of segments in the curve. Zero for a 16-bit based tables
+ cmsCurveSegment* Segments; // The segments
+ cmsInterpParams** SegInterp; // Array of private optimizations for interpolation in table-based segments
+
+ cmsParametricCurveEvaluator* Evals; // Evaluators (one per segment)
+
+ // 16 bit Table-based representation follows
+ cmsUInt32Number nEntries; // Number of table elements
+ cmsUInt16Number* Table16; // The table itself.
+};
+
+
+// Pipelines & Stages ---------------------------------------------------------------------------------------------
+
+// A single stage
+struct _cmsStage_struct {
+
+ cmsContext ContextID;
+
+ cmsStageSignature Type; // Identifies the stage
+ cmsStageSignature Implements; // Identifies the *function* of the stage (for optimizations)
+
+ cmsUInt32Number InputChannels; // Input channels -- for optimization purposes
+ cmsUInt32Number OutputChannels; // Output channels -- for optimization purposes
+
+ _cmsStageEvalFn EvalPtr; // Points to fn that evaluates the stage (always in floating point)
+ _cmsStageDupElemFn DupElemPtr; // Points to a fn that duplicates the *data* of the stage
+ _cmsStageFreeElemFn FreePtr; // Points to a fn that sets the *data* of the stage free
+
+ // A generic pointer to whatever memory needed by the stage
+ void* Data;
+
+ // Maintains linked list (used internally)
+ struct _cmsStage_struct* Next;
+};
+
+
+// Special Stages (cannot be saved)
+cmsStage* _cmsStageAllocLab2XYZ(cmsContext ContextID);
+cmsStage* _cmsStageAllocXYZ2Lab(cmsContext ContextID);
+cmsStage* _cmsStageAllocLabPrelin(cmsContext ContextID);
+cmsStage* _cmsStageAllocLabV2ToV4(cmsContext ContextID);
+cmsStage* _cmsStageAllocLabV2ToV4curves(cmsContext ContextID);
+cmsStage* _cmsStageAllocLabV4ToV2(cmsContext ContextID);
+cmsStage* _cmsStageAllocNamedColor(cmsNAMEDCOLORLIST* NamedColorList, cmsBool UsePCS);
+cmsStage* _cmsStageAllocIdentityCurves(cmsContext ContextID, int nChannels);
+cmsStage* _cmsStageAllocIdentityCLut(cmsContext ContextID, int nChan);
+cmsStage* _cmsStageNormalizeFromLabFloat(cmsContext ContextID);
+cmsStage* _cmsStageNormalizeFromXyzFloat(cmsContext ContextID);
+cmsStage* _cmsStageNormalizeToLabFloat(cmsContext ContextID);
+cmsStage* _cmsStageNormalizeToXyzFloat(cmsContext ContextID);
+
+// For curve set only
+cmsToneCurve** _cmsStageGetPtrToCurveSet(const cmsStage* mpe);
+
+
+// Pipeline Evaluator (in floating point)
+typedef void (* _cmsPipelineEvalFloatFn)(const cmsFloat32Number In[],
+ cmsFloat32Number Out[],
+ const void* Data);
+
+struct _cmsPipeline_struct {
+
+ cmsStage* Elements; // Points to elements chain
+ cmsUInt32Number InputChannels, OutputChannels;
+
+ // Data & evaluators
+ void *Data;
+
+ _cmsOPTeval16Fn Eval16Fn;
+ _cmsPipelineEvalFloatFn EvalFloatFn;
+ _cmsFreeUserDataFn FreeDataFn;
+ _cmsDupUserDataFn DupDataFn;
+
+ cmsContext ContextID; // Environment
+
+ cmsBool SaveAs8Bits; // Implementation-specific: save as 8 bits if possible
+};
+
+// LUT reading & creation -------------------------------------------------------------------------------------------
+
+// Read tags using low-level function, provide necessary glue code to adapt versions, etc. All those return a brand new copy
+// of the LUTS, since ownership of original is up to the profile. The user should free allocated resources.
+
+cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent);
+cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent);
+cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent);
+
+// Special values
+cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile);
+cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile);
+
+// Profile linker --------------------------------------------------------------------------------------------------
+
+cmsPipeline* _cmsLinkProfiles(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number TheIntents[],
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+// Sequence --------------------------------------------------------------------------------------------------------
+
+cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile);
+cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq);
+cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[]);
+
+
+// LUT optimization ------------------------------------------------------------------------------------------------
+
+cmsUInt16Number _cmsQuantizeVal(cmsFloat64Number i, int MaxSamples);
+int _cmsReasonableGridpointsByColorspace(cmsColorSpaceSignature Colorspace, cmsUInt32Number dwFlags);
+
+cmsBool _cmsEndPointsBySpace(cmsColorSpaceSignature Space,
+ cmsUInt16Number **White,
+ cmsUInt16Number **Black,
+ cmsUInt32Number *nOutputs);
+
+cmsBool _cmsOptimizePipeline(cmsContext ContextID,
+ cmsPipeline** Lut,
+ int Intent,
+ cmsUInt32Number* InputFormat,
+ cmsUInt32Number* OutputFormat,
+ cmsUInt32Number* dwFlags );
+
+
+// Hi level LUT building ----------------------------------------------------------------------------------------------
+
+cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID,
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsUInt32Number Intents[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number nGamutPCSposition,
+ cmsHPROFILE hGamut);
+
+
+// Formatters ------------------------------------------------------------------------------------------------------------
+
+#define cmsFLAGS_CAN_CHANGE_FORMATTER 0x02000000 // Allow change buffer format
+
+cmsBool _cmsFormatterIsFloat(cmsUInt32Number Type);
+cmsBool _cmsFormatterIs8bit(cmsUInt32Number Type);
+
+cmsFormatter _cmsGetFormatter(cmsContext ContextID,
+ cmsUInt32Number Type, // Specific type, i.e. TYPE_RGB_8
+ cmsFormatterDirection Dir,
+ cmsUInt32Number dwFlags);
+
+
+#ifndef CMS_NO_HALF_SUPPORT
+
+// Half float
+cmsFloat32Number _cmsHalf2Float(cmsUInt16Number h);
+cmsUInt16Number _cmsFloat2Half(cmsFloat32Number flt);
+
+#endif
+
+// Transform logic ------------------------------------------------------------------------------------------------------
+
+struct _cmstransform_struct;
+
+typedef struct {
+
+ // 1-pixel cache (16 bits only)
+ cmsUInt16Number CacheIn[cmsMAXCHANNELS];
+ cmsUInt16Number CacheOut[cmsMAXCHANNELS];
+
+} _cmsCACHE;
+
+
+
+// Transformation
+typedef struct _cmstransform_struct {
+
+ cmsUInt32Number InputFormat, OutputFormat; // Keep formats for further reference
+
+ // Points to transform code
+ _cmsTransformFn xform;
+
+ // Formatters, cannot be embedded into LUT because cache
+ cmsFormatter16 FromInput;
+ cmsFormatter16 ToOutput;
+
+ cmsFormatterFloat FromInputFloat;
+ cmsFormatterFloat ToOutputFloat;
+
+ // 1-pixel cache seed for zero as input (16 bits, read only)
+ _cmsCACHE Cache;
+
+ // A Pipeline holding the full (optimized) transform
+ cmsPipeline* Lut;
+
+ // A Pipeline holding the gamut check. It goes from the input space to bilevel
+ cmsPipeline* GamutCheck;
+
+ // Colorant tables
+ cmsNAMEDCOLORLIST* InputColorant; // Input Colorant table
+ cmsNAMEDCOLORLIST* OutputColorant; // Colorant table (for n chans > CMYK)
+
+ // Informational only
+ cmsColorSpaceSignature EntryColorSpace;
+ cmsColorSpaceSignature ExitColorSpace;
+
+ // White points (informative only)
+ cmsCIEXYZ EntryWhitePoint;
+ cmsCIEXYZ ExitWhitePoint;
+
+ // Profiles used to create the transform
+ cmsSEQ* Sequence;
+
+ cmsUInt32Number dwOriginalFlags;
+ cmsFloat64Number AdaptationState;
+
+ // The intent of this transform. That is usually the last intent in the profilechain, but may differ
+ cmsUInt32Number RenderingIntent;
+
+ // An id that uniquely identifies the running context. May be null.
+ cmsContext ContextID;
+
+ // A user-defined pointer that can be used to store data for transform plug-ins
+ void* UserData;
+ _cmsFreeUserDataFn FreeUserData;
+
+} _cmsTRANSFORM;
+
+// --------------------------------------------------------------------------------------------------
+
+cmsHTRANSFORM _cmsChain2Lab(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ const cmsUInt32Number Intents[],
+ const cmsHPROFILE hProfiles[],
+ const cmsBool BPC[],
+ const cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+
+cmsToneCurve* _cmsBuildKToneCurve(cmsContext ContextID,
+ cmsUInt32Number nPoints,
+ cmsUInt32Number nProfiles,
+ const cmsUInt32Number Intents[],
+ const cmsHPROFILE hProfiles[],
+ const cmsBool BPC[],
+ const cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags);
+
+cmsBool _cmsAdaptationMatrix(cmsMAT3* r, const cmsMAT3* ConeMatrix, const cmsCIEXYZ* FromIll, const cmsCIEXYZ* ToIll);
+
+cmsBool _cmsBuildRGB2XYZtransferMatrix(cmsMAT3* r, const cmsCIExyY* WhitePoint, const cmsCIExyYTRIPLE* Primaries);
+
+
+#define _lcms_internal_H
+#endif
+//<<<+++OPENSOURCE_MUST_END
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscam02.c b/core/src/fxcodec/lcms2/src/fx_cmscam02.c
new file mode 100644
index 0000000000..05584aa8f8
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmscam02.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscgats.c b/core/src/fxcodec/lcms2/src/fx_cmscgats.c
new file mode 100644
index 0000000000..80dcba7830
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmscgats.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c b/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c
new file mode 100644
index 0000000000..9b8a292f7b
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmserr.c b/core/src/fxcodec/lcms2/src/fx_cmserr.c
new file mode 100644
index 0000000000..6929e7b34c
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmserr.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsgamma.c b/core/src/fxcodec/lcms2/src/fx_cmsgamma.c
new file mode 100644
index 0000000000..f5bb690220
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsgamma.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsgmt.c b/core/src/fxcodec/lcms2/src/fx_cmsgmt.c
new file mode 100644
index 0000000000..ea82b15be0
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsgmt.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmshalf.c b/core/src/fxcodec/lcms2/src/fx_cmshalf.c
new file mode 100644
index 0000000000..6ed6b2b075
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmshalf.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsintrp.c b/core/src/fxcodec/lcms2/src/fx_cmsintrp.c
new file mode 100644
index 0000000000..affca53ca9
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsintrp.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsio0.c b/core/src/fxcodec/lcms2/src/fx_cmsio0.c
new file mode 100644
index 0000000000..2b6bc689cc
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsio0.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsio1.c b/core/src/fxcodec/lcms2/src/fx_cmsio1.c
new file mode 100644
index 0000000000..192e879063
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsio1.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmslut.c b/core/src/fxcodec/lcms2/src/fx_cmslut.c
new file mode 100644
index 0000000000..d9a319c774
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmslut.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsmd5.c b/core/src/fxcodec/lcms2/src/fx_cmsmd5.c
new file mode 100644
index 0000000000..3a3a99719c
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsmd5.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c b/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c
new file mode 100644
index 0000000000..667ac1f09e
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsnamed.c b/core/src/fxcodec/lcms2/src/fx_cmsnamed.c
new file mode 100644
index 0000000000..5e1b028e94
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsnamed.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsopt.c b/core/src/fxcodec/lcms2/src/fx_cmsopt.c
new file mode 100644
index 0000000000..f12429c551
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsopt.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmspack.c b/core/src/fxcodec/lcms2/src/fx_cmspack.c
new file mode 100644
index 0000000000..6e0640905d
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmspack.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmspcs.c b/core/src/fxcodec/lcms2/src/fx_cmspcs.c
new file mode 100644
index 0000000000..48ce39de52
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmspcs.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsplugin.c b/core/src/fxcodec/lcms2/src/fx_cmsplugin.c
new file mode 100644
index 0000000000..39266c2222
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsplugin.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsps2.c b/core/src/fxcodec/lcms2/src/fx_cmsps2.c
new file mode 100644
index 0000000000..dcf55409e4
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsps2.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmssamp.c b/core/src/fxcodec/lcms2/src/fx_cmssamp.c
new file mode 100644
index 0000000000..f1468fe16d
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmssamp.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmssm.c b/core/src/fxcodec/lcms2/src/fx_cmssm.c
new file mode 100644
index 0000000000..a53ad71f42
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmssm.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmstypes.c b/core/src/fxcodec/lcms2/src/fx_cmstypes.c
new file mode 100644
index 0000000000..19f9e706c1
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmstypes.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsvirt.c b/core/src/fxcodec/lcms2/src/fx_cmsvirt.c
new file mode 100644
index 0000000000..3e5397f11b
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsvirt.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c b/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c
new file mode 100644
index 0000000000..c11afd6016
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsxform.c b/core/src/fxcodec/lcms2/src/fx_cmsxform.c
new file mode 100644
index 0000000000..a4b6d85536
--- /dev/null
+++ b/core/src/fxcodec/lcms2/src/fx_cmsxform.c
@@ -0,0 +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"
diff --git a/core/src/fxcodec/libjpeg/cderror.h b/core/src/fxcodec/libjpeg/cderror.h
new file mode 100644
index 0000000000..c19d38fb4a
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/cderror.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/libjpeg/cdjpeg.h b/core/src/fxcodec/libjpeg/cdjpeg.h
new file mode 100644
index 0000000000..be12278810
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/cdjpeg.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c b/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c
new file mode 100644
index 0000000000..ec88cff26d
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c b/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c
new file mode 100644
index 0000000000..bf13542539
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c b/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c
new file mode 100644
index 0000000000..ea2a4a6cbb
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c b/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c
new file mode 100644
index 0000000000..8ff863a14b
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c
new file mode 100644
index 0000000000..d4082043a5
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c
new file mode 100644
index 0000000000..9ed1fe6da2
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c b/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c
new file mode 100644
index 0000000000..a0d8f8148f
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c
new file mode 100644
index 0000000000..25d61f2284
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c
new file mode 100644
index 0000000000..d0a9e87650
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c
new file mode 100644
index 0000000000..870f775191
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c b/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c
new file mode 100644
index 0000000000..964909ac4e
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c b/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c
new file mode 100644
index 0000000000..8b1b9b9fb6
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c
new file mode 100644
index 0000000000..c2655fbb48
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c b/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c
new file mode 100644
index 0000000000..539d87bbae
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c b/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c
new file mode 100644
index 0000000000..7256bd7356
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c b/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c
new file mode 100644
index 0000000000..ed541159f1
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c b/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c
new file mode 100644
index 0000000000..1b24b707b2
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c b/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c
new file mode 100644
index 0000000000..8c969eb309
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c
new file mode 100644
index 0000000000..89041f36f4
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c b/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c
new file mode 100644
index 0000000000..5cd35f5ec9
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c
new file mode 100644
index 0000000000..811d51d51a
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c
new file mode 100644
index 0000000000..b724d52b95
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c b/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c
new file mode 100644
index 0000000000..46a7efce2e
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c
new file mode 100644
index 0000000000..0fc77c7c17
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c
new file mode 100644
index 0000000000..60f2139f58
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c
new file mode 100644
index 0000000000..4adfd82375
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c
new file mode 100644
index 0000000000..29a996c063
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c
new file mode 100644
index 0000000000..3c19d0ab24
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c
new file mode 100644
index 0000000000..855be5c8aa
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c b/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c
new file mode 100644
index 0000000000..a2eae954b5
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c b/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c
new file mode 100644
index 0000000000..c076a6c2bd
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jerror.c b/core/src/fxcodec/libjpeg/fpdfapi_jerror.c
new file mode 100644
index 0000000000..9d673ba845
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jerror.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c b/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c
new file mode 100644
index 0000000000..9cd3dd798e
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c b/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c
new file mode 100644
index 0000000000..88810a7d8e
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c
new file mode 100644
index 0000000000..c0c504c2a6
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c
new file mode 100644
index 0000000000..82f5cc742a
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c
new file mode 100644
index 0000000000..7eb1d74afa
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c
new file mode 100644
index 0000000000..19ee66597b
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c b/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c
new file mode 100644
index 0000000000..98aed723a6
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jutils.c b/core/src/fxcodec/libjpeg/fpdfapi_jutils.c
new file mode 100644
index 0000000000..251fd504e5
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jutils.c
@@ -0,0 +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_
diff --git a/core/src/fxcodec/libjpeg/jchuff.h b/core/src/fxcodec/libjpeg/jchuff.h
new file mode 100644
index 0000000000..8c02c09ad0
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jchuff.h
@@ -0,0 +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[]));
diff --git a/core/src/fxcodec/libjpeg/jconfig.h b/core/src/fxcodec/libjpeg/jconfig.h
new file mode 100644
index 0000000000..2f4da14c54
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jconfig.h
@@ -0,0 +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 */
diff --git a/core/src/fxcodec/libjpeg/jdct.h b/core/src/fxcodec/libjpeg/jdct.h
new file mode 100644
index 0000000000..b664cab0d2
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jdct.h
@@ -0,0 +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
diff --git a/core/src/fxcodec/libjpeg/jdhuff.h b/core/src/fxcodec/libjpeg/jdhuff.h
new file mode 100644
index 0000000000..12c0747709
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jdhuff.h
@@ -0,0 +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));
diff --git a/core/src/fxcodec/libjpeg/jerror.h b/core/src/fxcodec/libjpeg/jerror.h
new file mode 100644
index 0000000000..a2b8f96f8f
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jerror.h
@@ -0,0 +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 */
diff --git a/core/src/fxcodec/libjpeg/jinclude.h b/core/src/fxcodec/libjpeg/jinclude.h
new file mode 100644
index 0000000000..070188a9b6
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jinclude.h
@@ -0,0 +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)))
diff --git a/core/src/fxcodec/libjpeg/jmemsys.h b/core/src/fxcodec/libjpeg/jmemsys.h
new file mode 100644
index 0000000000..baa7b7f993
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jmemsys.h
@@ -0,0 +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));
diff --git a/core/src/fxcodec/libjpeg/jmorecfg.h b/core/src/fxcodec/libjpeg/jmorecfg.h
new file mode 100644
index 0000000000..3602114a5c
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jmorecfg.h
@@ -0,0 +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 */
diff --git a/core/src/fxcodec/libjpeg/jpegint.h b/core/src/fxcodec/libjpeg/jpegint.h
new file mode 100644
index 0000000000..685a3610b2
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jpegint.h
@@ -0,0 +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 */
diff --git a/core/src/fxcodec/libjpeg/jpeglib.h b/core/src/fxcodec/libjpeg/jpeglib.h
new file mode 100644
index 0000000000..ac2aff90fd
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jpeglib.h
@@ -0,0 +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 */
diff --git a/core/src/fxcodec/libjpeg/jversion.h b/core/src/fxcodec/libjpeg/jversion.h
new file mode 100644
index 0000000000..dadd453a41
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/jversion.h
@@ -0,0 +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"
diff --git a/core/src/fxcodec/libjpeg/makefile b/core/src/fxcodec/libjpeg/makefile
new file mode 100644
index 0000000000..777d83b15a
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/makefile
@@ -0,0 +1,8 @@
+sources = fpdfapi_jcapimin.c fpdfapi_jcapistd.c fpdfapi_jccoefct.c fpdfapi_jccolor.c fpdfapi_jcdctmgr.c fpdfapi_jchuff.c fpdfapi_jcinit.c fpdfapi_jcmainct.c fpdfapi_jcmarker.c fpdfapi_jcmaster.c \
+ fpdfapi_jcomapi.c fpdfapi_jcparam.c fpdfapi_jcphuff.c fpdfapi_jcprepct.c fpdfapi_jcsample.c fpdfapi_jctrans.c fpdfapi_jdapimin.c fpdfapi_jdapistd.c fpdfapi_jdcoefct.c \
+ fpdfapi_jdcolor.c fpdfapi_jddctmgr.c fpdfapi_jdhuff.c fpdfapi_jdinput.c fpdfapi_jdmainct.c fpdfapi_jdmarker.c fpdfapi_jdmaster.c fpdfapi_jdmerge.c fpdfapi_jdphuff.c fpdfapi_jdpostct.c fpdfapi_jdsample.c \
+ fpdfapi_jdtrans.c fpdfapi_jerror.c fpdfapi_jfdctfst.c fpdfapi_jfdctint.c fpdfapi_jcapimin.c fpdfapi_jidctfst.c fpdfapi_jidctint.c fpdfapi_jidctred.c fpdfapi_jmemmgr.c \
+ fpdfapi_jmemnobs.c fpdfapi_jutils.c
+armsources=$(sources)
+armsourcesc=$(sources)
+include ../../src/makefile
diff --git a/core/src/fxcodec/libjpeg/transupp.h b/core/src/fxcodec/libjpeg/transupp.h
new file mode 100644
index 0000000000..eb0b05566a
--- /dev/null
+++ b/core/src/fxcodec/libjpeg/transupp.h
@@ -0,0 +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));